-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinarySearchTree.java
239 lines (204 loc) · 4.99 KB
/
BinarySearchTree.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
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
import java.util.*;
class Node{
int data;
Node left;
Node right;
Node(int d){
data = d;
left = null;
right = null;
}
}
class BinarySearchTree{
static Node root;
void Insert(int id){
Node newNode = new Node(id);
if(root==null){
root = newNode;
return;
}
Node current = root;
Node parent = null;
while(true){
parent = current;
if(id<current.data){
current = current.left;
if(current==null){
parent.left = newNode;
return;
}
}else{
current = current.right;
if(current==null){
parent.right = newNode;
return;
}
}
}
}
boolean Find(int n){
Node current = root;
while(current!=null){
if(current.data==n){
return true;
}
else if(current.data>n)
current = current.left;
else
current = current.right;
}
return false;
}
Node deleteRec(Node root, int key)
{
if (root == null) return root;
if (key < root.data)
root.left = deleteRec(root.left, key);
else if (key > root.data)
root.right = deleteRec(root.right, key);
else
{
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
root.data = minValue(root.right);
root.right = deleteRec(root.right, root.data);
}
return root;
}
int minValue(Node root)
{
int minv = root.data;
while (root.left != null)
{
minv = root.left.data;
root = root.left;
}
return minv;
}
void Display(Node root){
if(root!=null){
Display(root.left);
System.out.print(" " + root.data);
Display(root.right);
}
}
void PreDisplay(Node root){
if(root!=null){
System.out.print(" " + root.data);
PreDisplay(root.left);
PreDisplay(root.right);
}
}
void PostDisplay(Node root){
if(root!=null){
PostDisplay(root.left);
PostDisplay(root.right);
System.out.print(" " + root.data);
}
}
void rightRotate(){
Node x = root.left;
Node y = x.right;
x.right = root;
root.left = y;
root = x;
}
void leftRotate(){
Node x = root.right;
Node y = x.left;
x.left = root;
root.right = y;
root = x;
}
void printLnode(Node node){
/*if(node == null)
return; [BOTH CODES ARE RUNNING SUCCESSFULLY]
if(node.left == null && node.right==null)
System.out.println(node.data);
printLnode(node.left);
printLnode(node.right); */
if(node == null)
return;
if(node.left!=null||node.right!=null){
printLnode(node.left);
printLnode(node.right);
}
else if(node.left==null && node.right==null){
System.out.println(node.data+" ");
}
}
public static void main(String[] args) {
BinarySearchTree obj = new BinarySearchTree();
Scanner ip = new Scanner(System.in);
int res,v;
System.out.println("Press 1 to insert a node in tree.");
System.out.println("Press 2 to search a node in tree.");
System.out.println("Press 3 to display tree in Inorder form .");
System.out.println("Press 4 to delete a node in tree.");
System.out.println("Press 5 to display tree in Preorder form.");
System.out.println("Press 6 to display tree in Postorder form.");
System.out.println("Press 7 to rotate the tree towards right.");
System.out.println("Press 8 to rotate the tree towards left.");
System.out.println("Press 9 to exit.");
System.out.println("Press 10 to print leaf node.");
while(true){
res = ip.nextInt();
switch(res){
case 1:
System.out.println("Enter its data : ");
obj.Insert(v=ip.nextInt());
break;
case 2:
System.out.println("Enter its data : ");
System.out.println(obj.Find(v=ip.nextInt()));
break;
case 3:
obj.Display(root);
break;
case 4:
System.out.println("Enter its data : ");
obj.deleteRec(root,v=ip.nextInt());
break;
case 5:
obj.PreDisplay(root);
break;
case 6:
obj.PostDisplay(root);
break;
case 7:
System.out.println("Preorder form of BST before right rotation : ");
System.out.println();
obj.PreDisplay(root);
System.out.println();
System.out.println();
obj.rightRotate();
System.out.println("Preorder form of BST after right rotation : ");
System.out.println();
obj.PreDisplay(root);
break;
case 8:
System.out.println("Preorder form of BST before left rotation : ");
System.out.println();
obj.PreDisplay(root);
System.out.println();
System.out.println();
obj.leftRotate();
System.out.println("Preorder form of BST after left rotation : ");
System.out.println();
obj.PreDisplay(root);
break;
case 9:
break;
case 10:
obj.printLnode(root);
break;
default:
System.out.println("Enter a valid option!");
}
if(res==9)
break;
}
}
}