-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinaryTrees.cpp
More file actions
834 lines (712 loc) · 19.4 KB
/
binaryTrees.cpp
File metadata and controls
834 lines (712 loc) · 19.4 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
// main.cpp
// binary_trees
//
// Created by srinija on 10/2/14.
//
#include <iostream>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
int value;
node* left;
node* right;
};
node* newNode(int n);
void pre_order(node* head);
void iterative_pre(node* ptr);
void in_order(node* head);
void level_order(node* ptr);
int get_size(node* head);
void print_leaf_paths(node* ptr);
void print_paths(node* ptr, int* path, int index);
void root_leaf_sum(node* ptr,int num);
bool root_leaf_sum(node* ptr,int* sum,int num);
void level_order_spiral(node* ptr);
bool check_children(node* ptr);
void convert_children_sum(node* ptr);
void increment(node* ptr,int diff);
int diameter(node* ptr);
int get_height(node* ptr);
bool check_balance(node* ptr);
int check_balance(node* ptr, bool* check);
node* inorder_and_preorder(int* in,int size,int* pre);
void inorder_and_preorder(int* in, int size, int* pre, int* pre_index, node** ptr);
void double_tree(node* ptr);
bool check_fold(node* head);
bool check_fold(node* ptr1,node* ptr2);
bool check_sum_tree(node* root);
int check_sum_tree(node* root, bool* check);
int convert_sum_tree(node* head);
node* pre_and_post(int* pre,int* post, int size);
bool check_binary(node* root);
bool check_binary(node* root, int min, int max);
int largest_bst(node* head);
int largest_bst(node* root, int low, int high,int* size);
node* sorted_array_bst(int* arr, int high,int low);
node* tree_DLL(node* head);
void tree_DLL(node** ptr,node* current);
void printList(struct node *root);
int subarray_sum(int* arr, int n,int req_sum);
void two_d_spiral(int** A,int m, int n );
int main(int argc, const char * argv[])
{
int m=6,n=3;
int** A=new int*[n];
for(int i=0;i<n;i++)
A[i]=new int[m];
cout<<"Enter the matrix!"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cin>>A[i][j];
}
two_d_spiral(A,m,n);
int sum_arr[]={1, 11, 100, 1, 0, 200, 3, 2, 1, 250};
cout<<"Subarray sum prob: "<<subarray_sum(sum_arr, sizeof(sum_arr)/sizeof(sum_arr[0]), 280)<<endl;
node* head1=NULL;
head1=newNode(1);
head1->left = newNode(2);
head1->right = newNode(3);
head1->left->left = newNode(7);
head1->left->right = newNode(6);
head1->right->left = newNode(5);
head1->right->right = newNode(4);
node* head11=NULL;
head11=newNode(1);
head11->left = newNode(2);
head11->right = newNode(3);
head11->left->left = newNode(7);
head11->left->right = newNode(6);
head11->right->left = newNode(5);
head11->right->right = newNode(4);
node* head_DLL=tree_DLL(head11);
printList(head_DLL);
cout<<endl;
iterative_pre(head1);
cout<<"Checking foldability: "<<check_fold(head1)<<endl;
double_tree(head1);
level_order(head1);
node* head2 = newNode(20);
head2->left = newNode(8);
head2->right = newNode(2);
head2->left->left = newNode(3);
head2->left->right = newNode(5);
head2->right->right = newNode(2);
cout<<"Checking sum tree: "<<check_sum_tree(head2)<<endl;
node* head3 = newNode(50);
head3->left = newNode(7);
head3->right = newNode(2);
head3->left->left = newNode(3);
head3->left->right = newNode(5);
head3->right->left = newNode(1);
head3->right->right = newNode(30);
/* following binary tree is
1
/ \
2 3
/ \ /
4 5 6
/
7 */
node* head4=NULL;
head4 = newNode(1);
head4->left = newNode(2);
head4->right = newNode(3);
head4->left->left = newNode(4);
head4->left->right = newNode(5);
head4->right->left = newNode(6);
head4->left->left->left = newNode(7);
/* Constructed binary tree is
1
/ \
2 3
/ \
4 5
/
6
*/
node* head5 = newNode(1);
head5->left = newNode(2);
head5->right = newNode(3);
head5->left->left = newNode(4);
head5->left->right = newNode(5);
head5->left->left->left=newNode(6);
/*cout<<"Pre_order Traversal: ";
pre_order(head);
cout<<endl;
cout<<"In_order Traversal: ";
in_order(head); //Non recursive version
cout<<endl;
cout<<"Level Order Traversal: ";
level_order(head);
cout<<endl;
cout<<"Size of the tree is "<<get_size(head)<<endl;*/
cout<<"Printing Paths: ";
print_leaf_paths(head4);
root_leaf_sum(head4,12);
cout<<"Level Order Traversal in spiral form: ";
level_order_spiral(head1);
cout<<endl;
cout<<"Diameter of the tree is: "<<diameter(head1)<<endl;
cout<<"Checking Children sum property: "<<check_children(head1)<<endl;
cout<<"Converting..."<<endl;
convert_children_sum(head1);
cout<<"Checking Children sum property: "<<check_children(head1)<<endl;
cout<<"Checking balance: "<<check_balance(head4)<<endl;
int in[] = {4,2,5,1,3,6};
int pre[] = {1,2,4,5,3,6};
int len = sizeof(in)/sizeof(in[0]);
node* result=inorder_and_preorder(in, len, pre);
cout<<"Printing inorder: ";
in_order(result);
node* root = newNode(10);
root->left = newNode(-2);
root->right = newNode(6);
root->left->left = newNode(8);
root->left->right = newNode(-4);
root->right->left = newNode(7);
root->right->right = newNode(5);
convert_sum_tree(root);
cout<<"\n Printing level order: "<<endl;
level_order(root);
int pre1[] = {1, 2, 4, 8, 9, 5, 3, 6, 7};
int post1[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
int size1 = sizeof( pre1 ) / sizeof( pre1[0] );
node *root2 = pre_and_post(pre1, post1, size1);
cout<<"Printing in order: "<<endl;
in_order(root2);
node *root1 = newNode(50);
root1->left = newNode(10);
root1->right = newNode(60);
root1->left->left = newNode(5);
root1->left->right = newNode(20);
root1->right->left = newNode(55);
root1->right->left->left = newNode(45);
root1->right->right = newNode(70);
root1->right->right->left = newNode(65);
root1->right->right->right = newNode(80);
cout<<"Largest Binary search tree is: "<<largest_bst(root1)<<endl; //wrong!!
int sorted[]={1, 2, 3, 4, 5, 6, 7};
node* root3=sorted_array_bst(sorted, sizeof(sorted)/sizeof(sorted[0]), 0);
in_order(root3);
}
node* newNode(int n)
{
node* temp=new node;
temp->value=n;
temp->left=NULL;
temp->right=NULL;
return temp;
}
//pre order traversal
void pre_order(node* head) //<root><left><right>
{
if(head==NULL)
return;
cout<<head->value<<" ";
pre_order(head->left);
pre_order(head->right);
}
void iterative_pre(node* root)
{
if (root == NULL)
return;
// Create an empty stack and push root to it
stack<node *> nodeStack;
nodeStack.push(root);
/* Pop all items one by one. Do following for every popped item
a) print it
b) push its right child
c) push its left child
Note that right child is pushed first so that left is processed first */
while (nodeStack.empty() == false)
{
// Pop the top item from stack and print it
struct node *node = nodeStack.top();
printf ("%d ", node->value);
nodeStack.pop();
// Push right and left children of the popped node to stack
if (node->right)
nodeStack.push(node->right);
if (node->left)
nodeStack.push(node->left);
}
}
void in_order(node* ptr) //<left> <root> <right> //Non recursive version
{
stack<node*> stk;
while(!stk.empty()||ptr!=NULL)
{
while(ptr!=NULL)
{
stk.push(ptr);
ptr=ptr->left;
}
ptr=stk.top();
stk.pop();
cout<<ptr->value<<" ";
ptr=ptr->right;
}
}
void level_order(node* ptr)
{
queue<node*> q;
q.push(ptr);
while(!q.empty())
{
ptr=q.front();
q.pop();
cout<<ptr->value<<" ";
if(ptr->left) q.push(ptr->left);
if(ptr->right)q.push(ptr->right);
}
}
int get_size(node* head) //Size=Number of elements present in the tree
{
if(head==NULL)
return 0;
return 1+get_size(head->left)+get_size(head->right);
}
void print_leaf_paths(node* ptr) //Do it urself first!! Awesome :)
{
int size=get_size(ptr);
int path[size];
int index=0;
print_paths(ptr,path,index);
}
void print_paths(node* ptr, int* path, int index) //Utitlity function for above
{
if(ptr==NULL)
return;
if(ptr!=NULL)
{
path[index]=ptr->value;
index++;
}
if(ptr->left==NULL&&ptr->right==NULL)
{
for(int i=0;i<index;i++)
cout<<path[i]<<" ";
cout<<endl;
return;
}
print_paths(ptr->left,path,index);
print_paths(ptr->right,path, index);
}
void level_order_spiral(node* ptr)
{
int level=0;
int num;
queue<node*> q;
stack<int> stk;
q.push(ptr);
q.push(NULL);
while(!q.empty()&&q.front()!=NULL)
{
level++;
while(q.front()!=NULL)
{
ptr=q.front();
q.pop();
if(ptr->left) q.push(ptr->left);
if(ptr->right) q.push(ptr->right);
if(level%2==0)
cout<<ptr->value<<" ";
else{
stk.push(ptr->value);
}
}
q.push(NULL);
q.pop();
if(!stk.empty())
{
while(!stk.empty()){
num=stk.top();
cout<<num<<" ";
stk.pop();
}
}
}
}
bool check_children(node* ptr)
{
stack<node*> stk;
stk.push(ptr);
int child1,child2;
while(!stk.empty())
{
ptr=stk.top();
stk.pop();
if(ptr->left||ptr->right)
{
if(ptr->left){
child1=ptr->left->value;
stk.push(ptr->left);
}
else child1=0;
if(ptr->right){
child2=ptr->right->value;
stk.push(ptr->right);
}
else child2=0;
if(ptr->value!=child1+child2)
return false;
}
}
return true;
}
void convert_children_sum(node* ptr)
{
if(ptr==NULL||(ptr->left==NULL&&ptr->right==NULL))
return;
convert_children_sum(ptr->left);
convert_children_sum(ptr->right);
int child_left=0, child_right=0;
if(ptr->left)
child_left=ptr->left->value;
if(ptr->right)
child_right=ptr->right->value;
int diff=ptr->value-child_left-child_right;
if(diff<0)
ptr->value-=diff;
else if(diff>0)
increment(ptr,diff);
}
void increment(node* ptr,int diff)
{
if(ptr->left==NULL&&ptr->right==NULL)
return;
if(ptr->left){
ptr->left->value+=diff;
increment(ptr->left,diff);
}
else{
ptr->right->value+=diff;
increment(ptr->right,diff);
}
}
int diameter(node* ptr)
{
if(ptr==NULL)
return 0;
return max(max(diameter(ptr->left),diameter(ptr->right)),get_height(ptr->left)+get_height(ptr->right)+1);
}
int get_height(node* ptr)
{
if(ptr==NULL)
return 0;
return max(get_height(ptr->left),get_height(ptr->right))+1;
}
bool check_balance(node* ptr)
{
bool check=true;
check_balance(ptr,&check);
return check;
}
int check_balance(node* ptr, bool* check)
{
if(ptr==NULL){
return 0;
}
int hl=check_balance(ptr->left);
int hr=check_balance(ptr->right);
if(!(hl-hr==1||hl-hr==0||hl-hr==-1))
*check=false;
return max(hl,hr)+1;
}
void root_leaf_sum(node* ptr,int num) //function checks if Root to leaf path sum equal to a given number
{
int sum=0;
bool check=root_leaf_sum(ptr, &sum,num);
check? cout<<"Sum tallys"<<endl:cout<<"Sum doesnt tally"<<endl;
}
bool root_leaf_sum(node* ptr,int* sum,int num) //Utility function for the above
{
if(ptr==NULL)
return false;
(*sum)+=ptr->value;
if(ptr->left==NULL&&ptr->right==NULL){
if(*sum==num){
(*sum)-=ptr->value;
return true;
}
}
bool check1=root_leaf_sum(ptr->left,sum,num);
bool check2=root_leaf_sum(ptr->right,sum,num);
(*sum)-=ptr->value;
return check1||check2;
}
node* inorder_and_preorder(int* in,int size,int* pre) //Construct Tree from given Inorder and Preorder traversals-- Did it by myself! :D
{
node* head=NULL;
int pre_index=0;
inorder_and_preorder(in,size,pre,&pre_index,&head);
return head;
}
void inorder_and_preorder(int* in, int size, int* pre, int* pre_index, node** ptr)
{
if(size<=0)
return;
//find in_index=pre_index;
int in_index;
for(in_index=0;in_index<size;in_index++)
{
if(in[in_index]==pre[*pre_index])
break;
}
*ptr=newNode(in[in_index]);
(*pre_index)++;
inorder_and_preorder(in, in_index,pre, pre_index,&((*ptr)->left));
inorder_and_preorder(in+in_index+1, size-in_index-1,pre, pre_index, &((*ptr)->right));
}
void double_tree(node* ptr)
{
stack<node*> stk;
stk.push(ptr);
while(!stk.empty())
{
node* temp=stk.top();
stk.pop();
if(temp->left)stk.push(temp->left);
if(temp->right)stk.push(temp->right);
node* temp2=temp->left;
temp->left = newNode(temp->value);
temp->left->left=temp2;
}
}
bool check_fold(node* head)
{
return check_fold(head->left,head->right);
}
bool check_fold(node* ptr1,node* ptr2)
{
if(ptr1==NULL&&ptr2==NULL)
return true;
else if((ptr1==NULL&&ptr2!=NULL)||(ptr1!=NULL&&ptr2==NULL))
return false;
return check_fold(ptr1->left,ptr2->right) && check_fold(ptr1->right,ptr2->left);
}
bool check_sum_tree(node* root)
{
bool check=true;
check_sum_tree(root,&check);
return check;
}
int check_sum_tree(node* root, bool* check)
{
if(!root)
return 0;
int left=check_sum_tree(root->left,check);
int right=check_sum_tree(root->right,check);
if(root->left!=NULL||root->right!=NULL){
if(root->value!=left+right){
*check=false;
}
}
return left+right+root->value;
}
int convert_sum_tree(node* head)
{
if(head==NULL)
return 0;
int orig_value=head->value;
if(head->left==NULL&&head->right==NULL)
{
head->value=0;
}
else
head->value=convert_sum_tree(head->left)+convert_sum_tree(head->right);
return orig_value+head->value;
}
node* special_tree(int* in,int size) //Different approach
{
node* root=newNode(in[0]);
for(int i=1;i<size;i++)
{
if(in[i]>root->value)
{
node* temp=root;
root=newNode(in[i]);
root->left=temp;
}
if(in[i]<root->value)
{
node* ptr=root;
while(ptr->right!=NULL&&ptr->right->value>in[i])
{
ptr=ptr->right;
}
if(ptr->right==NULL)
ptr->right=newNode(in[i]);
else{
node* temp=ptr->right;
ptr->right=newNode(in[i]);
ptr->right->left=temp;
}
}
}
return root;
}
node* pre_and_post(int* pre,int* post, int size)
{
if(size==0)
return NULL;
if(size==1)
{
node* root=newNode(pre[0]);
return root;
}
node* root=newNode(pre[0]);
int index;
for(index=0;index<size;index++){
if(post[index]==pre[1]) break;
}
pre++;
root->left=pre_and_post(pre, post, index+1);
root->right=pre_and_post(pre+index+1, post+index+1, size-index-2);
return root;
}
//WRONG METHOD TO CHECK IF BINARY!!
bool check_binary_wrong(node* root)
{
if(root==NULL||(root->left==NULL&&root->right==NULL))
return true;
if(root->left!=NULL&& root->left->value>root->value)
return false;
if(root->right!=NULL&&root->right->value<root->value)
return false;
if(check_binary(root->left)&&check_binary(root->right))
return true;
return false;
}
//CORRECT METHOD:
bool check_binary(node* root)
{
return check_binary(root, INT_MIN, INT_MAX);
}
bool check_binary(node* root, int min, int max)
{
if(root==NULL)return true;
if(root->value<min||root->value>max) return false;
return check_binary(root->left,min,root->value-1)&&check_binary(root->right,root->value+1,max);
}
int largest_bst(node* head)
{
int size=0;
largest_bst(head,INT_MAX,INT_MIN,&size);
return size;
}
int largest_bst(node* root, int low, int high,int* size)
{
static bool check=true;
if(root==NULL)
return 0;
//if(root->left==NULL&&root->right==NULL) return 1;
int left= largest_bst(root->left,root->value,high,size);
int right= largest_bst(root->right,low, root->value,size);
int total=left+right+1;
if( root->value<low&&root->value>high&&check==true) *size=max(total,*size);
else check=false;
cout<<"Check is: "<<check<<endl;
return total;
}
node* sorted_array_bst(int* arr, int high,int low)
{
if(high>low){
int mid=(high+low)/2;
node* head=new node;
head->value=arr[mid];
head->left=sorted_array_bst(arr, mid, low);
head->right=sorted_array_bst(arr, high, mid+1);
return head;
}
return NULL;
}
node* tree_DLL(node* head)
{
node** ptr;
node* new_head=new node;
*ptr=new_head;
tree_DLL(ptr,head);
(*ptr)->right=NULL;
new_head->right->left=NULL;
return new_head->right;
}
void tree_DLL(node** ptr,node* current)
{
if(current==NULL)return;
tree_DLL(ptr,current->left);
(*ptr)->right=current;
current->left=*ptr;
*ptr=(*ptr)->right;
tree_DLL(ptr,current->right);
}
void printList(struct node *root)
{
node* root2=NULL;
while (root != NULL)
{
printf("\t%d", root->value);
if(root->right!=NULL)root2=root;
root = root->right;
}
cout<<"Printing in reverse:"<<endl;
while(root2!=NULL)
{
cout<<root2->value<<" ";
root2=root2->left;
}
}
int subarray_sum(int* arr, int n,int req_sum)
{
int sum=0;
int count=INT_MAX;
for(int i=0,j=0;i<n;i++)
{
sum+=arr[i];
while(sum>req_sum){
count=min(count,i-j+1);
sum-=arr[j];
j++;
}
}
return count;
}
void two_d_spiral(int** A,int m, int n )
{
int i1=0,i2=n-1,j1=0,j2=m-1;
int i,j;
cout<<"Yo! Printing the array in spiral form: "<<endl;
while(i1<i2&&j1<j2)
{
for(j=j1;j<=j2;j++)
{
cout<<A[i1][j]<<" ";
}
for(i=i1+1;i<=i2;i++)
{
cout<<A[i][j2]<<" ";
}
for(j=j2-1;j>=j1;j--)
{
cout<<A[i2][j]<<" ";
}
for(i=i2-1;i>=i1+1;i--)
{
cout<<A[i][j1]<<" ";
}
i1=i1+1; i2=i2-1; j1=j1+1; j2=j2-1;
}
if(i1<i2&&j1==j2)
{
for(i=i1;i<=i2;i++)
cout<<A[i][j1]<<" ";
}
else if(j1<j2&&i1==i2)
{
for(j=j1;j<=j2;j++)
cout<<A[i1][j]<<" ";
}
}