1
+ /*
2
+ Top view of binary tree
3
+ Given a binary tree, print the nodes in left to right manner as visible from above the tree.
4
+ Input is given in order of root left child then right child.
5
+ For e.g. 1 2 3 4 5 6 -1 -1 -1 -1 -1 -1 -1
6
+ Tree looks like
7
+ 1
8
+ / \
9
+ 2 3
10
+ / \ /
11
+ 4 5 6
12
+
13
+ nodes '5' and '6' will be overlapped by '1' as seen from above,
14
+ so when viewed from the top , we would see the nodes 4, 2, 1 and 3.
15
+ */
16
+
17
+ #include < bits/stdc++.h>
18
+ using namespace std ;
19
+
20
+ // class for defining the the node, left and right child pointer along
21
+ // with a constructor for assigning the "key" to the node.
22
+ class node
23
+ {
24
+ public:
25
+ int val;
26
+ node *left;
27
+ node *right;
28
+
29
+ node (int d)
30
+ {
31
+ val = d;
32
+ left = NULL , right = NULL ;
33
+ }
34
+ };
35
+
36
+ // function for taking the input from user and assigning the node values accordingly.
37
+ // Queue has been used to assign the "keys" to left and right child alternatively
38
+ node *maketree ()
39
+ {
40
+ int d;
41
+ queue<node *> q;
42
+ cout << " Enter the root value: " ;
43
+ cin >> d;
44
+ node *root = new node (d);
45
+ q.push (root);
46
+ while (!q.empty ())
47
+ {
48
+ node *temp = q.front ();
49
+ q.pop ();
50
+ int l, r;
51
+ cout << " \n Enter the left and right childs : " ;
52
+ cin >> l >> r;
53
+ if (l != -1 )
54
+ {
55
+ // assigning value to the left child pointer and inserting is as the parent node to repeat the process
56
+ temp->left = new node (l);
57
+ q.push (temp->left );
58
+ }
59
+ if (r != -1 )
60
+ {
61
+ // assigning value to the right child pointer and inserting it as the parent node to repeat the process
62
+ temp->right = new node (r);
63
+ q.push (temp->right );
64
+ }
65
+ }
66
+ cout << " \n " ;
67
+ return root;
68
+ }
69
+
70
+ // The Concept is combining the left side view and right side view of that tree, will be
71
+ // the in total, the top view as all overlapping will be overcomed.
72
+
73
+ /* Now for printing the tree, firstly the left child are printed recursively.
74
+ --> 1
75
+ / \
76
+ --> 2 3
77
+ / \ /
78
+ --> 4 5 6
79
+ so the nodes 4, 2 and 1 are printed respectively
80
+ */
81
+ void printleft (node *root)
82
+ {
83
+ if (root == NULL )
84
+ return ;
85
+ printleft (root->left );
86
+ cout << root->val << " " ;
87
+ return ;
88
+ }
89
+
90
+ /* Now for printing the tree, secondly the right child are printed recursively.
91
+ 1
92
+ / \
93
+ 2 3 <--
94
+ / \ /
95
+ 4 5 6
96
+ so the node 3 is printed respectively.
97
+ */
98
+ void printright (node *root)
99
+ {
100
+ if (root == NULL )
101
+ return ;
102
+ cout << root->val << " " ;
103
+ printright (root->right );
104
+ return ;
105
+ }
106
+
107
+ int main ()
108
+ {
109
+ // calling the function for making the binary tree
110
+ node *root = maketree ();
111
+ // calling the printleft and printright function for printing nodes and overall, the top view
112
+ cout << " Top view: " ;
113
+ printleft (root);
114
+ printright (root->right );
115
+ return 0 ;
116
+ }
117
+
118
+ /*
119
+ Sample Input:
120
+ Enter the root value: 1
121
+ Enter the left and right childs : 2 3
122
+ Enter the left and right childs : 4 5
123
+ Enter the left and right childs : 6 -1
124
+ Enter the left and right childs : -1 -1
125
+ Enter the left and right childs : -1 -1
126
+ Enter the left and right childs : -1 -1
127
+ Sample Output:
128
+ Top view: 4 2 1 3
129
+
130
+ Time Complexity: O(n), for traversing the nodes
131
+ Space Complexity: O(n), since queue has been used for n nodes
132
+ */
0 commit comments