forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binarytree_operations.cpp
617 lines (560 loc) · 12.5 KB
/
Binarytree_operations.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
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
//============================================================================
// Name : btree.cpp
// Author : Snehal Khandve
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
/*
1) This C++ program has both recursive and non-recursive codes for all traversals , height , copy and
mirror image of the tree .
2) For better understanding of data structure , I have not taken tree nodes in an array
but constructed it using a class object for each tree node .
3) Also , for those who wants clear understanding of how stack and queue works , I have written this code
without making use of STL vector and queue and have actually implemented stack and queue
.
*/
#include <iostream>
#include <cmath>
using namespace std;
#define MAX 10
// 1. insert
// 2. delete
// 3. modify
// 4. search
class node
{
node *right;
node *left;
int data;
public:
node()
{
data=0;
right=left=NULL;
}
node(int d)
{
data=d;
right=left=NULL;
}
friend class queue1;
friend class binarytree;
friend class stack1;
};
class queue1
{
int front,rear;
node * arr[MAX];
public:
queue1()
{
front=rear=-1;
for(int i=0; i<MAX; i++)
arr[i]=NULL;
}
int isempty()
{
if(front==-1)
return 1;
else
return 0;
}
int isfull()
{
if(rear==MAX)
return 1;
else
return 0;
}
node* getrear()
{
node* temp=arr[rear];
return temp;
}
node* getfront()
{
node* temp=arr[front];
return temp;
}
void insert(node* temp)
{
if(!isfull())
{
if(front==-1)
{
++front;
}
++rear;
arr[rear]=temp;
}
else
cout<<"\nQueue is full";
}
node* del()
{
//cout<<size();
if(!isempty())
{
node* temp;
temp=arr[front];
if(front==rear)
front=rear=-1;
else
front++;
return temp;
}
else
{
cout<<"\nQueue is empty";
return NULL;
}
}
void clear()
{
int i=0;
while(arr[i]!=NULL)
{
arr[i]=NULL;
i++;
}
}
void disp()
{
int i=front;
while(i!=(rear+1))
{
cout<<"\n"<<arr[i]->data;
i++;
}
if(isempty()) // NOt working :(
cout<<"\nUnderflow";
}
};
class stack1
{
int top;
node* data[20];
public:
stack1()
{
top=-1;
}
node* pop()
{
if(isempty())
cout<<"\nUnderflow";
else
{
node* temp=data[top];
top--;
return temp;
}
}
void push(node * temp)
{
if(isfull())
cout<<"\nOverflow";
else
{
top++;
data[top]=temp;
}
}
int isfull()
{
if(top==(MAX-1))
return 1;
else
return 0;
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
};
class binarytree
{
node *root;
int info,leaf_nodes;
public:
int no_of_ele;
node* getroot()
{
return root;
}
binarytree() //constructor
{
root=NULL;
info=0;
no_of_ele=0;
leaf_nodes=0;
}
queue1 q;
void create_nonrec()
{
int info;
queue1 q;
node *temp;
if(root==NULL)
{
cout<<"\nEnter data";
cin>>info;
root=new node(info);
no_of_ele++;
}
q.insert(root);
while(!q.isempty())
{
temp=q.del();
cout<<"Left data of "<<temp->data;
cin>>info;
if(info!=(-1))
{
temp->left=new node(info);
q.insert(temp->left);
no_of_ele++;
}
cout<<"Right data of "<<temp->data;
cin>>info;
if(info!=(-1))
{
temp->right=new node(info);
q.insert(temp->right);
no_of_ele++;
}
}
}
void insert_rec()
{
root=create_rec();
}
node* create_rec()
{
node *r;
cout<<"\n Enter data ";
cin>>info;
if(info==-1)
return NULL;
else
{
r=new node(info);
no_of_ele++;
cout<<"\nLeft child of "<<r->data;
r->left=create_rec();
cout<<"\nRight child of "<<r->data;
r->right=create_rec();
return r;
}
}
void inorder_rec(node *temp) //being recursive code u have to pass root as a
{ //parameter so every tym it is called we pass a node
if(temp!=NULL)
{
inorder_rec(temp->left);
cout<<temp->data<<" ";
inorder_rec(temp->right);
}
}
void inorder_nonrec() //being non-rec we needn't pass the root cuz it isn't being
{ // and doesn't need the parameter
node* current=root;
stack1 s; // whatever we did for recursive internally with the stack we do same thing
label: while(current!=NULL) // here in non-recursive but here we just hand code the internal working
{
s.push(current); // inorder_rec(temp->left); --> equivalent line in recursive code
current=current->left;
}
if(current==NULL && !s.isempty())
{
node* item=s.pop(); // cout<<temp->data<<" "; --> equivalent line in recursive code
cout<<item->data<<" ";
current=item->right; // inorder_rec(temp->right); --> equivalent line in recursive code
goto label;
}
if(current==NULL && s.isempty())
return;
}
void postorder_rec(node *temp)
{
if(temp!=NULL)
{
postorder_rec(temp->left);
postorder_rec(temp->right);
cout<<temp->data<<" ";
}
}
void postorder_nonrec()
{
stack1 s1,s2;
node *temp;
node *p;
while(!s1.isempty()) //to clear the stack
s1.pop();
while(!s2.isempty()) //to clear the stack
s2.pop();
s1.push(root);
while(!s1.isempty())
{
temp=s1.pop();
s2.push(temp); // take example : 20
// / \
// 10 30
if(temp->left!=NULL)
s1.push(temp->left); // 1st take root into s1
if(temp->right!=NULL) // keep popping out s1 n insert this popped element into s2
s1.push(temp->right); // also inert popped element left n right into s1
} // entire s2 is to be displayed
while(!s2.isempty())
{
p=s2.pop();
cout<<p->data<<" ";
}
}
void preorder_rec(node *temp)
{
if(temp!=NULL)
{
cout<<temp->data<<" ";
preorder_rec(temp->left);
preorder_rec(temp->right);
}
}
void preorder_nonrec()
{
stack1 s;
node *temp;
while(!s.isempty())
s.pop();
s.push(root);
while(!s.isempty())
{
temp=s.pop();
cout<<temp->data<<" ";
if(temp->right!=NULL) // so that root->left nodes remain on top of stack n left children r
s.push(temp->right); // displayed 1st then right children
if(temp->left!=NULL)
s.push(temp->left);
}
}
int height_rec(node *temp)
{
int lefth=0,righth=0;
if(temp==NULL)
return 0; //return -1 if u want height of leaf as 0
else
{
lefth+=height_rec(temp->left);
righth+=height_rec(temp->right);
return(1+max(lefth,righth));
}
}
int height_nonrec()
{
int ht=0;
node *temp;
if(root==NULL)
return 0;
q.insert(root);
while(1)
{
if(q.isempty())
return ht;
while(!q.isempty())
{
ht++;
temp=q.del();
if((temp->left)!=NULL)
{
q.insert(temp->left);
if((temp->right)!=NULL) // if 2 nodes r present at same lvl no need to
ht--; // increment ht for just 1 node
}
if((temp->right)!=NULL) // if 2 nodes present at same lvl ht is incremented for
{ // this node
q.insert(temp->right);
}
}
}
// return ht;
}
void mirror_rec(node *temp)
{
if(temp!=NULL)
{
mirror_rec(temp->left);
mirror_rec(temp->right);
node *temp2=temp->left; // exchanging code
temp->left=temp->right;
temp->right=temp2;
}
}
void mirror_nonrec()
{ // same as tree traversal just exchange it's r n l links
queue1 q;
q.clear();
q.insert(root);
node *temp;
while(!q.isempty())
{
temp = q.del();
if(temp->left!=NULL)
q.insert(temp->left);
if(temp->right!=NULL)
q.insert(temp->right);
node *exch;
exch = temp->left;
temp->left = temp->right;
temp->right = exch;
}
}
void insert_copyrec(node* root1)
{
root=copy_rec(root1);
}
node* copy_rec(node* root1) //copy from root1 to root2
{ // same as creation
node* root2;
if(root1==NULL)
return NULL;
else
{
root2=new node(root1->data);
no_of_ele++;
root2->left=copy_rec(root1->left);
root2->right=copy_rec(root1->right);
return root2;
}
}
void copy_nonrec(node* root1) //copy from root1 to root2
{
queue1 orig;
queue1 cop;
orig.insert(root1);
node *temp;
if(root1!=NULL)
{
temp = new node(root1->data);
root = temp;
}
cop.insert(temp);
while(!orig.isempty())
{
node *o = orig.del();
node *c = cop.del();
if(o->left!=NULL)
{
c->left = new node(o->left->data);
orig.insert(o->left);
cop.insert(c->left);
}
if(o->right!=NULL)
{
c->right = new node(o->right->data);
orig.insert(o->right);
cop.insert(c->right);
}
}
}
void deltree(node *temp)
{
if(temp!=NULL)
{
cout<<"\n inside delete()";
deltree(temp->left);
deltree(temp->right);
temp=NULL;
delete temp;
cout<<"\nAfter deletion";
}
}
void operator = (binarytree b)
{
insert_copyrec(b.getroot());
}
int count_nodes(node* temp)
{
if(root!=NULL)
{
count_nodes(temp->left); // }->this is just a traversal thru tree
count_nodes(temp->right); // }
if((temp->left==NULL)&&(temp->right==NULL))
{
leaf_nodes++;
}
}
return leaf_nodes;
}
};
int main() {
int choice;
int c=1,leaf_nodes;
binarytree obj,obj2;
queue1 q;
do
{
cout<<"\nEnter\n 1.Create non-recursively.\n 2.Create recursively.\n";
cin>>choice;
switch(choice)
{
case 1: obj.create_nonrec(); //non-recurrence method
cout<<"\nInorder :";
obj.inorder_nonrec();
cout<<"\nPreorder :";
obj.preorder_nonrec();
cout<<"\nPostorder :";
obj.postorder_nonrec();
cout<<"\nHeight of tree non-rec :"<<obj.height_nonrec();
cout<<"\nCopy of tree :";
obj2.copy_nonrec(obj.getroot());
cout<<"\nInorder :";
obj2.inorder_nonrec();
cout<<"\nPreorder :";
obj2.preorder_nonrec();
cout<<"\nPostorder :";
obj2.postorder_nonrec();
cout<<"\nMirror of tree :";
obj.mirror_nonrec();
cout<<"\nInorder :";
obj.inorder_nonrec();
cout<<"\nPreorder :";
obj.preorder_nonrec();
cout<<"\nPostorder :";
obj.postorder_nonrec();
break;
case 2: obj.insert_rec();
cout<<"\nInorder :";
obj.inorder_rec(obj.getroot());
cout<<"\nPreorder :";
obj.preorder_rec(obj.getroot());
cout<<"\nPostorder :";
obj.postorder_rec(obj.getroot());
cout<<"\nHeight of tree :"<<obj.height_rec(obj.getroot());
cout<<"\nCopy of tree :";
obj2=obj;
cout<<"\nInorder :";
obj2.inorder_rec(obj2.getroot());
cout<<"\nPreorder :";
obj2.preorder_rec(obj2.getroot());
cout<<"\nPostorder :";
obj2.postorder_rec(obj2.getroot());
cout<<"\nMirror of tree :";
obj2.mirror_rec(obj2.getroot());
cout<<"\nInorder :";
obj2.inorder_rec(obj2.getroot());
cout<<"\nPreorder :";
obj2.preorder_rec(obj2.getroot());
cout<<"\nPostorder :";
obj2.postorder_rec(obj2.getroot());
leaf_nodes=obj.count_nodes(obj.getroot());
cout<<"\nTotal Interior nodes :"<<(obj.no_of_ele)-leaf_nodes;
cout<<"\nTotal leaf nodes"<<leaf_nodes;
break;
}
cout<<"\nContinue?(0/1)";
cin>>c;
}while(c==1);
}