Skip to content
Navigation Menu
{{ message }}
This repository was archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathBinary_Tree.cpp
More file actions
402 lines (363 loc) · 8.54 KB
/
Copy pathBinary_Tree.cpp
File metadata and controls
402 lines (363 loc) · 8.54 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
data = d;
left = NULL;
right = NULL;
}
};
node *buildTree()
{
int d;
cin >> d;
if (d == -1)
{
return NULL;
}
node *root = new node(d);
root->left = buildTree();
root->right = buildTree();
return root;
}
void printIn(node *root)
{
if (root == NULL)
{
return;
}
printIn(root->left);
cout << root->data << ",";
printIn(root->right);
}
void printPre(node *root)
{
if (root == NULL)
{
return;
}
cout << root->data << ",";
printPre(root->left);
printPre(root->right);
}
void printPost(node *root)
{
if (root == NULL)
{
return;
}
printPost(root->left);
printPost(root->right);
cout << root->data << ",";
}
int height(node* root){
if(root==NULL){
return 0;
}
return max(height(root->left),height(root->right))+1;
}
void printkthLevel(node* root,int k){
if(root==NULL){
return;
}
if(k==0){
cout << root->data << " ";
return;
}
else{
printkthLevel(root->left,k-1);
printkthLevel(root->right,k-1);
}
}
void printAlllevels(node* root){
int h = height(root);
for(int i=0;i<h ;i++){
printkthLevel(root,i);
cout << endl;
}
}
//TC = O(n)
void bfs(node* root){
queue<node*> q;
q.push(root);
q.push(NULL);
while(!q.empty()){
node* f = q.front();
if(f==NULL){
cout << endl;
q.pop();
if(!q.empty()){
q.push(NULL);
}
}
else{
cout << f->data << " ";
q.pop();
if(f->left){
q.push(f->left);
}
if(f->right){
q.push(f->right);
}
}
}
}
int nodeCount(node* root){
if(root == NULL){
return 0;
}
return nodeCount(root->left)+nodeCount(root->right)+1;
}
int sumofNodes(node* root){
if(root == NULL){
return 0;
}
return sumofNodes(root->left)+sumofNodes(root->right)+root->data;
}
//O(n2) complexity
int diameterTree(node* root){
if(root == NULL){
return 0;
}
int h1 = height(root->left);
int h2 = height(root->right);
int op1 = h1+h2;
int op2 = diameterTree(root->left);
int op3 = diameterTree(root->right);
return max(op1,max(op2,op3));
}
class Pair{
public:
int diameter;
int height;
};
//O(N) Complexity
Pair diameterOptimised(node* root){
Pair p;
if(root == NULL){
p.diameter = p.height = 0;
return p;
}
//Otherwise
Pair leftPair = diameterOptimised(root->left);
Pair rightPair = diameterOptimised(root->right);
p.diameter = max(leftPair.height+rightPair.height, max(leftPair.diameter,rightPair.diameter));
p.height = max(leftPair.height,rightPair.height) + 1;
return p;
}
int sumReplacement(node* root){
if(root == NULL){
return 0;
}
if(root->left == NULL && root->right == NULL){
return root->data;
}
int temp = root->data;
root->data = sumReplacement(root->left) + sumReplacement(root->right);
return root->data + temp;
}
//Unoptimised
bool heightBalanced(node* root){
if(root == NULL){
return true;
}
//Otherwise
int h1 = height(root->left);
int h2 = height(root->right);
if(abs(h1-h2) <= 1 && heightBalanced(root->left) && heightBalanced(root->right)){
return true;
}
else{
return false;
}
}
//Optimised Balanced Tree
class HBPair{
public:
int height;
int balanced;
};
//bottom to top approach - postorder
HBPair heightBalancedOptimised(node* root){
HBPair p;
if(root == NULL){
p.height = 0;
p.balanced = true;
return p;
}
//Recursive Case
HBPair left = heightBalancedOptimised(root->left);
HBPair right = heightBalancedOptimised(root->right);
p.height = max(left.height , right.height) + 1;
if(abs(left.height-right.height) <= 1 and left.balanced and right.balanced){
p.balanced = true;
}
else{
p.balanced = false;
}
return p;
}
node* buildBalancedTree(int *arr, int s, int e){
if(s>e){
return NULL;
}
int mid = (s + e)/2;
node* root = new node(arr[mid]);
root->left = buildBalancedTree(arr,s,mid-1);
root->right = buildBalancedTree(arr,mid+1,e);
return root;
}
//Algo for building a tree from preorder+inorder
//1. iterate preorder and pick one item, create a node
//2. search that item's index in inorder, let the index is i.
//3. make a recursive call on arr(0,i-1) and arr(i+1,e) and attach to the left and right subtree of previous node.
node* createtreefromTrav(int *in, int *pre, int s, int e){
static int i =0;
//Base case
if(s>e){
return NULL;
}
//recursive case
node* root = new node(pre[i]);
int index = -1;
for(int j =s ; j<=e ; j++){
if(pre[i] == in[j]){
index = j;
break;
}
}
i++;
root->left = createtreefromTrav(in,pre,s,index-1);
root->right = createtreefromTrav(in,pre,index+1,e);
return root;
}
void rightview(node* root, int level, int &maxlevel){
if(root == NULL){
return;
}
if(level > maxlevel){
//found new level so printing it's data
cout << root->data << ",";
maxlevel = level;
}
rightview(root->right, level+1, maxlevel);
rightview(root->left,level+1, maxlevel);
}
int printAtDistanceK(node* root, node* target, int k){
if(root == NULL){
return -1;
}
//reach the target node
if(root == target){
printkthLevel(target,k);
return 0;
}
//ancestor
int DL = printAtDistanceK(root->left,target,k);
if(DL != -1){
//Again 2 cases
//Ancestor itself or you need to go to right subtree
if(DL +1 == k){
cout << root->data << " ";
}
else{
printkthLevel(root->right,k-2-DL);
}
return 1+DL;
}
int DR = printAtDistanceK(root->right,target,k);
if(DR != -1){
if(DR+1 == k){
cout << root->data << " ";
}
else{
printkthLevel(root->left,k-2-DR);
}
return 1+DR;
}
//node was not present in left and right subtree of given node
return -1;
}
//assuming both a and b is present in tree , all keys are unique
node* lca(node* root, int a,int b){
if(root == NULL){
return NULL;
}
if(root->data == a or root->data == b){
return root;
}
//searching in left and right subtrees
node* leftans = lca(root->left,a,b);
node* rightans = lca(root->right,a,b);
if(leftans != NULL and rightans != NULL){
return root;
}
else if(leftans!=NULL){
return leftans;
}
return rightans;
}
node* deletion(node* root,int key){
if(root == NULL){
return NULL;
}
if(root->data == key){
return NULL;
}
root->left = deletion(root->left,key);
root->right = deletion(root->right,key);
return root;
}
int main()
{
//Sample Input -> 8 10 1 -1 -1 6 9 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1
node *root = buildTree();
// printPre(root);
// cout << endl;
// printIn(root);
// cout << endl;
// printPost(root);
// cout << endl;
// cout << height(root);
// cout << endl;
// printAlllevels(root);
bfs(root);
// cout << nodeCount(root) << "\n";
// cout << sumofNodes(root) << "\n";
// cout << diameterTree(root) << endl;
// Pair p = diameterOptimised(root);
// cout << "Daimeter : " << p.diameter << endl;
// cout << "Height : "<< p.height << endl;
// sumReplacement(root);
// bfs(root);
// HBPair p = heightBalancedOptimised(root);
// if(p.balanced){
// cout << "TREE IS BALANCED!" << endl;
// }
// else{
// cout << "TREE UNBALANCED!" << endl;
// }
// int arr[7] = {1,2,3,4,5,6,7};
// node* newroot = buildBalancedTree(arr,0,6);
// bfs(newroot);
// int pre[9] = {8,10,1,6,9,7,3,14,13};
// int in[9] = {1,10,9,6,7,8,3,13,14};
// node* newroot = createtreefromTrav(in,pre,0,8);
// bfs(newroot);
// int maxlevel = -1;
// rightview(root,0,maxlevel);
// node* target = root->left;
// int k=2;
// printAtDistanceK(root,target,k);
// node* ans = lca(root,10,9);
// cout << ans->data << endl;
root = deletion(root,6);
bfs(root);
return 0;
}
You can’t perform that action at this time.
