#include <bits/stdc++.h>
using
namespace
std;
struct
Node {
int
data;
int
hd;
struct
Node* left;
struct
Node* right;
};
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)
{
if
(root == NULL)
return
;
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);
}
}
printTopViewUtil(root->left, height + 1, hd - 1, m);
printTopViewUtil(root->right, height + 1, hd + 1, m);
}
void
printTopView(Node* root)
{
map<
int
, pair<
int
,
int
> > m;
printTopViewUtil(root, 0, 0, m);
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;
}