m2skills

vertical order bt cpp

May 16th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. // program to print the nodes of a binary tree in vertical order
  2. #include <iostream>
  3. #include <queue>
  4. #include <map>
  5. #include <list>
  6. #include <stack>
  7.  
  8. using namespace std;
  9.  
  10. // node class
  11. class node{
  12. public:
  13.     int data;
  14.     int hd;
  15.     node* left;
  16.     node* right;
  17. };
  18.  
  19. // function that returns a pointer to new node
  20. node* createNode(int element){
  21.     node* temp = (node*) malloc(sizeof(node));
  22.     temp->data = element;
  23.     temp->hd = -1;
  24.     temp->left = NULL;
  25.     temp->right = NULL;
  26.     return temp;
  27. }
  28.  
  29. // function to print the nodes of the binary tree in vertical order
  30. // using horizontal distance of a node from the root node
  31. void vertical_order(node* root){
  32.     if(root == NULL){
  33.         return;
  34.     }
  35.  
  36.     // initialize a map and queue
  37.     // we will use iterative BFS traversal to solve this
  38.     map<int, vector<int> > M;
  39.     queue<node *> Q;
  40.  
  41.     // push root in queue and initialize horizontal distance
  42.     Q.push(root);
  43.     int horizontal_distance = root->hd;
  44.  
  45.     while(!Q.empty()){
  46.         node* current = Q.front();
  47.         Q.pop();
  48.         M[current->hd].push_back(current->data);
  49.  
  50.         // update horizontal distance of the left child and put it in the queue
  51.         if(current->left != NULL){
  52.             current->left->hd = current->hd - 1;
  53.             Q.push(current->left);
  54.         }
  55.         // update horizontal distance of the right child and put it in the queue
  56.         if(current->right != NULL) {
  57.             current->right->hd = current->hd + 1;
  58.             Q.push(current->right);
  59.         }
  60.     }
  61.  
  62.     // printing the contents of the map
  63.     for (auto iter = M.begin(); iter != M.end(); iter++){
  64.         vector<int> simple = iter->second;
  65.         cout<<"vertical distance = "<<iter->first<<" elements : ";
  66.         for(auto i=0; i != simple.size(); i++){
  67.             cout<<simple[i]<<" ";
  68.         }
  69.         cout<<"\n";
  70.     }
  71. }
  72.  
  73. int main() {
  74.  
  75.     node* head = createNode(1);
  76.     head->left = createNode(2);
  77.     head->right = createNode(3);
  78.     head->left->left = createNode(4);
  79.     head->left->right = createNode(5);
  80.     head->right->right = createNode(6);
  81.     head->left->left->right = createNode(7);
  82.     head->right->right->left = createNode(8);
  83.     head->left->left->right->left = createNode(9);
  84.     head->left->left->right->left->left = createNode(10);
  85.     head->right->right->left->right = createNode(11);
  86.     head->hd = 0;
  87.     vertical_order(head);
  88.  
  89. }
  90.  
  91. /*
  92.  
  93. vertical distance = -3 elements : 10
  94. vertical distance = -2 elements : 4 9
  95. vertical distance = -1 elements : 2 7
  96. vertical distance = 0 elements : 1 5
  97. vertical distance = 1 elements : 3 8
  98. vertical distance = 2 elements : 6 11
  99. Process finished with exit code 0
  100.  
  101. */
Add Comment
Please, Sign In to add comment