Advertisement
shabbyheart

Tree traverse

Jun 16th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Node{
  4. public:
  5.  
  6.     int key;
  7.     Node *left;
  8.     Node *right;
  9.     Node( int k)
  10.     {
  11.         key = k;
  12.         left = right = NULL;
  13.     }
  14. };
  15. class Tree{
  16. public:
  17.     Node *root;
  18.     void printInorder( Node *temp )
  19.     {
  20.  
  21.         if( temp == NULL){
  22.             return;
  23.         }
  24.         printInorder( temp -> left);
  25.         cout<<temp->key;
  26.         printInorder( temp -> right);
  27.     }
  28. };
  29. int main()
  30. {
  31.     Node *n1 = new Node(1);
  32.     Node *n2 = new Node(2);
  33.     Node *n3 = new Node(3);
  34.     Node *n4 = new Node(4);
  35.     Node *n5 = new Node(5);
  36.  
  37.     Tree *t = new Tree();
  38.     t -> root = n1;
  39.     t -> root -> left = n2;
  40.     t -> root ->right = n3;
  41.     t -> root -> left -> left = n4;
  42.     t -> root -> left -> right = n5;
  43.  
  44.     t ->printInorder(n1);
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement