-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
257 lines (241 loc) · 5.67 KB
/
BinarySearchTree.java
File metadata and controls
257 lines (241 loc) · 5.67 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
import java.util.Scanner;
class Node
{
int data;
Node left, right;
public Node(int d)
{
data = d;
left = right = null;
}
}
class BST
{
Node root;
public void insert(int data)
{
Node newNode = new Node(data);
if (root == null)
{
root = newNode;
}
else
{
whereToInsert(root, newNode);
}
}
public void whereToInsert(Node current, Node newNode)
{
if (newNode.data < current.data)
{
if (current.left == null)
{
current.left = newNode;
}
else
{
whereToInsert(current.left, newNode);
}
}
else
{
if (current.right == null)
{
current.right = newNode;
}
else
{
whereToInsert(current.right, newNode);
}
}
}
public void preOrderTraversal(Node node)
{
if (node != null)
{
System.out.print(node.data + " ");
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}
public void inOrderTraversal(Node node)
{
if (node != null)
{
inOrderTraversal(node.left);
System.out.print(node.data + " ");
inOrderTraversal(node.right);
}
}
public void postOrderTraversal(Node node)
{
if (node != null)
{
postOrderTraversal(node.left);
postOrderTraversal(node.right);
System.out.print(node.data + " ");
}
}
int flag;
public void Search(Node current, int key)
{
flag = 0;
if (current == null)
{
return;
}
if (current.data == key)
{
flag = 1;
return;
}
else if (key < current.data)
{
Search(current.left, key);
}
else if (key > current.data)
{
Search(current.right, key);
}
}
public int minValue()
{
if (root == null)
{
System.out.println("Tree is empty");
return -1;
}
Node current = root;
while (current.left != null)
{
current = current.left;
}
return current.data;
}
public int maxValue()
{
if (root == null)
{
System.out.println("Tree is empty");
return -1;
}
Node current = root;
while (current.right != null)
{
current = current.right;
}
return current.data;
}
public Node Delete(Node root, int del)
{
if (root == null)
{
return root;
}
if (del < root.data)
{
root.left = Delete(root.left, del);
}
else if (del > root.data)
{
root.right = Delete(root.right, del);
}
else
{
if (root.left == null)
{
return root.right;
}
else if (root.right == null)
{
return root.left;
}
root.data = minValue(root.right);
root.right = Delete(root.right, root.data);
}
return root;
}
public int minValue(Node node)
{
int minValue = node.data;
while (node.left != null)
{
minValue = node.left.data;
node = node.left;
}
return minValue;
}
}
public class BinarySearchTree
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
BST b = new BST();
int choice;
do
{
System.out.println("Menu : \n1. Insert \n2. Display \n3. Search Element \n4. Minimum Value \n5. Maximum Value \n6. Delete Element \n0. Exit");
choice = s.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter element: ");
int data = s.nextInt();
b.insert(data);
System.out.println("Element entered successfully !");
break;
case 2:
System.out.print("Preordered Traversal : ");
b.preOrderTraversal(b.root);
System.out.println();
System.out.print("Inordered Traversal : ");
b.inOrderTraversal(b.root);
System.out.println();
System.out.print("Postordered Traversal : ");
b.postOrderTraversal(b.root);
System.out.println();
break;
case 3:
System.out.print("Enter the element you want to search: ");
int key = s.nextInt();
b.Search(b.root, key);
if (b.flag == 1)
{
System.out.println("Element Found!");
}
else
{
System.out.println("Element Not Found!");
}
break;
case 4:
int min = b.minValue();
if (min != -1)
{
System.out.println("Minimum Value : " + min);
}
break;
case 5:
int max = b.maxValue();
if (max != -1)
{
System.out.println("Maximum Value : " + max);
}
break;
case 6:
System.out.print("Enter the element you want to delete: ");
int del = s.nextInt();
b.Delete(b.root, del);
System.out.println("Element deleted successfully!");
break;
case 0:
System.out.println("Program Ended !");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 0);
s.close();
}
}