forked from kabirthakkar/Hkfst2k21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
545 lines (502 loc) · 12.8 KB
/
BinaryTree.java
File metadata and controls
545 lines (502 loc) · 12.8 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
/*
Create a binary tree and perform inorder, preorder and postorder
*/
package tree;
import java.util.*;
//IMPORTING ALL THE CLASSES
class TreeNode
{
TreeNode left,right;
int data;
public TreeNode (int d)//parameterized constructor
{
left=null;
right=null;
data=d;
}
}
class BinaryTree{ //Binary tree class
TreeNode root;
public BinaryTree()//Default constructor
{
root=null;
}
void create()//Create tree
{
Scanner sc= new Scanner(System.in);
Scanner sn= new Scanner(System.in);
int ans;
do {
System.out.println("Enter the data you want to insert in the Binary tree: ");
int d=sc.nextInt();
TreeNode temp=new TreeNode(d);
TreeNode ptr;
if(root==null) {//If the element is the first element in the tree
root=temp;
}
else {
ptr=root;
while(ptr!=null) {
System.out.println("Enter the direction(l/r): ");
char dir=sn.nextLine().charAt(0);
if(dir=='l') {
if(ptr.left==null) {//If left child is not present then attaching temp as left child
ptr.left=temp;
break;
}
else {
ptr=ptr.left;//If left child is present then shifting ptr on it
}
}
else {
if(ptr.right==null) {//If right child is not present then attaching temp as right child
ptr.right=temp;
break;
}
else {
ptr=ptr.right;//If right child is present then shifting ptr on it
}
}
}
}
System.out.println("Do you want to continue(1/0): ");
ans =sc.nextInt();
}while(ans!=0);
}
void recursiveInorder(TreeNode lRoot)//Recursive inorder function
{
if(lRoot==null) {//Base case
return;
}
recursiveInorder(lRoot.left); //processing inorder of left subtree
System.out.println(lRoot.data);//Displaying current node data
recursiveInorder(lRoot.right);//processing inorder of right subtree
}
void recursivePreorder(TreeNode lRoot)//Recursive Preorder function
{
if(lRoot==null) {//Base case
return;
}
System.out.println(lRoot.data);//Displaying current node data
recursivePreorder(lRoot.left);//processing Preorder of left subtree
recursivePreorder(lRoot.right);//processing Preorder of right subtree
}
void recursivePostorder(TreeNode lRoot)//Recursive Postorder function
{
if(lRoot==null) {//Base case
return;
}
recursivePostorder(lRoot.left);//processing Postorder of left subtree
recursivePostorder(lRoot.right);//processing Postorder of right subtree
System.out.println(lRoot.data);//Displaying current node data
}
void nonrecursiveInorder()//Non Recursive Inorder function
{
Stack<TreeNode> st=new Stack<TreeNode>();
TreeNode ptr=root;
while(ptr!=null || !st.empty()) {
while(ptr!=null) {//Traversing till leftmost node
st.push(ptr);
ptr=ptr.left;
}
if(!st.empty()) {//Pop element from stack and print the data and move to the right child
ptr=st.pop();
System.out.println(ptr.data);
ptr=ptr.right;
}
}
}
void nonrecursivePreorder()//Non Recursive Preorder function
{
Stack<TreeNode> st=new Stack<TreeNode>();
TreeNode ptr=root;
while(ptr!=null || !st.empty()) {
while(ptr!=null) {//Traversing and printing data till leftmost node
System.out.println(ptr.data);
st.push(ptr);
ptr=ptr.left;
}
if(!st.empty()) {//Pop element from stack and move to the right child
ptr=st.pop();
ptr=ptr.right;
}
}
}
void nonrecursivePostOrder()//Non recursive post order function
{
TreeNode ptr=root;
if(root==null)
return ;
TreeNode pre=null;
Stack<TreeNode> s = new Stack();
while(ptr!=null || !s.empty()){
if(ptr!=null){ //Traversing till leftmost node
s.push(ptr);
ptr = ptr.left;
}
else{
ptr = s.peek();
if(ptr.right==null || ptr.right==pre){//Checking if the node do not have right child or if the right child is already processed
System.out.println(ptr.data);
s.pop();
pre=ptr;
ptr = null;
}
else
ptr = ptr.right;//Moving ptr to the right child
}
}
}
/********************************EXTRA*************************************************/
void displayTree(TreeNode lroot) //function to Display Binary tree
{
if(lroot==null)//Base case
return;
System.out.print(lroot.data + " : ");//Displaying data of the current node
if(lroot.left!=null)
System.out.print("L " + lroot.left.data);//Displaying left child of the current node if present
if(lroot.right!=null)
System.out.print(" R " + lroot.right.data);//Displaying right child of the current node if present
System.out.println(" ");
displayTree(lroot.left);//Recursive call on left subtree
displayTree(lroot.right);//Recursive call on right subtree
}
int count(TreeNode lroot)//Function to count no of nodes in the tree
{
if(lroot==null) {//Base Case
return 0;
}
int ct=1+count(lroot.left)+count(lroot.right);//Recursive call
return ct;
}
}
public class Mainclass {//Main class
public static void main(String[] args) {
int ch;
BinaryTree b=new BinaryTree();
Scanner sc=new Scanner(System.in);
do
{
System.out.println("**************************************");
System.out.println("MENU: \n\n1.Create a Binary Tree \n2.Display the recursive Inorder \n3.Display the non recursive Inorder \n4.Display the non recursive Preorder\n5.Display the non Recursive Postorder \n6.Display the recursive Preorder \n7.Display the recursive Postorder\n8. Display Tree\n9.Count no of Nodes");
System.out.println("\n*************************************");
System.out.println("\nEnter your choice: ");
ch=sc.nextInt();
switch(ch)//Switch case
{
case 1:
b.create();//call create();
break;
case 2:
System.out.println("\nThe Recursive Inorder is: ");
b.recursiveInorder(b.root);//callInOrder();
break;
case 3:
System.out.println("\nThe Non Recursive Inorder is: ");
b.nonrecursiveInorder();//nonrecursiveInorder();
break;
case 4:
System.out.println("\nThe Non Recursive Preorder is: ");
b.nonrecursivePreorder();//nonrecursivePreorder();
break;
case 5:
System.out.println("\nThe Non Recursive Postorder is: ");
b.nonrecursivePostOrder();//nonrecursivePostOrder();
break;
case 6:
System.out.println("\nThe Recursive Preorder is: ");
b.recursivePreorder(b.root);//callPreOrder();
break;
case 7:
System.out.println("\nThe Recursive Postorder is: ");
b.recursivePostorder(b.root);//callPostOrder();
break;
case 8:
System.out.println("\nThe Binary Tree is: ");
b.displayTree(b.root);//callPostOrder();
break;
case 9:
System.out.println("\nThe number of nodes in the tree are: "+ b.count(b.root));//callPostOrder();
break;
}
System.out.println("Do you want to continue ,1 to continue,0 to exit: ");
ch=sc.nextInt();
}
while (ch==1);
}
}
/* TIME COMPLEXITIES
* Create: O(n)
* Recursive Inorder,Preorder,Postorder: O(n)
* Non Recursive Inorder,Preorder,Postorder: O(n)
* Display Tree:O(n)
* Count: O(n)
*
* Output
* **************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
1
Enter the data you want to insert in the Binary tree:
13
Do you want to continue(1/0):
1
Enter the data you want to insert in the Binary tree:
8
Enter the direction(l/r):
l
Do you want to continue(1/0):
1
Enter the data you want to insert in the Binary tree:
15
Enter the direction(l/r):
r
Do you want to continue(1/0):
1
Enter the data you want to insert in the Binary tree:
14
Enter the direction(l/r):
r
Enter the direction(l/r):
l
Do you want to continue(1/0):
1
Enter the data you want to insert in the Binary tree:
19
Enter the direction(l/r):
r
Enter the direction(l/r):
r
Do you want to continue(1/0):
1
Enter the data you want to insert in the Binary tree:
7
Enter the direction(l/r):
l
Enter the direction(l/r):
l
Do you want to continue(1/0):
1
Enter the data you want to insert in the Binary tree:
10
Enter the direction(l/r):
l
Enter the direction(l/r):
r
Do you want to continue(1/0):
1
Enter the data you want to insert in the Binary tree:
4
Enter the direction(l/r):
l
Enter the direction(l/r):
l
Enter the direction(l/r):
l
Do you want to continue(1/0):
0
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
8
The Binary Tree is:
13 : L 8 R 15
8 : L 7 R 10
7 : L 4
4 :
10 :
15 : L 14 R 19
14 :
19 :
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
2
The Recursive Inorder is:
4
7
8
10
13
14
15
19
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
3
The Non Recursive Inorder is:
4
7
8
10
13
14
15
19
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
4
The Non Recursive Preorder is:
13
8
7
4
10
15
14
19
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
6
The Recursive Preorder is:
13
8
7
4
10
15
14
19
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
5
The Non Recursive Postorder is:
4
7
10
8
14
19
15
13
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
7
The Recursive Postorder is:
4
7
10
8
14
19
15
13
Do you want to continue ,1 to continue,0 to exit:
1
**************************************
MENU:
1.Create a Binary Tree
2.Display the recursive Inorder
3.Display the non recursive Inorder
4.Display the non recursive Preorder
5.Display the non Recursive Postorder
6.Display the recursive Preorder
7.Display the recursive Postorder
8. Display Tree
9.Count no of Nodes
*************************************
Enter your choice:
9
The number of nodes in the tree are: 8
Do you want to continue ,1 to continue,0 to exit:
0
*/