m2skills

top view cpp

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