-
Notifications
You must be signed in to change notification settings - Fork 4
/
BinarySearchTree.java
492 lines (442 loc) · 14.9 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
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
package tree.bst;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import lombok.Getter;
/**
* Binary search tree (BST) implementation along with utility functions for performing various operations on BST.
* @author Suyash
*/
public class BinarySearchTree {
/**
* <code>root</code> node of this BST.
*/
@Getter
public Node root;
private Node prev=null;
private List<Integer> inorderList = new ArrayList<Integer>();
/**
* Inserts a node in BST using iterative approach.
* @param data - data to be inserted.
*/
public void insertIterative(Integer data){
Node newNode = new Node(data);
// If root is null, it indicates tree is empty.
if(root==null){
root = newNode;
return;
}
Node temp = root;
while(true) {
// If data to be inserted is less than current node's data, Insert this new node in left subtree.
if(data < temp.data) {
if(temp.left != null) {
temp = temp.left;
} else {
temp.left = newNode;
break;
}
}
// If data to be inserted is greater than current node's data, Insert this new node in right subtree.
else if(data > temp.data) {
if(temp.right != null) {
temp = temp.right;
} else {
temp.right = newNode;
break;
}
}
}
}
/**
* Inserts a node in BST using recursive approach.
* @param data - data to be inserted.
* @param current - <code>root</code> node.
*/
public Node insertRecursive(Integer data, Node current){
if(current==null) {
Node newNode = new Node(data);
current = newNode;
} else if(data <= current.data) { // If data to be inserted is less than current node's data, Insert this new node in left subtree.
current.left = insertRecursive(data, current.left);
} else if(data > current.data) { // If data to be inserted is greater than current node's data, Insert this new node in right subtree.
current.right = insertRecursive(data, current.right);
}
return current;
}
/**
* <i>Recursively</i> checks if data a node containing <code>data</code> is present in this BST.
* @param data - Data to be searched.
* @return - <code>true</code> if data is found, <code>false</code> otherwise.
*/
public boolean searchIterative(int data) {
Node temp = root;
while(temp!=null) {
if (data == temp.data){ // Data found.
return true;
} else if (data < temp.data) { // If data being searched is less than current node's data then search in left subtree.
temp = temp.left;
} else if (data > temp.data) { // If data being searched is greater than current node's data then search in right subtree.
temp = temp.right;
} else {
return false;
}
}
return false;
}
/**
* <i>Iteratively</i> checks if data a node containing <code>data</code> is present in this BST.
* @param data - Data to be searched.
* @return - <code>true</code> if data is found, <code>false</code> otherwise.
*/
public boolean searchRecursive(Node current, int data) {
if(current==null) { // Data not found.
return false;
}
if(data==current.data) { // Data found.
return true;
} else if(data < current.data) { // If data being searched is less than current node's data then search in left subtree.
return searchRecursive(current.left, data);
} else { // if(data > current.data)
return searchRecursive(current.right, data); // If data being searched is greater than current node's data then search in right subtree.
}
}
/**
* <b>Category : </b>BFS (Breadth-First-Search).<br/>
* <p>All the node at a level are traversed before moving to next level. In other words, Nodes
* will be traversed level-by-level. </p>
*/
public void levelOrderTraversal() {
Queue<Node> q = new ArrayDeque<Node>();
q.add(root);
while(q.size()>0) {
Node node = q.poll();
System.out.print(node.data+" ");
if(node.left != null) {
q.add(node.left);
}
if(node.right != null) {
q.add(node.right);
}
}
}
/**
* <b>Category : </b>DFS (Depth-First-Search).<br/>
* <p>Traversal order : <b>Root --> Left subtree --> Right-subtree</b> </p>
* @param current - <code>root</code> node.<br/>
*/
public void preOrderTraversal(Node current) {
if(current==null)
return;
System.out.print(current.data+" ");
preOrderTraversal(current.left);
preOrderTraversal(current.right);
}
/**
* <b>Category : </b>DFS (Depth-First-Search).<br/>
* <p>Traversal order : <b>Left subtree --> Root --> Right-subtree</b> </p>
* <b>Note : </b> All the nodes will be traversed in a sorted order.
* @param current - <code>root</code> node.<br/>
*/
public void inOrderTraversal(Node current) {
if(current==null)
return;
inOrderTraversal(current.left);
System.out.print(current.data+" ");
inOrderTraversal(current.right);
}
/**
* <b>Category : </b>DFS (Depth-First-Search).<br/>
* <p>Traversal order : <b>Left subtree --> Right-subtree --> Root</b> </p>
* @param current - <code>root</code> node.<br/>
*/
public void postOrderTraversal(Node current) {
if(current==null)
return;
postOrderTraversal(current.left);
postOrderTraversal(current.right);
System.out.print(current.data+" ");
}
/**
* This method finds out the minimum element using <i>iterative</i> approach.
* @return Minimum element.
*/
public Integer findMinIterative() {
Node temp = root;
if(temp==null) {
throw new RuntimeException("Tree is empty.");
} else {
while(temp.left!=null) {
temp=temp.left;
}
return temp.data;
}
}
/**
* This method finds out the minimum element using <i>recursive</i> approach.
* @param current - <code>root</code> node.<br/>
* @return Minimum element.
*/
public Integer findMinRecursive(Node current) {
if(current==null){
throw new RuntimeException("Tree is empty.");
} else {
if(current.left==null) {
return current.data;
} else {
return findMinRecursive(current.left);
}
}
}
/**
* This method finds out the maximum element using <i>iterative</i> approach.
* @return Maximum element.
*/
public Integer findMaxIterative() {
Node temp = root;
if(temp==null) {
throw new RuntimeException("Tree is empty.");
} else {
while(temp.right!=null) {
temp=temp.right;
}
return temp.data;
}
}
/**
* This method finds out the maximum element using <i>recursive</i> approach.
* @param current - <code>root</code> node.<br/>
* @return Maximum element.
*/
public Integer findMaxRecursive(Node current) {
if(current==null){
throw new RuntimeException("Tree is empty.");
} else {
if(current.right==null) {
return current.data;
} else {
return findMaxRecursive(current.right);
}
}
}
/**
* Calculates the height/maximum depth of tree <i>recursively</i>.
* @param current <code>root</code> node.
* @return Height of tree. i.e. height of <code>root</code> node. This is also equal to the maximum <i>depth</i> of the tree.
*/
public Integer findHeightRecursive(Node current) {
if(current==null) {
return -1; // Height of root nodes is 0;
}
// Calculate height of left subtree.
int leftHeight = findHeightRecursive(current.left);
// Calculate height of right subtree.
int rightHeight = findHeightRecursive(current.right);
// Height of node = Maximum of leftHeight(Height of it's left subtree) & rightHeight(Height of it's right subtree).
int max = Math.max(leftHeight, rightHeight);
return max + 1;
}
/**
* Calculates the height/maximum depth of tree <i>iteratively</i>.
* @return Height of tree. i.e. height of <code>root</code> node. This is also equal to the maximum <i>depth</i> of the tree.
*/
public Integer findHeightIterative() {
Queue<Node> q = new ArrayDeque<Node>();
// Enqueue Root and initialize height.
q.add(root);
int height=0, size;
// size (queue size) indicates number of nodes at current level.
while(true) {
size = q.size();
if(size==0)
return height-1;
else {
height++;
// Dequeue all nodes of current level and Enqueue all nodes of next level
while(size>0) {
Node node = q.poll(); // Retrieve and removes an element from the head/front of this queue.
if(node.left != null) {
q.add(node.left); // Enqueue the left node at next level, it is not null.
}
if(node.right != null) {
q.add(node.right); // Enqueue the right node at next level, it is not null.
}
size--;
}
}
}
}
/**
* @return <code>true</code> if this is BST, <code>false</code> otherwise.
*/
public boolean isBST() {
return isBstUtill(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
/**
*
* @param current - <code>root</code> node.
* @param minVal - <code>java.lang.Integer.MIN_VALUE</code>
* @param maxVal - <code>java.lang.Integer.MAX_VALUE</code>
* @return <code>true</code> if data inside all nodes lies in permissible range. <code>false</code> otherwise.
*/
private boolean isBstUtill(Node current, int minVal, int maxVal) {
if(current==null) {
return true;
}
if(current.data <= maxVal &&
current.data > minVal &&
isBstUtill(current.left, minVal, current.data) &&
isBstUtill(current.right, current.data, maxVal) ) {
return true;
} else{
return false;
}
}
/**
* @return <code>true</code> if this is BST, <code>false</code> otherwise.
*/
public boolean isBSTUsingInorderWithExtraArray() {
inorderTraversalList(root);
for(int i=0; i<inorderList.size()-1; i++){
if(inorderList.get(i) > inorderList.get(i+1)){
return false;
}
}
return true;
}
/**
* Performs in-order traversal and add each node in a <code>java.util.List</code>.
* @param current - <code>root</code> node.
*/
private void inorderTraversalList(Node current) {
if(current==null)
return;
inorderTraversalList(current.left);
inorderList.add(current.data);
inorderTraversalList(current.right);
}
/**
* @return <code>true</code> if this is BST, <code>false</code> otherwise.
*/
public boolean isBSTUsingInorder() {
return isBSTUtil(root);
}
/**
* <p>If using in-order traversal nodes are visited in sorted order then underlying tree is a BST.</p>
* Performs in-order traversal and keep track of previous node traversed.
* <ul>
* <li>if previous node is <code>lesser</code> than the current node then we are visiting the nodes in sorted order. Continue with traversal.</li>
* <li>But if previous node is <code>greater</code> than the current node then we are visiting the nodes in sorted order. exit and return <code>false</code> </li>
* </ul>
* @param current - <code>root</code> node.
* @return <code>true</code> if this is BST, <code>false</code> otherwise.
*/
private boolean isBSTUtil(Node current) {
if(current==null)
return true;
if(!isBSTUtil(current.left))
return false;
if(prev != null && current.data <= prev.data) {
return false;
}
prev = current;
return isBSTUtil(current.right);
}
/**
* Deletes a node from BST.
* @param current - <code>root</code> node.
* @param data - Data to be deleted.
* @return - <code>root</code> node, as <code>root</code> might change after deletion.
*/
public Node delete(Node current, int data) {
if(current==null) return current;
// If data in a node to be deleted is lesser than data in current node then target node is in left sub-tree.
else if(data < current.data) {
current.left = delete(current.left, data);
}
// If data in a node to be deleted is greater than data in current node then target node is in right sub-tree.
else if(data > current.data) {
current.right = delete(current.right, data);
}
// If data in a node to be deleted is equal to data in current node then, we found the target node,
// Now go ahead and delete it.
else { // data == current.data
// node with only one child or no child
if(current.left == null) {
current = current.right;
}
else if(current.right == null) {
current = current.left;
}
// node with 2 children
else {
// There are following 2 approaches-
// Approach - 1 : Through in-order successor.
// Find In-order successor, i.e. min. element from right subtree and copy it's data to current node's data.
// Then delete that in-order successor.
// Since In-order successor is a min. element in a sub-tree so In-order successor will never have left child though it may or may not have right child.
// Clearly in-order successor will have either 0 or 1 child, Hence deleting in-order successor is either Case-1 or Case-2.
// Approach - 2 : Through in-order predecessor.
// We could also find In-order predecessor, i.e. max. element from left subtree and copy it' data to current node's data.
// Then delete that in-order predecessor.
// Since In-order predecessor is a max. element in a sub-tree so In-order predecessor will never have right child though it may or may not have left child.
// Clearly in-order predecessor will have either 0 or 1 child, Hence deleting in-order predecessor is either Case-1 or Case-2.
// Here we'll go with Approach-1.
int inOrderSuccessorData = findMinRecursive(current.right); // Finding in-order successor.
current.data = inOrderSuccessorData; // Copying the data of in-order successor to current node.
current.right = delete(current.right, inOrderSuccessorData); // deleting the in-order successor.
}
}
return current;
}
public Node findInOrderSuccessor(Node rootNode, int data) {
if(rootNode==null) return null;
Node targetNode = find(data);
if(targetNode.right!=null) { // Case-1 : Current node has right subtree.
return findMin(targetNode.right);
} else { // Case-2 : Current node doesn't have right subtree.
Node ancestor = root;
Node successor = null;
while(ancestor != targetNode) {
if(targetNode.data < ancestor.data) {
successor = ancestor;
ancestor = ancestor.left;
} else {
ancestor = ancestor.right;
}
}
return successor;
}
}
private Node find(int data) {
Node temp = root;
while(temp!=null) {
if(data < temp.data) {
temp = temp.left;
} else if(data > temp.data) {
temp = temp.right;
} else {
return temp;
}
}
return null;
}
private Node findMin(Node current) {
if(current==null) return null;
while(current.left!=null) {
current = current.left;
}
return current;
}
@Getter
static class Node{
private Integer data;
private Node left;
private Node right;
public Node(Integer data) {
this.data = data;
left = right = null;
}
}
}