m2skills

bottom view cpp

Jun 3rd, 2018
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to print the bottom view of a given binary tree
  3. #include <iostream>
  4. #include <queue>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. // node class
  10. class node{
  11. public:
  12.     int data;
  13.     int hd;
  14.     node* left;
  15.     node* right;
  16. };
  17.  
  18. // function that returns a pointer to new node
  19. node* createNode(int element){
  20.     node* temp = (node*) malloc(sizeof(node));
  21.     temp->data = element;
  22.     temp->hd = -1;
  23.     temp->left = NULL;
  24.     temp->right = NULL;
  25.     return temp;
  26. }
  27.  
  28. // function to print the bottom view of the binary tree
  29. void bottom_view(node *root){
  30.     if(root == NULL){
  31.         return;
  32.     }
  33.  
  34.     // we keep track of the horizontal distance of the node from the root node
  35.     // to print the bottom view
  36.     int hd = 0; // hd = horizontal distance from the root node
  37.     hd = root->hd;
  38.  
  39.     // creating a Queue for storing node for level wise traversal
  40.     // and a map for mapping horizontal distances with the node data
  41.     queue<node *> Q;
  42.     map<int, int> Map;
  43.  
  44.     Q.push(root);
  45.     while(!Q.empty()){
  46.  
  47.         // pop the first node from the queue
  48.         node *p = Q.front();
  49.         Q.pop();
  50.        
  51.         hd = p->hd;
  52.         Map[hd] = p->data;
  53.  
  54.         // increase the horizontal distance from the root node in negative direction
  55.         // and push the node on queue
  56.         if(p->left != NULL){
  57.             p->left->hd = hd - 1;
  58.             Q.push(p->left);
  59.         }
  60.  
  61.         // increase the horizontal distance from the root node in positive direction
  62.         // and push the node on queue
  63.         if(p->right != NULL){
  64.             p->right->hd = hd + 1;
  65.             Q.push(p->right);
  66.         }
  67.     }
  68.  
  69.     // print the generated map
  70.     cout<<"Bottom view of the binary tree is : "<<endl;
  71.     for (auto iter = Map.begin(); iter != Map.end(); iter++){
  72.         cout<<iter->second <<" ";
  73.     }
  74. }
  75.  
  76. int main() {
  77.     node* head = createNode(1);
  78.     head->left = createNode(2);
  79.     head->right = createNode(3);
  80.     head->left->left = createNode(4);
  81.     head->left->right = createNode(5);
  82.     head->right->right = createNode(6);
  83.     head->left->left->right = createNode(7);
  84.     head->right->right->left = createNode(8);
  85.     head->left->left->right->left = createNode(9);
  86.     head->left->left->right->left->left = createNode(10);
  87.     head->right->right->left->right = createNode(11);
  88.     head->hd = 0;
  89.     bottom_view(head);
  90.     return 0;
  91. }
Add Comment
Please, Sign In to add comment