-
Notifications
You must be signed in to change notification settings - Fork 2
/
leftAndRightViewOfBinaryTree.cpp
167 lines (148 loc) · 4.02 KB
/
leftAndRightViewOfBinaryTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
tree -->
50
30 70
20 40 60 80
150
*/
#include "iostream"
using namespace std;
#include "queue"
#include "list"
//Node data structure
struct Node
{
struct Node *right, * left;
int data;
};
//paramter root is having a global scope
//we should avoid using root as local varible in all function
Node *root;
//function to insert element in the tree
Node* insert( Node *temp,int data)
{
//add the node here.
if(temp == NULL)
{
cout<<" insert : "<<data<<endl;
temp = new Node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
//recursive call
if( data > temp->data)
temp->right = insert( temp->right,data);
else
temp->left = insert( temp->left,data);
return temp; //this is require to conncet all nodes
}
//function to display left view of tree
//BFS
void displayLeftViewOfTree(Node* temp)
{
cout<<"\ndisplay the left view of binary tree =>"<<endl;
if(temp != NULL)
{
queue<Node*>q1;
q1.push(temp);
while(!q1.empty())
{
int numOfNodeInCurrentLevel = q1.size();
cout<<q1.front()->data<<endl;
//get all node of next level into the queue
for(int i =0; i< numOfNodeInCurrentLevel; i++)
{
auto tem = q1.front();
q1.pop();
if(tem->left != NULL) q1.push(tem->left);
if(tem->right != NULL) q1.push(tem->right);
}
}
}
}
//function to display top view of tree
void displayTopViewOfTree(Node* temp)
{
queue<Node*>q;
q.push(temp);
list<int>ans;
int rootData= temp->data;
ans.push_back(rootData);
int max = rootData , min= rootData;
while(!q.empty())
{
int size = q.size();
//approch
//maintain min and max, check the each node data and compare it with min max
//and add it in a ans list.
//cout<<"min : "<<min<<" max: "<<max<<" front : "<<q.front()->data<<" back : "<<q.back()->data<<endl;
if(((q.front()->data< min) || (max < q.back()->data)))
{
if(max < q.back()->data)
{
//cout<<" changing max :"<<q.back()->data<<endl;
max = q.back()->data;
ans.push_back(q.back()->data);
}
if( q.front()->data < min)
{
//cout<<" changing min :"<<q.front()->data<<endl;
min = q.front()->data;
ans.push_front(q.front()->data);
}
}
for(int i =0;i<size;i++)
{
Node *d = q.front();
q.pop();
if(d->left != NULL)
q.push(d->left);
if(d->right != NULL)
q.push(d->right);
}
}
cout<<"\n display the top view of binary tree =>"<<endl;
for(auto it: ans)
cout<<it<<" ";
}
//function to display right view of tree
void displayRightViewOfTree(Node* temp)
{
cout<<"\ndisplay the right view of binary tree =>"<<endl;
if(temp != NULL)
{
queue<Node*>q1;
q1.push(temp);
while(!q1.empty())
{
int numOfNodeInCurrentLevel = q1.size();
cout<<q1.back()->data<<endl;
//get all node of next level into the queue
for(int i =0; i< numOfNodeInCurrentLevel; i++)
{
auto tem = q1.front();
q1.pop();
if(tem->left != NULL) q1.push(tem->left);
if(tem->right != NULL) q1.push(tem->right);
}
}
}
}
int main()
{
root =insert(root,50);
insert(root,30);
insert(root,20);
insert(root,40);
insert(root,45);
insert(root,70);
insert(root,60);
insert(root,80);
insert(root,150);
insert(root,120);
displayLeftViewOfTree(root);
displayRightViewOfTree(root);
displayTopViewOfTree(root);
}