-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
BinarySearchTree.java
360 lines (292 loc) · 10.4 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
/**
* This file contains an implementation of a Binary Search Tree (BST) Any comparable data is allowed
* within this tree (numbers, strings, comparable Objects, etc...). Supported operations include
* adding, removing, height, and containment checks. Furthermore, multiple tree traversal Iterators
* are provided including: 1) Preorder traversal 2) Inorder traversal 3) Postorder traversal 4)
* Levelorder traversal
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.datastructures.binarysearchtree;
public class BinarySearchTree<T extends Comparable<T>> {
// Tracks the number of nodes in this BST
private int nodeCount = 0;
// This BST is a rooted tree so we maintain a handle on the root node
private Node root = null;
// Internal node containing node references
// and the actual node data
private class Node {
T data;
Node left, right;
public Node(Node left, Node right, T elem) {
this.data = elem;
this.left = left;
this.right = right;
}
}
// Check if this binary tree is empty
public boolean isEmpty() {
return size() == 0;
}
// Get the number of nodes in this binary tree
public int size() {
return nodeCount;
}
// Add an element to this binary tree. Returns true
// if we successfully perform an insertion
public boolean add(T elem) {
// Check if the value already exists in this
// binary tree, if it does ignore adding it
if (contains(elem)) {
return false;
// Otherwise add this element to the binary tree
} else {
root = add(root, elem);
nodeCount++;
return true;
}
}
// Private method to recursively add a value in the binary tree
private Node add(Node node, T elem) {
// Base case: found a leaf node
if (node == null) {
node = new Node(null, null, elem);
} else {
// Pick a subtree to insert element
if (elem.compareTo(node.data) < 0) {
node.left = add(node.left, elem);
} else {
node.right = add(node.right, elem);
}
}
return node;
}
// Remove a value from this binary tree if it exists, O(n)
public boolean remove(T elem) {
// Make sure the node we want to remove
// actually exists before we remove it
if (contains(elem)) {
root = remove(root, elem);
nodeCount--;
return true;
}
return false;
}
private Node remove(Node node, T elem) {
if (node == null) return null;
int cmp = elem.compareTo(node.data);
// Dig into left subtree, the value we're looking
// for is smaller than the current value
if (cmp < 0) {
node.left = remove(node.left, elem);
// Dig into right subtree, the value we're looking
// for is greater than the current value
} else if (cmp > 0) {
node.right = remove(node.right, elem);
// Found the node we wish to remove
} else {
// This is the case with only a right subtree or
// no subtree at all. In this situation just
// swap the node we wish to remove with its right child.
if (node.left == null) {
return node.right;
// This is the case with only a left subtree or
// no subtree at all. In this situation just
// swap the node we wish to remove with its left child.
} else if (node.right == null) {
return node.left;
// When removing a node from a binary tree with two links the
// successor of the node being removed can either be the largest
// value in the left subtree or the smallest value in the right
// subtree. In this implementation I have decided to find the
// smallest value in the right subtree which can be found by
// traversing as far left as possible in the right subtree.
} else {
// Find the leftmost node in the right subtree
Node tmp = findMin(node.right);
// Swap the data
node.data = tmp.data;
// Go into the right subtree and remove the leftmost node we
// found and swapped data with. This prevents us from having
// two nodes in our tree with the same value.
node.right = remove(node.right, tmp.data);
// If instead we wanted to find the largest node in the left
// subtree as opposed to smallest node in the right subtree
// here is what we would do:
// Node tmp = findMax(node.left);
// node.data = tmp.data;
// node.left = remove(node.left, tmp.data);
}
}
return node;
}
// Helper method to find the leftmost node (which has the smallest value)
private Node findMin(Node node) {
while (node.left != null) node = node.left;
return node;
}
// Helper method to find the rightmost node (which has the largest value)
private Node findMax(Node node) {
while (node.right != null) node = node.right;
return node;
}
// returns true is the element exists in the tree
public boolean contains(T elem) {
return contains(root, elem);
}
// private recursive method to find an element in the tree
private boolean contains(Node node, T elem) {
// Base case: reached bottom, value not found
if (node == null) return false;
int cmp = elem.compareTo(node.data);
// Dig into the left subtree because the value we're
// looking for is smaller than the current value
if (cmp < 0) return contains(node.left, elem);
// Dig into the right subtree because the value we're
// looking for is greater than the current value
else if (cmp > 0) return contains(node.right, elem);
// We found the value we were looking for
else return true;
}
// Computes the height of the tree, O(n)
public int height() {
return height(root);
}
// Recursive helper method to compute the height of the tree
private int height(Node node) {
if (node == null) return 0;
return Math.max(height(node.left), height(node.right)) + 1;
}
// This method returns an iterator for a given TreeTraversalOrder.
// The ways in which you can traverse the tree are in four different ways:
// preorder, inorder, postorder and levelorder.
public java.util.Iterator<T> traverse(TreeTraversalOrder order) {
switch (order) {
case PRE_ORDER:
return preOrderTraversal();
case IN_ORDER:
return inOrderTraversal();
case POST_ORDER:
return postOrderTraversal();
case LEVEL_ORDER:
return levelOrderTraversal();
default:
return null;
}
}
// Returns as iterator to traverse the tree in pre order
private java.util.Iterator<T> preOrderTraversal() {
final int expectedNodeCount = nodeCount;
final java.util.Stack<Node> stack = new java.util.Stack<>();
stack.push(root);
return new java.util.Iterator<T>() {
@Override
public boolean hasNext() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
return root != null && !stack.isEmpty();
}
@Override
public T next() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
Node node = stack.pop();
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
return node.data;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
// Returns as iterator to traverse the tree in order
private java.util.Iterator<T> inOrderTraversal() {
final int expectedNodeCount = nodeCount;
final java.util.Stack<Node> stack = new java.util.Stack<>();
stack.push(root);
return new java.util.Iterator<T>() {
Node trav = root;
@Override
public boolean hasNext() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
return root != null && !stack.isEmpty();
}
@Override
public T next() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
// Dig left
while (trav != null && trav.left != null) {
stack.push(trav.left);
trav = trav.left;
}
Node node = stack.pop();
// Try moving down right once
if (node.right != null) {
stack.push(node.right);
trav = node.right;
}
return node.data;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
// Returns as iterator to traverse the tree in post order
private java.util.Iterator<T> postOrderTraversal() {
final int expectedNodeCount = nodeCount;
final java.util.Stack<Node> stack1 = new java.util.Stack<>();
final java.util.Stack<Node> stack2 = new java.util.Stack<>();
stack1.push(root);
while (!stack1.isEmpty()) {
Node node = stack1.pop();
if (node != null) {
stack2.push(node);
if (node.left != null) stack1.push(node.left);
if (node.right != null) stack1.push(node.right);
}
}
return new java.util.Iterator<T>() {
@Override
public boolean hasNext() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
return root != null && !stack2.isEmpty();
}
@Override
public T next() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
return stack2.pop().data;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
// Returns as iterator to traverse the tree in level order
private java.util.Iterator<T> levelOrderTraversal() {
final int expectedNodeCount = nodeCount;
final java.util.Queue<Node> queue = new java.util.LinkedList<>();
queue.offer(root);
return new java.util.Iterator<T>() {
@Override
public boolean hasNext() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
return root != null && !queue.isEmpty();
}
@Override
public T next() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
Node node = queue.poll();
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
return node.data;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}