-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.java
334 lines (304 loc) · 12.2 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
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
import java.util.Queue;
import java.util.LinkedList;
// the binary search tree node
class TreeNode{
int data;
TreeNode left;
TreeNode right;
// Creating a binary search tree node using constructor
// Time Complexity - O(1) , Space Complexity - O(1)
TreeNode(int x){
this.data = x;
this.left=null;
this.right=null;
}
}
public class BinarySearchTree {
// the root of BST
TreeNode root;
// queue to do level order traversal
Queue<TreeNode> q = new LinkedList<>();
// wrapper method to add a new node with value 'data' in BST
// Time Complexity - O(log n) [as it traverses the BST one level at a time] , Space Complexity - O(log n) [As recursion is used and it traverses tree level wise]
public void addNode(int data){
root = addNode(root,data);
}
// overloaded method to add a new node to BST
private TreeNode addNode(TreeNode currentNode,int data){
// if currentNode is null
if(currentNode==null){
// make a new node and assign it to currentNode
currentNode = new TreeNode(data);
}
// if data to be inserted has value less than or equal to currentNode, we move to left of currentNode so as to maintain the property of BST
else if(data<=currentNode.data){
currentNode.left = addNode(currentNode.left,data);
}
// if data to be inserted has value more than currentNode, we move to right of currentNode so as to maintain the property of BST
else{
currentNode.right = addNode(currentNode.right,data);
}
return currentNode;
}
// wrapper method for inOrderTraversal of BST - LEFT ROOT RIGHT
// Time Complexity - O(n) , Space Complexity - O(n) [As recursion is used, so internal stack is maintained]
public void inOrderTraversal(){
// if BST is empty
if(this.root==null){
System.out.println("Binary search Tree is empty, can't traverse");
return;
}
// else if BST is not empty
System.out.println("Inorder Traversal...");
inOrderTraversal(root);
System.out.println();
}
// overloaded method for inOrderTraversal of BST
private void inOrderTraversal(TreeNode currentNode){
if(currentNode==null){
return;
}
inOrderTraversal(currentNode.left);
System.out.print(currentNode.data+" ");
inOrderTraversal(currentNode.right);
}
// wrapper method for preOrderTraversal of BST - ROOT LEFT RIGHT
// Time Complexity - O(n) , Space Complexity - O(n) [As recursion is used, so internal stack is maintained]
public void preOrderTraversal(){
// if BST is empty
if(this.root==null){
System.out.println("Binary search Tree is empty, can't traverse");
return;
}
// if BST is not empty
System.out.println("PreOrder Traversal...");
preOrderTraversal(root);
System.out.println();
}
// overloaded method for preOrderTraversal of BST
private void preOrderTraversal(TreeNode currentNode){
if(currentNode==null){
return;
}
System.out.print(currentNode.data+" ");
preOrderTraversal(currentNode.left);
preOrderTraversal(currentNode.right);
}
// wrapper method for postOrderTraversal of BST - LEFT RIGHT ROOT
// Time Complexity - O(n) , Space Complexity - O(n) [As recursion is used, so internal stack is maintained]
public void postOrderTraversal(){
// if BST is empty
if(this.root==null){
System.out.println("Binary search Tree is empty, can't traverse");
return;
}
// if BST is not empty
System.out.println("PostOrder Traversal...");
postOrderTraversal(root);
System.out.println();
}
// overloaded method for postOrderTraversal of BST
private void postOrderTraversal(TreeNode currentNode){
if(currentNode==null){
return;
}
postOrderTraversal(currentNode.left);
postOrderTraversal(currentNode.right);
System.out.print(currentNode.data+" ");
}
// method to do a level order traversal on BST
// Time Complexity - O(n) , Space Complexity - O(n) [As queue is used]
public void levelOrderTraversal(){
// if BST is empty
if(root==null){
System.out.println("Binary Search Tree is empty, can't traverse");
return;
}
// else if BST is not empty:
// clear the queue first, so as to reove nodes from ay previous operation
q.clear();
// add the root to queue
TreeNode currentNode=root;
q.add(currentNode);
System.out.println("Level Order Traversal...");
while(q.size()!=0){
// dequeue
currentNode = q.remove();
// check if currentNode has left child, if yes, add it to queue
if(currentNode.left!=null){
q.add(currentNode.left);
}
// check if currentNode has right child, if yes, add it to queue
if(currentNode.right!=null){
q.add(currentNode.right);
}
// finally print the current node
System.out.print(currentNode.data+" ");
}
System.out.println();
}
// wrapper method to search for a node having value 'x' in BST
// Time Complexity - O(log n) [as it traverses the BST one level at a time] , Space Complexity - O(log n) [As recursion is used and it traverses tree level wise]
public void searchNode(int x){
// if BST is empty
if(root==null){
System.out.println("Binary Search Tree is empty, can't search given node");
return;
}
// if BST is not empty, search whether the node is present in BST or not
TreeNode foundNode = searchNode(root,x);
// if node is not in BST
if(foundNode==null){
System.out.println("Node with given value: "+x+" not found in binary search tree");
return;
}
// else if node found in BST
System.out.println("Node with given value: "+x+" found in binary search tree");
System.out.println("Data: "+foundNode.data);
}
// TO REVISE - VERY IMPORTANT
// overloaded method to search node in BST
private TreeNode searchNode(TreeNode currentNode, int x){
if(currentNode==null){
return null;
}
else if(currentNode.data == x){
return currentNode;
}
else if(x > currentNode.data){
currentNode = searchNode(currentNode.right,x);
}
else {
currentNode = searchNode(currentNode.left,x);
}
return currentNode;
}
// wrapper method to delete a node with given value 'x' from BST
// Time Complexity - O(log n) [as it traverses the BST one level at a time] , Space Complexity - O(log n) [As recursion is used and it traverses tree level wise]
public void deleteNode(int x){
// if BST is empty
if(root==null){
System.out.println("Binary Search Tree is empty, can't delete given node");
return;
}
// if BST is not empty , search if the nodeToDelete is even present in the BST or not
TreeNode nodeToDelete = searchNode(root,x);
// if node to delete is not present in BST
if(nodeToDelete==null){
System.out.println("Node with given value: "+x+" is not present in binary search tree, hence can't delete");
return;
}
// else if node to delete is present in BST
root = deleteNode(root,x);
System.out.println("Node with given data: "+x+" deleted successfully from binary search tree");
}
// TO REVISE - VERY IMPORTANT
// overloaded method to delete a node from BST
private TreeNode deleteNode(TreeNode currentNode, int x){
// if value to delete is smaller than currentNode's data
if(x < currentNode.data){
currentNode.left = deleteNode(currentNode.left,x);
}
// else if value to delete is bigger than currentNode's data
else if(x > currentNode.data){
currentNode.right = deleteNode(currentNode.right,x);
}
// else if the currentNode has the data to delete
else{
// if currentNode / nodeToDelete has 2 childrens
if(currentNode.left!=null && currentNode.right!=null){
// find the minimum element from th right subtree of currentNode
TreeNode minNodeRightSubtree = findMinFromRightSubtree(currentNode.right);
// copy data of minNodeRightSubtree to nodeToDelete / currentNode
currentNode.data = minNodeRightSubtree.data;
// finally delete the minNodeRightSubtree from the right subtree of currentNode
currentNode.right = deleteNode(currentNode.right, minNodeRightSubtree.data);
}
// if currentNode / nodeToDelete has only 1 children
// if it has only left child
else if(currentNode.left!=null && currentNode.right==null){
currentNode = currentNode.left;
}
// if it has only right child
else if(currentNode.right!=null && currentNode.left==null){
currentNode = currentNode.right;
}
// if currentNode / nodeToDelete has no children
else{
currentNode=null;
}
}
return currentNode;
}
// helper method to find the node with minimum value in right subtree
// Time Complexity - O(log n) , Space Complexity - O(1)
private TreeNode findMinFromRightSubtree(TreeNode currentNode){
while(currentNode.left!=null){
currentNode = currentNode.left;
}
return currentNode;
}
// method to delete the comple BST
// Time Complexity - O(1) , Space Complexity - O(1)
public void deleteBinarySearchTree(){
if(root==null){
System.out.println("Binary search tree doesn't exists, hence can't delete it");
return;
}
root = null;
System.out.println("Binary Search Tree deleted successfully");
}
public static void main(String[] args) throws Exception {
BinarySearchTree bst = new BinarySearchTree();
bst.addNode(100);
bst.addNode(80);
bst.addNode(200);
bst.addNode(70);
bst.addNode(90);
bst.addNode(150);
bst.addNode(300);
bst.addNode(50);
bst.addNode(160);
bst.addNode(400);
bst.addNode(40);
bst.addNode(60);
bst.addNode(155);
bst.addNode(170);
bst.addNode(407);
bst.inOrderTraversal();
bst.preOrderTraversal();
bst.postOrderTraversal();
bst.levelOrderTraversal();
bst.searchNode(4);
bst.searchNode(24);
bst.searchNode(170);
bst.deleteNode(3);
bst.levelOrderTraversal();
bst.deleteNode(100);
bst.levelOrderTraversal();
bst.deleteBinarySearchTree();
bst.deleteBinarySearchTree();
}
}
/* ========================================= OUTPUT ===================================
Inorder Traversal...
40 50 60 70 80 90 100 150 155 160 170 200 300 400 407
PreOrder Traversal...
100 80 70 50 40 60 90 200 150 160 155 170 300 400 407
PostOrder Traversal...
40 60 50 70 90 80 155 170 160 150 407 400 300 200 100
Level Order Traversal...
100 80 200 70 90 150 300 50 160 400 40 60 155 170 407
Node with given value: 4 not found in binary search tree
Node with given value: 24 not found in binary search tree
Node with given value: 170 found in binary search tree
Data: 170
Node with given value: 3 is not present in binary search tree, hence can't delete
Level Order Traversal...
100 80 200 70 90 150 300 50 160 400 40 60 155 170 407
Node with given data: 100 deleted successfully from binary search tree
Level Order Traversal...
150 80 200 70 90 160 300 50 155 170 400 40 60 407
Binary Search Tree deleted successfully
Binary search tree doesn't exists, hence can't delete it
===============================================================================================*/