1/* This is not the entire code. It's just the function which implements
2 bottom view. You need to write required code. */
3
4// Obj class is used to store node with it's distance from parent.
5class Obj
6{
7 public:
8 Node *root;
9 int dis; // distance from parent node. distance of root node will be 0.
10
11 Obj(Node *node, int dist)
12 {
13 root = node;
14 dis = dist;
15 }
16};
17
18void topView(Node *root)
19{
20 queue<Obj*> q;
21 q.push(new Obj(root, 0));
22 map<int,int> m;
23
24 while(!q.empty())
25 {
26 Obj *ob = q.front();
27 q.pop();
28
29 /* insert node of unique distance from parent node. ignore repitation
30 of distance. */
31 if(m.find(ob->dis) == m.end())
32 m[ob->dis] = ob->root->data;
33
34 if(ob->root->left != NULL)
35 q.push(new Obj(ob->root->left, ob->dis-1));
36 if(ob->root->right != NULL)
37 q.push(new Obj(ob->root->right, ob->dis+1));
38 }
39
40 // printing nodes.
41 for(auto it=m.begin(); it!=m.end(); it++)
42 cout << it->second << "\t";
43
44 cout << endl;
45}