Print a Binary Tree in Vertical Order | Set 2 (Hashmap based Method) | GeeksforGeeks
Given a binary tree, print it vertically.
Read full article from Print a Binary Tree in Vertical Order | Set 2 (Hashmap based Method) | GeeksforGeeks
Given a binary tree, print it vertically.
// Utility function to store vertical order in map 'm'
// 'hd' is horigontal distance of current node from root.
// 'hd' is initally passed as 0
void
getVerticalOrder(Node* root,
int
hd, map<
int
, vector<
int
>> &m)
{
// Base case
if
(root == NULL)
return
;
// Store current node in map 'm'
m[hd].push_back(root->key);
// Store nodes in left subtree
getVerticalOrder(root->left, hd-1, m);
// Store nodes in right subtree
getVerticalOrder(root->right, hd+1, m);
}
// The main function to print vertical oder of a binary tree
// with given root
void
printVerticalOrder(Node* root)
{
// Create a map and store vertical oder in map using
// function getVerticalOrder()
map <
int
,vector<
int
> > m;
int
hd = 0;
getVerticalOrder(root, hd,m);
// Traverse the map and print nodes at every horigontal
// distance (hd)
map<
int
,vector<
int
> > :: iterator it;
for
(it=m.begin(); it!=m.end(); it++)
{
for
(
int
i=0; i<it->second.size(); ++i)
cout << it->second[i] <<
" "
;
cout << endl;
}
}