-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tnode.java
139 lines (127 loc) · 3.91 KB
/
Tnode.java
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
import java.util.Scanner;
class Tnode {
int data;
Tnode left;
Tnode right;
int lbit, rbit;
public Tnode(int data) {
this.data = data;
this.left = null;
this.right = null;
this.lbit = 0;
this.rbit = 0;
}
}
class TBT {
Tnode root, head;
public TBT() {
root = null;
head = null;
}
void createTBT(int size, int rootData) {
root = new Tnode(rootData);
head = new Tnode(-9999);
head.left = root;
head.right = head;
head.lbit = 1;
head.rbit = 0;
root.left = head;
root.right = head;
Scanner scanner = new Scanner(System.in);
for (int i = 1; i < size; i++) {
System.out.println("Enter new node : ");
int data = scanner.nextInt();
Tnode temp = new Tnode(data);
Tnode current = root;
while (true) {
if (data < current.data) {
if (current.lbit == 0) {
temp.left = current.left;
temp.right = current;
current.left = temp;
current.lbit = 1;
break;
} else {
current = current.left;
}
} else {
if (current.rbit == 0) {
temp.left = current;
temp.right = current.right;
current.right = temp;
current.rbit = 1;
break;
} else {
current = current.right;
}
}
}
}
}
void preorder(Tnode node) {
if (node != null) {
System.out.print(node.data + " ");
if (node.lbit == 1) {
preorder(node.left);
} else {
System.out.print("NULL ");
}
if (node.rbit == 1) {
preorder(node.right);
} else {
System.out.print("NULL ");
}
}
}
void inorder(Tnode node) {
if (node != null) {
if (node.lbit == 1) {
inorder(node.left);
} else {
System.out.print("NULL ");
}
System.out.print(node.data + " ");
if (node.rbit == 1) {
inorder(node.right);
} else {
System.out.print("NULL ");
}
}
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of nodes (including root) : ");
int size = scanner.nextInt();
System.out.print("Enter root node : ");
int rootData = scanner.nextInt();
TBT T = new TBT();
T.createTBT(size, rootData);
int ch;
do {
System.out.println("\n1. Inorder Display of Threaded Binary Tree ");
System.out.println("2. Preorder Display of Threaded Binary Tree ");
System.out.println("3. Exit ");
System.out.print("Enter your choice : ");
ch = scanner.nextInt();
switch (ch) {
case 1:
System.out.print("Inorder Traversal: ");
T.inorder(T.root); // Pass root for traversal
System.out.println("\n");
break;
case 2:
System.out.print("Preorder Traversal: ");
T.preorder(T.root); // Pass root for traversal
System.out.println("\n");
break;
case 3:
break;
default:
System.out.println("Enter correct choice");
break;
}
} while (ch != 3);
}
}