Repository navigation
Expand file tree
/
Copy pathTree_all_operations_functions_binary.cpp
More file actions
338 lines (278 loc) · 7.97 KB
/
Copy pathTree_all_operations_functions_binary.cpp
File metadata and controls
338 lines (278 loc) · 7.97 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
/*
Function -------> Logic
1) build tree -------> new node creation by recursive calls
2) level order traversal -------> Queue
3) inorder recursive -------> Recursion
4) inorder iterative -------> Stack
5) preorder recursive -------> Recursion
6) postorder recursive -------> Recursion
7) height O(n2) -------> Recursive calls
8) height O(n) -------> level order traversal
9) diameter O(n^2) -------> Dia left -- Dia Right -- height(root)
10) diameter O(n) -------> using pair to store height (avoid recursive calls)
11) check balanced O(n^2) -------> condition = | H (left) - H (right) | <= 1
12) check balanced O(n) -------> using pair to store height
13) identical tree -------> inorder traversal & checking data elements
14) sum tree -------> root -> data = left(subtree) + right(subtree)
*/
class node{
public:
int data;
node* right;
node* left;
node(int d){
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
// 1) Building Tree
node* build_tree(node* root){
int d;
cout<<"Enter value : ";
cin>>d;
if(d==-1){return NULL;}
root = new node(d);
cout<<"For left of "<<d<<endl;
root->left = build_tree(root->left);
cout<<"For right of "<<d<<endl;
root->right = build_tree(root->right);
return root;
}
// 2) Level order traversal
void level_order_traversal(node* root){
queue<node*> q;
q.push(root);
q.push(NULL);
while(!q.empty()){
node* temp = q.front();
q.pop();
if(temp==NULL and q.size()==0){return;}
else if(temp == NULL and q.size()>0)
{
q.push(NULL);
cout<<endl;
}
else{
cout<<temp->data<<" ";
if(temp->left){q.push(temp->left);}
if(temp->right){q.push(temp->right);}
}
}
}
// 3) Inorder Traversal ===== left root right
void inorder(node* root){
if(root==NULL){return;}
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
// 4) Inorder (Iterative)
void inorder_itr(node* root){
stack<node*> st;
node* temp = root;
while(true){ // -----> Striver's Logic
if(temp!=NULL){
st.push(temp);
temp = temp->left;
}
else{
if(st.empty()){break;}
temp = st.top();
st.pop();
cout<<temp->data<<" ";
temp = temp->right;
}
}
}
// 5) preorder (Recursive) -----> root left right
void preorder(node* root){
if(root==NULL){return;}
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
// 6) postorder (Recursive) -----> left right root
void postorder(node* root){
if(root==NULL){return;}
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
// 7) Height of Binary Tree O(n)
int height(node* root){
if(root==NULL){return 0;}
int left = height(root->left);
int right = height(root->right);
int ans = max(left,right)+1;
return ans;
}
// 8) Height in O(n)
int fast_height(node* root){
queue<node*> q;
q.push(root);
q.push(NULL);
int count = 1;
while(!q.empty()){
node* temp = q.front();
q.pop();
if(temp==NULL and q.size()==0){return count;}
else if(temp==NULL and q.size()>0){
q.push(NULL);
count++;
}
else{
if(temp->left){q.push(temp->left);}
if(temp->right){q.push(temp->right);}
}
}
//return count;
}
// 9) Diameter O(n^2)
int diameter(node* root){
if(root==NULL){
return 0;
}
int op1 = diameter(root->left);
int op2 = diameter(root->right);
int op3 = height(root->left)+height(root->right)+1;
int ans = max(op1,max(op2,op3));
return ans;
}
// 10) Diameter O(n)
pair<int,int> fast_diameter(node* root){
if(root==NULL){
pair<int,int> p = make_pair(0,0);
return p;
}
pair<int,int> left = fast_diameter(root->left);
pair<int,int> right = fast_diameter(root->right);
int op1 = left.first;
int op2 = right.first;
int op3 = left.second+right.second+1;
pair<int,int> ans;
ans.first = max(op1,max(op2,op3));
ans.second = max(left.second,right.second)+1;
return ans;
}
// 11) Balanced Tree O(n^2)
bool balanced(node* root){
if(root==NULL){
return true;
}
bool left = balanced(root->left);
bool right = balanced(root->right);
bool condition = abs(height(root->left)-height(root->right))<=1;
if(left and right and condition){
return true;
}
else{
return false;
}
}
// 12) Balanced O(n)
pair<bool,int> fast_balanced(node* root){
if(root==NULL){
pair<bool,int> p = make_pair(true,0);
return p;
}
pair<bool,int> left = fast_balanced(root->left);
pair<bool,int> right = fast_balanced(root->right);
bool leftAns = left.first;
bool rightAns = right.first;
bool condition = abs(left.second - right.second) <= 1;
pair<bool,int> ans;
if(leftAns and rightAns and condition){
ans.first = true;
ans.second = max(left.second,right.second)+1;
}
else{
ans.first = false;
}
return ans;
}
// 13) Identical Trees
bool inorder(node* r1,node* r2){
if(r1==NULL and r2==NULL){
return true;
}
if(r1==NULL and r2!=NULL){
return false;
}
if(r2==NULL and r1!=NULL){
return false;
}
bool left = inorder(r1->left,r2->left);
bool right = inorder(r1->right,r2->right);
bool ans;
if(left and right and r1->data==r2->data){
ans = true;
}
else{
ans = false;
}
return ans;
}
// 14) Sum tree = O(n)
pair<bool,int> sum_tree(node* root){
if(root == NULL){
pair<bool,int> p =make_pair(true,0);
return p;
}
if(root->left==NULL and root->right==NULL){
pair<bool,int> x = make_pair(true,root->data);
return x;
}
pair<bool,int> left = sum_tree(root->left);
pair<bool,int> right = sum_tree(root->right);
bool leftAns = left.first;
bool rightAns = right.first;
bool condition = root->data == left.second+right.second;
pair<bool,int> ans;
if(leftAns and rightAns and condition){
ans.first = true;
ans.second = root->data+left.second+right.second;
}
else{
ans.first = false;
}
return ans;
}
int main(){
/*INPUT testcase
1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1
*/
node* root = NULL;
root = build_tree(root);
cout<<" "<<endl;
cout<<"\nLevel Order Traversal "<<endl;
level_order_traversal(root);
cout<<" "<<endl;
cout<<"\nInorder (Recursive) -> ";
inorder(root);
cout<<" "<<endl;
cout<<"\nInorder (Iterative) -> ";
inorder_itr(root);
cout<<" "<<endl;
cout<<"\nPreorder (Recursive) -> ";
preorder(root);
cout<<" "<<endl;
cout<<"\nPostorder (Recursive) -> ";
postorder(root);
cout<<" "<<endl;
cout<<"\nHeight O(n^2) -> "<<height(root);
cout<<" "<<endl;
cout<<"\nHeight O(n) -> "<<fast_height(root);
cout<<" "<<endl;
cout<<"\nDiameter O(n^2) -> "<<diameter(root);
cout<<" "<<endl;
cout<<"\nDiameter O(n) -> "<<fast_diameter(root).first;
cout<<" "<<endl;
cout<<"\nBalanced O(n^2) -> "<<balanced(root);
cout<<" "<<endl;
cout<<"\nBalanced O(n) -> "<<fast_balanced(root).first;
}