|
| 1 | +// C++ Program to print Bottom View of Binary Tree |
| 2 | +#include<bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +// Tree node class |
| 6 | +struct Node |
| 7 | +{ |
| 8 | + int data; //data of the node |
| 9 | + int hd; //horizontal distance of the node |
| 10 | + Node *left, *right; //left and right references |
| 11 | + |
| 12 | + // Constructor of tree node |
| 13 | + Node(int key) |
| 14 | + { |
| 15 | + data = key; |
| 16 | + hd = INT_MAX; |
| 17 | + left = right = NULL; |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +// Method that prints the bottom view. |
| 22 | +void bottomView(Node *root) |
| 23 | +{ |
| 24 | + if (root == NULL) |
| 25 | + return; |
| 26 | + |
| 27 | + // Initialize a variable 'hd' with 0 |
| 28 | + // for the root element. |
| 29 | + int hd = 0; |
| 30 | + |
| 31 | + // TreeMap which stores key value pair |
| 32 | + // sorted on key value |
| 33 | + map<int, int> m; |
| 34 | + |
| 35 | + // Queue to store tree nodes in level |
| 36 | + // order traversal |
| 37 | + queue<Node *> q; |
| 38 | + |
| 39 | + // Assign initialized horizontal distance |
| 40 | + // value to root node and add it to the queue. |
| 41 | + root->hd = hd; |
| 42 | + q.push(root); // In STL, push() is used enqueue an item |
| 43 | + |
| 44 | + // Loop until the queue is empty (standard |
| 45 | + // level order loop) |
| 46 | + while (!q.empty()) |
| 47 | + { |
| 48 | + Node *temp = q.front(); |
| 49 | + q.pop(); // In STL, pop() is used dequeue an item |
| 50 | + |
| 51 | + // Extract the horizontal distance value |
| 52 | + // from the dequeued tree node. |
| 53 | + hd = temp->hd; |
| 54 | + |
| 55 | + // Put the dequeued tree node to TreeMap |
| 56 | + // having key as horizontal distance. Every |
| 57 | + // time we find a node having same horizontal |
| 58 | + // distance we need to replace the data in |
| 59 | + // the map. |
| 60 | + m[hd] = temp->data; |
| 61 | + |
| 62 | + // If the dequeued node has a left child, add |
| 63 | + // it to the queue with a horizontal distance hd-1. |
| 64 | + if (temp->left != NULL) |
| 65 | + { |
| 66 | + temp->left->hd = hd-1; |
| 67 | + q.push(temp->left); |
| 68 | + } |
| 69 | + |
| 70 | + // If the dequeued node has a right child, add |
| 71 | + // it to the queue with a horizontal distance |
| 72 | + // hd+1. |
| 73 | + if (temp->right != NULL) |
| 74 | + { |
| 75 | + temp->right->hd = hd+1; |
| 76 | + q.push(temp->right); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Traverse the map elements using the iterator. |
| 81 | + for (auto i = m.begin(); i != m.end(); ++i) |
| 82 | + cout << i->second << " "; |
| 83 | +} |
| 84 | + |
| 85 | +// Driver Code |
| 86 | +int main() |
| 87 | +{ |
| 88 | + Node *root = new Node(20); |
| 89 | + root->left = new Node(8); |
| 90 | + root->right = new Node(22); |
| 91 | + root->left->left = new Node(5); |
| 92 | + root->left->right = new Node(3); |
| 93 | + root->right->left = new Node(4); |
| 94 | + root->right->right = new Node(25); |
| 95 | + root->left->right->left = new Node(10); |
| 96 | + root->left->right->right = new Node(14); |
| 97 | + cout << "Bottom view of the given binary tree :\n" |
| 98 | + bottomView(root); |
| 99 | + return 0; |
| 100 | +} |
0 commit comments