-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Tree_Traversal.cpp
464 lines (409 loc) · 8.17 KB
/
Binary_Tree_Traversal.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
/*
Title : Program to implement Binary Tree Traversals in Recursive and Non Recursive way.
*/
// Program -
#include <iostream>
using namespace std;
struct BinaryTree
{
BinaryTree *left;
BinaryTree *right;
int data;
};
class BT
{
BinaryTree *root, *newnode, *temp, *parent, *stack[20];
int top;
public:
BT() // Constuctor
{
root = NULL;
top = -1;
}
int isempty();
int isfull();
void push(BinaryTree *);
BinaryTree *pop();
void create();
void display();
void insert(BinaryTree *, BinaryTree *);
void inorder(BinaryTree *);
void preorder(BinaryTree *);
void postorder(BinaryTree *);
void inorder_nonrec(BinaryTree *);
void preorder_nonrec(BinaryTree *);
void postorder_nonrec(BinaryTree *);
};
int BT::isempty()
{
if (top == -1)
return 1;
return 0;
}
int BT::isfull()
{
if (top == 19)
return 1;
return 0;
}
void BT::push(BinaryTree *d)
{
if (!isfull())
stack[++top] = d;
}
BinaryTree *BT::pop()
{
BinaryTree *temp = stack[top];
top--;
return temp;
}
void BT::inorder_nonrec(BinaryTree *root)
{
BT s;
while (!s.isempty() || root != NULL)
{
while (root != NULL)
{
s.push(root);
root = root->left;
}
root = s.pop();
cout << " " << root->data << " ";
root = root->right;
}
}
void BT::preorder_nonrec(BinaryTree *root)
{
BT s;
while (!s.isempty() || root != NULL)
{
while (root != NULL)
{
cout << " " << root->data << " ";
s.push(root);
root = root->left;
}
root = s.pop();
root = root->right;
}
}
void BT::postorder_nonrec(BinaryTree *root)
{
BT s;
int i = 0;
int str[20];
while (!s.isempty() || root != NULL)
{
while (root != NULL)
{
str[i++] = root->data;
s.push(root);
root = root->right;
}
root = s.pop();
root = root->left;
}
while (--i >= 0)
cout << str[i] << " ";
}
void BT::create()
{
newnode = new BinaryTree; // dynamically creating new BinaryTree
newnode->left = NULL; // intialiing the tree
newnode->right = NULL;
cout << "Enter a element::";
cin >> newnode->data;
if (root == NULL)
{
root = newnode;
cout << "Root is Created";
}
else
{
insert(root, newnode);
}
}
void BT::insert(BinaryTree *root, BinaryTree *newnode)
{
char choice;
cout << "\t " << root->data;
cout << "\n\t /";
cout << " \\";
cout << "\n\tLeft";
cout << " Right\n\n";
cout << "\nEnter Your Choice Here -->> ";
cin >> choice;
if (choice == 'L' || choice == 'l')
{
if (root->left == NULL)
{
root->left = newnode;
}
else
{
insert(root->left, newnode);
}
}
else
{
if (root->right == NULL)
{
root->right = newnode;
}
else
{
insert(root->right, newnode);
}
}
}
void BT::display() // to display the BT
{
int t, m;
if (root == NULL)
{
cout << "\nTree Is Empty !!";
}
else
{
do
{
cout << "Which Traversal U want ? \n======RECURSIVE======\n1.Inorder \n2.Preorder \n3.Postorder\n====NON RECURSIVE====\n4.Inorder \n5.Preorder \n6.Postorder";
cin >> t;
switch (t)
{
case 1:
cout << "\nInorder By Recursive";
inorder(root);
break;
case 2:
cout << "\npreorder By Recursive";
preorder(root);
break;
case 3:
cout << "\npostorder By Recursive";
postorder(root);
break;
case 4:
cout << "\nInorder By Non Recursive";
inorder_nonrec(root);
break;
case 5:
cout << "\npreorder By Non Recursive";
preorder_nonrec(root);
break;
case 6:
cout << "\npostorder By Non Recursive";
postorder_nonrec(root);
break;
}
cout << "\nDo U want another traversal\n1.YES\n2.NO";
cin >> m;
} while (m != 2);
}
}
void BT::inorder(BinaryTree *ptr) // inorder by recursive method
{
if (ptr != NULL)
{
inorder(ptr->left);
cout << " " << ptr->data << " ";
inorder(ptr->right);
}
}
void BT ::postorder(BinaryTree *ptr) // postorder by recursive
{
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout << " " << ptr->data << " ";
}
}
void BT::preorder(BinaryTree *ptr) // preorder by recursive
{
if (ptr != NULL)
{
cout << " " << ptr->data << " ";
preorder(ptr->left);
preorder(ptr->right);
}
}
int main()
{
int ch1, ch2, o;
BT obj;
do
{
cout << "\nWhich operation do you want perform \n\t 1.Insert Element :: \n\t 2.Display Tree::";
cin >> ch1;
switch (ch1)
{
case 1:
do
{
obj.create();
cout << "\n\nDo you want to Add more BinaryTree\n\t 1. Yes 2. No ::";
cin >> o;
} while (o != 2);
break;
case 2:
obj.display();
break;
}
cout << "\nDo you want to continue \n\t 1.Yes 2.No ::";
cin >> ch2;
} while (ch2 != 2);
return 0;
}
/* OutPut -
Which operation do you want perform
1.Insert Element ::
2.Display Tree::1
Enter a element::15
Root is Created
Do you want to Add more BinaryTree
1. Yes 2. No ::1
Enter a element::10
15
/ \
Left Right
Enter Your Choice Here -->> l
Do you want to Add more BinaryTree
1. Yes 2. No ::1
Enter a element::8
15
/ \
Left Right
Enter Your Choice Here -->> l
10
/ \
Left Right
Enter Your Choice Here -->> l
Do you want to Add more BinaryTree
1. Yes 2. No ::1
Enter a element::12
15
/ \
Left Right
Enter Your Choice Here -->> l
10
/ \
Left Right
Enter Your Choice Here -->> r
Do you want to Add more BinaryTree
1. Yes 2. No ::1
Enter a element::19
15
/ \
Left Right
Enter Your Choice Here -->> r
Do you want to Add more BinaryTree
1. Yes 2. No ::1
Enter a element::18
15
/ \
Left Right
Enter Your Choice Here -->> r
19
/ \
Left Right
Enter Your Choice Here -->> l
Do you want to Add more BinaryTree
1. Yes 2. No ::1
Enter a element::30
15
/ \
Left Right
Enter Your Choice Here -->> r
19
/ \
Left Right
Enter Your Choice Here -->> r
Do you want to Add more BinaryTree
1. Yes 2. No ::2
Do you want to continue
1.Yes 2.No ::1
Which operation do you want perform
1.Insert Element ::
2.Display Tree::2
Which Traversal U want ?
======RECURSIVE======
1.Inorder
2.Preorder
3.Postorder
====NON RECURSIVE====
4.Inorder
5.Preorder
6.Postorder1
Inorder By Recursive 8 10 12 15 18 19 30
Do U want another traversal
1.YES
2.NO1
Which Traversal U want ?
======RECURSIVE======
1.Inorder
2.Preorder
3.Postorder
====NON RECURSIVE====
4.Inorder
5.Preorder
6.Postorder2
preorder By Recursive 15 10 8 12 19 18 30
Do U want another traversal
1.YES
2.NO1
Which Traversal U want ?
======RECURSIVE======
1.Inorder
2.Preorder
3.Postorder
====NON RECURSIVE====
4.Inorder
5.Preorder
6.Postorder3
postorder By Recursive 8 12 10 18 30 19 15
Do U want another traversal
1.YES
2.NO1
Which Traversal U want ?
======RECURSIVE======
1.Inorder
2.Preorder
3.Postorder
====NON RECURSIVE====
4.Inorder
5.Preorder
6.Postorder4
Inorder By Non Recursive 8 10 12 15 18 19 30
Do U want another traversal
1.YES
2.NO1
Which Traversal U want ?
======RECURSIVE======
1.Inorder
2.Preorder
3.Postorder
====NON RECURSIVE====
4.Inorder
5.Preorder
6.Postorder5
preorder By Non Recursive 15 10 8 12 19 18 30
Do U want another traversal
1.YES
2.NO1
Which Traversal U want ?
======RECURSIVE======
1.Inorder
2.Preorder
3.Postorder
====NON RECURSIVE====
4.Inorder
5.Preorder
6.Postorder6
postorder By Non Recursive8 12 10 18 30 19 15
Do U want another traversal
1.YES
2.NO2
Do you want to continue
1.Yes 2.No ::2
*/