Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes should be printed from left to right.
Note: A node x is there in output if x is the topmost node at its horizontal distance. Horizontal distance of the left child of a node x is equal to the horizontal distance of x minus 1, and that of right child is the horizontal distance of x plus 1.
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
Output: Top view: 4 2 1 3 7
Input:
1
/ \
2 3
\
4
\
5
\
6
Output: Top view: 2 1 3 6
The idea is to do something similar to Vertical Order Traversal. Like Vertical Order Traversal, we need to group nodes of same horizontal distance together. We do a level order traversal so that the topmost node at a horizontal node is visited before any other node of same horizontal distance below it. A Map is used to map the horizontal distance of the node with the node’s Data and vertical distance of the node.
Below is the implementation of the above approach:
// C++ Program to print Top View of Binary Tree // using hashmap and recursion #include <bits/stdc++.h> using namespace std; // Node structure struct Node { // Data of the node int data; // Horizontal Distance of the node int hd; // Reference to left node struct Node* left; // Reference to right node struct Node* right; }; // Initialising node struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->hd = INT_MAX; node->left = NULL; node->right = NULL; return node; } void printTopViewUtil(Node* root, int height, int hd, map<int, pair<int, int> >& m) { // Base Case if (root == NULL) return; // If the node for particular horizontal distance // is not present in the map, add it. // For top view, we consider the first element // at horizontal distance in level order traversal if (m.find(hd) == m.end()) { m[hd] = make_pair(root->data, height); } else{ pair<int, int> p = (m.find(hd))->second; if (p.second > height) { m.erase(hd); m[hd] = make_pair(root->data, height); } } // Recur for left and right subtree printTopViewUtil(root->left, height + 1, hd - 1, m); printTopViewUtil(root->right, height + 1, hd + 1, m); } void printTopView(Node* root) { // Map to store horizontal distance, // height and node's data map<int, pair<int, int> > m; printTopViewUtil(root, 0, 0, m); // Print the node's value stored by printTopViewUtil() for (map<int, pair<int, int> >::iterator it = m.begin(); it != m.end(); it++) { pair<int, int> p = it->second; cout << p.first << " "; } } int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->right = newNode(4); root->left->right->right = newNode(5); root->left->right->right->right = newNode(6); cout << "Top View : "; printTopView(root); return 0; } |
Top View : 2 1 3 6
Recommended Posts:
- Print Nodes in Top View of Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print Right View of a Binary Tree
- Print Left View of a Binary Tree
- Print all nodes in a binary tree having K leaves
- Print all even nodes of Binary Search Tree
- Print all nodes between two given levels in Binary Tree
- Print all full nodes in a Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print all leaf nodes of a Binary Tree from left to right
- Print nodes between two given level numbers of a binary tree
- Print leftmost and rightmost nodes of a Binary Tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



