forked from sinagarajan/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.cpp
More file actions
483 lines (426 loc) · 9.58 KB
/
binarytree.cpp
File metadata and controls
483 lines (426 loc) · 9.58 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "binarytree.h"
/* Creation of new Node */
struct tree * createNode(int data)
{
struct tree *newNode= (struct tree *)malloc(sizeof(struct tree));
newNode->data=data;
newNode->right=newNode->left=NULL;
return newNode;
}
/* Insert an element in the Binary Search tree */
struct tree * insertNodeBST(struct tree* root,int data)
{
if(root==NULL)
return createNode(data);
else
{
if(data<=root->data)
root->left=insertNodeBST(root->left,data);
else
root->right=insertNodeBST(root->right,data);
}
return root;
}
/* Insert an element in the Binary Tree */
struct tree * insertNodeBT(struct tree* root,int data)
{
queue<struct tree *> q;
struct tree *ptr=createNode(data);
struct tree *itr;
if(root==NULL)
{
root=ptr;
}
else
{
q.push(root);
while(!q.empty())
{
itr=q.front();
q.pop();
if(itr->left==NULL)
{
itr->left=ptr;
break;
}
else if(itr->right==NULL)
{
itr->right=ptr;
break;
}
else
{
q.push(itr->left);
q.push(itr->right);
}
}
/* Queue does not have standard clear */
while(!q.empty()){q.pop();}
}
return root;
}
/* Inorder Traversal */
void printInorderRecursive(struct tree *root)
{
if(root==NULL)
return;
else
{
printInorderRecursive(root->left);
printf("\t%d",root->data);
printInorderRecursive(root->right);
}
}
/* Pre order and Post Order Traversal */
void printPreorderRecursive(struct tree *root)
{
if(root==NULL)
return;
else
{
printf("\t%d",root->data);
printPreorderRecursive(root->left);
printPreorderRecursive(root->right);
}
}
void printPostorderRecursive(struct tree *root)
{
if(root==NULL)
return;
else
{
printPostorderRecursive(root->left);
printPostorderRecursive(root->right);
printf("\t%d",root->data);
}
}
void printLevelorder(struct tree *root)
{
queue<struct tree*> q;
struct tree *ptr;
if(root==NULL)
return;
q.push(root);
while(!q.empty())
{
ptr=q.front();
q.pop();
printf("\t%d",ptr->data);
if(ptr->left != NULL)q.push(ptr->left);
if(ptr->right != NULL)q.push(ptr->right);
}
}
/* Count number of nodes in a Binary tree */
int countNode(struct tree *root)
{
if(root==NULL)
return 0;
else
{
return countNode(root->left)+countNode(root->right)+1;
}
}
/* Find maximum element in binary tree */
int findMaxBT(struct tree *root)
{
int a,b;
if(root==NULL)
return 0;
else
{
a=findMaxBT(root->left);
b=findMaxBT(root->right);
return std::max(root->data,std::max(a,b));
}
}
/* Find minimum element in binary tree */
int findMinBT(struct tree *root)
{
int a,b;
if(root==NULL)
return 32767;
else
{
a=findMinBT(root->left);
b=findMinBT(root->right);
return std::min(root->data,std::min(a,b));
}
}
/* Find if tree is BST or not using Post order */
int findIsBST(struct tree *root)
{
int a,b,c;
if(root==NULL)
return 1;
else
{
a=findIsBST(root->left);
b=findIsBST(root->right);
c= a&& b;
if(c==0 || root->data < findMaxBT(root->left) || (root->data > findMinBT(root->right)))
return 0;
else
return 1;
}
}
/* Is BST by passing min and max to leaves */
int findIsBST1(struct tree *root,int min,int max)
{
if(root==NULL)
return 1;
if((root->data < min) || (root->data > max))
return 0;
if(!findIsBST1(root->left,min,root->data) || !findIsBST1(root->right,root->data,max))
return 0;
return 1;
}
/* Print tree in reverse order */
void printReverse(struct tree *root)
{
stack<struct tree*> s;
queue<struct tree*> q;
struct tree *itr;
if(root==NULL)return;
q.push(root);
while(!q.empty())
{
itr=q.front();
q.pop();
s.push(itr);
if(itr->left!=NULL ) q.push(itr->left);
if(itr->right!=NULL) q.push(itr->right);
}
while(!s.empty())
{
itr=s.top();
s.pop();
printf("\t%d",itr->data);
}
}
/* Print in zig zag way */
void printZigZag(struct tree *root)
{
deque<struct tree*> dq;
struct tree *itr;
int current,next,flag;
if(root==NULL)return;
dq.push_front(root);
current=1;flag=0;next=0;
while(1)
{
while(current >0 )
{
if(flag)
{
itr=dq.back();
dq.pop_back();
printf("\t%d",itr->data);
if(itr->left != NULL){ dq.push_front(itr->left);next++;}
if(itr->right != NULL){ dq.push_front(itr->right);next++;}
}
else
{
itr=dq.front();
dq.pop_front();
printf("\t%d",itr->data);
if(itr->right != NULL){ dq.push_back(itr->right);next++;}
if(itr->left != NULL){ dq.push_back(itr->left);next++;}
}
current--;
}
++flag;
flag%=2;
// Break when it is last level
if(next==0) break;
current=next;
next=0;
}
}
/* Print the tree in zig zag way using two stacks */
void printZigZagStack(struct tree *root)
{
stack<struct tree *> current,next,temp;
struct tree *itr;
int flag=1;
if(root==NULL) return;
current.push(root);
while(!current.empty())
{
itr=current.top();
current.pop();
printf("\t%d",itr->data);
if(flag)
{
if(itr->left != NULL) next.push(itr->left);
if(itr->right!=NULL) next.push(itr->right);
}
else
{
if(itr->right != NULL) next.push(itr->right);
if(itr->left!=NULL) next.push(itr->left);
}
if(current.empty())
{
current=next;
next=temp;
flag++;
flag%=2;
}
}
}
/* Find Lowest Common Ancestor */
struct tree *findLCA(struct tree * root, int a, int b)
{
if(root==NULL) return NULL;
if(root->data==a || root->data==b)return root;
struct tree *left=findLCA(root->left,a,b);
struct tree *right=findLCA(root->right,a,b);
if( left && right) return root;
return left?left:right;
}
/* Maximum height */
int printMaxHeight(struct tree *root)
{
int left,right;
if(root==NULL) return 0;
left=printMaxHeight(root->left);
right=printMaxHeight(root->right);
return (std::max(left,right)+1);
}
/* Print All Paths */
void printArray(int paths[],int pathLen)
{
for(int i=0;i<pathLen;i++)
printf("\t%d",paths[i]);
printf("\n");
}
void printAll(struct tree *root,int path[],int pathLen)
{
if(root==NULL) return;
path[pathLen]=root->data;
pathLen++;
if(root->left == NULL && root->right==NULL)
printArray(path,pathLen);
else
{
printAll(root->left,path,pathLen);
printAll(root->right,path,pathLen);
}
}
void printAllPaths(struct tree *root)
{
int paths[1000];
printAll(root,paths,0);
}
int printAncestors(struct tree *root,int a)
{
if(root==NULL) return 0;
if(root->data==a || printAncestors(root->left,a) || printAncestors(root->right,a))
printf("\t%d",root->data);
return 1;
}
/* Diamter of the tree */
int findDiameter(struct tree *root)
{
int lheight,rheight,ldia,rdia;
if(root==NULL) return 0;
lheight = printMaxHeight(root->left);
rheight = printMaxHeight(root->right);
ldia=findDiameter(root->left);
rdia=findDiameter(root->right);
return std::max(lheight+rheight+1,std::max(ldia,rdia));
}
/* Sorted array using Binary Search Tree principle */
struct tree * convertSortedArraytoBST(int array[],int start,int end)
{
int mid= (start + end)/2;
struct tree *root;
if(start > end) return NULL;
root=createNode(array[mid]);
root->left=convertSortedArraytoBST(array,start,mid-1);
root->right=convertSortedArraytoBST(array,mid+1,end);
return root;
}
int findLargestBSTSubtree(struct tree *p, int &min, int &max,
int &maxNodes, struct tree *& largestBST) {
if (!p) return 0;
bool isBST = true;
int leftNodes = findLargestBSTSubtree(p->left, min, max, maxNodes, largestBST);
int currMin = (leftNodes == 0) ? p->data : min;
if (leftNodes == -1 ||
(leftNodes != 0 && p->data <= max))
isBST = false;
int rightNodes = findLargestBSTSubtree(p->right, min, max, maxNodes, largestBST);
int currMax = (rightNodes == 0) ? p->data : max;
if (rightNodes == -1 ||
(rightNodes != 0 && p->data >= min))
isBST = false;
printf("\nLeft Nodes %d",leftNodes);
printf("\nRight Nodes %d",rightNodes);
printf("\n%d is BST",isBST);
printf("\nMAX: %d",max);
printf("\nMIN: %d",min);
printf("\np->data %d",p->data);
printf("\nCMIN: %d",currMin);
printf("\nCMAX: %d",currMax);
if (isBST) {
min = currMin;
max = currMax;
int totalNodes = leftNodes + rightNodes + 1;
if (totalNodes > maxNodes) {
maxNodes = totalNodes;
largestBST = p;
}
return totalNodes;
} else {
return -1; // This subtree is not a BST
}
}
struct tree* findLargestBSTSubtree(struct tree *root) {
struct tree *largestBST = NULL;
int min, max;
int maxNodes = INT_MIN;
findLargestBSTSubtree(root, min, max, maxNodes, largestBST);
return largestBST;
}
/* Find the nodes along the circumference of the tree . Select first and last nodes in each level followed by choosing the leaf nodes at each level */
void findCircumference(struct tree *root)
{
int currLevel = 0, nextLevel =0 , count = 0;
struct tree *itr;
stack<struct tree *> s1;
stack<struct tree *> s2;
stack<struct tree *> temp;
if(root == NULL)
return ;
s1.push(root);
++currLevel;
while(!s1.empty())
{
itr= s1.top();
s1.pop();
//Concept of same two stack used for zig zag traversal
if(itr->left !=NULL)
{
s2.push(itr->left);
++nextLevel;
}
if(itr->right != NULL)
{
s2.push(itr->right);
++nextLevel;
}
//Check for node at the circumference
if((itr->left == NULL && itr->right == NULL) || (count == 0) || (count == currLevel-1))
printf("\t%d", itr->data);
++count;
if(s1.empty())
{
s1 = s2;
s2 = temp;
count=0;
currLevel = nextLevel;
nextLevel = 0;
}
}
}