-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
520 lines (497 loc) · 10.9 KB
/
Tree.cpp
File metadata and controls
520 lines (497 loc) · 10.9 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
/*
Name:Chirantan Joshi
Division:G
Batch: G1
Roll No.:PG13
TREE OPERATIONS WITH AND WITHOUT RECURSION
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class treenode //treenode class
{
int data;
treenode *right,*left; //right and left pointers for treenode
friend class tree;
};
class stack //stack
{
int top;
treenode *data[30];
public:
stack()
{top=-1;}
void push(treenode *temp) //push
{
top++;
data[top]=temp;
}
treenode *pop() //pop
{
treenode *temp;
temp=data[top];
top--;
return temp;
}
int isemp() //check empty or not
{
if(top==-1)
return 1;
else
return 0;
}
friend class tree;
};
class tree
{
treenode *root; //creating root
public:
tree()
{
root=NULL;
}
void create_r() //creating tree with recursion
{
root=new treenode;
cout<<"Enter the data at root: ";
cin>>root->data;
create_r(root); //passing root to create function
}
void create_r(treenode *temp)
{
treenode *curr; //current node to traverse tree
char choicel='n',choicer='n';
cout<<"Do you want to add node to left of "<<temp->data<<" (y/n): ";
cin>>choicel;
if(choicel=='y') //creating left subtree with recursion
{
curr=new treenode;
cout<<"Enter the data: ";
cin>>curr->data;
temp->left=curr;
create_r(curr);
}
cout<<"Do you want to add node to right of "<<temp->data<<" (y/n): ";
cin>>choicer;
if(choicer=='y') //creating right subtree with recursion
{
curr=new treenode;
cout<<"Enter the data: ";
cin>>curr->data;
temp->right=curr;
create_r(curr);
}
}
void create_nr() //creation whout recursion
{
char choice='n',choicelr='n';
treenode *temp,*curr;
if(root==NULL) //first node
{
root=new treenode;
cout<<"Enter the data at root: ";
cin>>root->data;
}
do
{
temp=root;
int flag=0;
while(flag==0)
{
cout<<"Enter at which side of "<<temp->data<<" you want to add node(l/r): ";
cin>>choicelr;
if(choicelr=='l') //left subtree
{
if(temp->left==NULL)
{
curr=new treenode;
cout<<"Enter the data at left of "<<temp->data<<": ";
cin>>curr->data;
temp->left=curr;
flag=1;
}
temp=temp->left;
}
else //right subtree
{
if(temp->right==NULL)
{
curr=new treenode;
cout<<"Enter the data at right of "<<temp->data<<": ";
cin>>curr->data;
temp->right=curr;
flag=1;
}
temp=temp->right;
}
}
cout<<"Do you want to add more nodes(y/n)? ";
cin>>choice;
}while(choice=='y'); //continue till choice yes
}
void inorder_r()
{
inorder_r(root);
}
void preorder_r()
{
preorder_r(root);
}
void postorder_r()
{
postorder_r(root);
}
void inorder_r(treenode *temp) //inorder traversal
{
if(temp!=NULL)
{
inorder_r(temp->left); //traversing left subtree
cout<<temp->data<<endl; //visit node
inorder_r(temp->right); //traversing right subtree
}
}
void preorder_r(treenode *temp) //preorder traversal
{
if(temp!=NULL)
{
cout<<temp->data<<endl; //visit node
preorder_r(temp->left); //traversing left subtree
preorder_r(temp->right); //traversing right subtree
}
}
void postorder_r(treenode *temp) //postorder traversal
{
if(temp!=NULL)
{
postorder_r(temp->left); //traversing left subtree
postorder_r(temp->right); //traversing right subtree
cout<<temp->data<<endl; //visit node
}
}
void inorder_nr()
{
stack s;
treenode *temp=root;
while(1) //infinite loop
{
while(temp!=NULL)
{
s.push(temp); //push node in stack and move to left subtree
temp=temp->left;
}
if(s.isemp()==1) //if stack empty break loop
break;
temp=s.pop(); //visit node from stack
cout<<temp->data<<endl;
temp=temp->right; //move to right subtree
}
}
void preorder_nr() //preorder traversal
{
stack s;
treenode *temp=root;
while(1)
{
while(temp!=NULL)
{
cout<<temp->data<<endl; //visit node
s.push(temp); //push node to stack and move to left subtree
temp=temp->left;
}
if(s.isemp())
break;
temp=s.pop();
temp=temp->right; //move to right subtree
}
}
void postorder_nr() //postorder traversal
{
stack s;
treenode *temp=root;
while(1)
{
while(temp!=NULL)
{
s.push(temp); //push node to stack and move to left subtree
temp=temp->left;
}
if(s.data[s.top]->right==NULL) //move to right subtree
{
temp=s.pop();
cout<<temp->data<<endl;
}
while(!s.isemp()&&s.data[s.top]->right==temp) //visit node from stack
{
temp=s.pop();
cout<<temp->data<<endl;
}
if(s.isemp())
break;
temp=s.data[s.top]->right;
}
}
};
int main()
{
tree bt;
int num;
while(1)
{
cout<<"\n-------------MENU-------------\n1.Create with recursion.\n2.Create without recursion.\n3.Inorder with recursion.\n4.Inorder without recursion.\n5.Preorder with recursion.\n6.Preorder without recursion.\n7.Postorder with recursion.\n8.Postorder without recursion.\n9.Exit.\nEnter your choice: ";
cin>>num;
cout<<endl;
switch(num)
{
case 1: bt.create_r();
break;
case 2: bt.create_nr();
break;
case 3: cout<<"Inorder traversal without recursion: "<<endl;
bt.inorder_r();
break;
case 4: cout<<"Inorder traversal with recursion: "<<endl;
bt.inorder_nr();
break;
case 5: cout<<"Preorder traversal without recursion: "<<endl;
bt.preorder_r();
break;
case 6: cout<<"Preorder traversal with recursion: "<<endl;
bt.preorder_nr();
break;
case 7: cout<<"Postorder traversal without recursion: "<<endl;
bt.postorder_r();
break;
case 8: cout<<"Postorder traversal with recursion: "<<endl;
bt.postorder_nr();
break;
case 9: exit(1);
break;
default:cout<<"Wrong choice!!";
}
}
return 0;
}
/*
OUTPUT:
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 1
Enter the data at root: 12
Do you want to add node to left of 12 (y/n): y
Enter the data: 6
Do you want to add node to left of 6 (y/n): y
Enter the data: 2
Do you want to add node to left of 2 (y/n): n
Do you want to add node to right of 2 (y/n): n
Do you want to add node to right of 6 (y/n): y
Enter the data: 4
Do you want to add node to left of 4 (y/n): y
Enter the data: 1
Do you want to add node to left of 1 (y/n): n
Do you want to add node to right of 1 (y/n): n
Do you want to add node to right of 4 (y/n): n
Do you want to add node to right of 12 (y/n): y
Enter the data: 8
Do you want to add node to left of 8 (y/n): n
Do you want to add node to right of 8 (y/n): n
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 3
Inorder traversal without recursion:
2
6
1
4
12
8
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 4
Inorder traversal with recursion:
2
6
1
4
12
8
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 5
Preorder traversal without recursion:
12
6
2
4
1
8
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 6
Preorder traversal with recursion:
12
6
2
4
1
8
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 7
Postorder traversal without recursion:
2
1
4
6
8
12
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 8
Postorder traversal with recursion:
2
1
4
6
8
12
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 2
Enter at which side of 12 you want to add node(l/r): l
Enter at which side of 6 you want to add node(l/r): l
Enter at which side of 2 you want to add node(l/r): l
Enter the data at left of 2: 5
Do you want to add more nodes(y/n)? y
Enter at which side of 12 you want to add node(l/r): r
Enter at which side of 8 you want to add node(l/r): l
Enter the data at left of 8: 10
Do you want to add more nodes(y/n)? y
Enter at which side of 12 you want to add node(l/r): r
Enter at which side of 8 you want to add node(l/r): r
Enter the data at right of 8: 20
Do you want to add more nodes(y/n)? y
Enter at which side of 12 you want to add node(l/r): r
Enter at which side of 8 you want to add node(l/r): r
Enter at which side of 20 you want to add node(l/r): l
Enter the data at left of 20: 15
Do you want to add more nodes(y/n)? n
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 3
Inorder traversal without recursion:
5
2
6
1
4
12
10
8
15
20
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 6
Preorder traversal with recursion:
12
6
2
5
4
1
8
10
20
15
-------------MENU-------------
1.Create with recursion.
2.Create without recursion.
3.Inorder with recursion.
4.Inorder without recursion.
5.Preorder with recursion.
6.Preorder without recursion.
7.Postorder with recursion.
8.Postorder without recursion.
9.Exit.
Enter your choice: 9
*/