-
Notifications
You must be signed in to change notification settings - Fork 18
/
BinarySearchTree.java
399 lines (370 loc) · 8.31 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
/**
* Copyright 2022 jingedawang
*/
package container;
import utils.ArrayGenerator;
import utils.ArrayPrinter;
import utils.TreePrinter;
/**
* Binary search tree.
*/
public class BinarySearchTree extends AbstractTree implements SearchTree, BinaryTree {
/**
* Test code.
*/
public static void main(String[] args) {
int[] arr = ArrayGenerator.fixedArray();
// int[] arr = ArrayGenerator.randomArray(20, 20);
ArrayPrinter.print(arr);
BinarySearchTree tree = new BinarySearchTree(arr);
TreePrinter.print(tree);
Node searchResult = tree.search(arr[4]);
System.out.println("The 4-th value of arr is " + searchResult.value);
Node minimum = tree.minimum();
System.out.println("The minimum value of the tree is " + minimum.value);
Node successor = tree.successor(minimum);
System.out.println("The successor of the minimum is " + successor.value);
tree.delete(tree.root);
TreePrinter.print(tree);
}
/**
* Default constructor.
*/
public BinarySearchTree() {
root = nil;
}
/**
* Constructor with underlying values.
* <p>
* This constructor will construct the binary search tree according to the order of the input array.
*
* @param values The values used to constructing the tree.
*/
public BinarySearchTree(int[] values) {
root = nil;
for (int value : values) {
insert(new Node(value, nil, nil));
}
}
/**
* Find a node with value k.
*
* @param k The value searched in this tree.
* @return The node with value k.
*/
@Override
public Node search(int k) {
return search(root, k);
}
/**
* Find a node with value k in the subtree specified by {@code root}.
*
* @param root The root node of the subtree.
* @param k The value searched in this tree.
* @return The node with value k.
*/
@Override
public Node search(Node root, int k) {
while (root != nil && k != root.value) {
if (k < root.value) {
root = root.left;
} else {
root = root.right;
}
}
return root;
}
/**
* Find the node with minimum value in the tree.
*
* @return The node with minimum value in the tree.
*/
@Override
public Node minimum() {
return minimum(root);
}
/**
* Find the node with minimum value in the subtree specified by {@code root}.
*
* @param root The root node of the subtree.
* @return The node with minimum value in the tree.
*/
@Override
public Node minimum(Node root) {
while (root.left != nil) {
root = root.left;
}
return root;
}
/**
* Find the node with maximum value in the tree.
*
* @return The node with maximum value in the tree.
*/
@Override
public Node maximum() {
return maximum(root);
}
/**
* Find the node with maximum value in the subtree specified by @{code root}.
*
* @param root The root node of the subtree.
* @return The node with maximum value in the tree.
*/
@Override
public Node maximum(Node root) {
while (root.right != nil) {
root = root.right;
}
return root;
}
/**
* Find the real predecessor of the given node.
* <p>
* The node found must have different value with the given node.
*
* @param node The node whose predecessor will be found.
* @return The predecessor node of the given node.
*/
@Override
public Node predecessor(Node node) {
Node predecessor = predecessorNode(node);
while (predecessor != nil && predecessor.value == node.value) {
predecessor = predecessorNode(predecessor);
}
return predecessor;
}
/**
* Find the predecessor node of the given node.
*
* @param node The node whose predecessor will be found.
* @return The predecessor node of the given node.
*/
public Node predecessorNode(Node node) {
if (node.left != nil) {
return maximum(node.left);
}
Node parent = node.parent;
while (parent != nil && node == parent.left) {
node = parent;
parent = parent.parent;
}
return parent;
}
/**
* Find the real successor of the given node.
* <p>
* The node found must have different value with the given node.
*
* @param node The node whose successor will be found.
* @return The successor node of the given node.
*/
@Override
public Node successor(Node node) {
Node successor = successorNode(node);
while (successor != nil && successor.value == node.value) {
successor = successorNode(successor);
}
return successor;
}
/**
* Find the successor node of the given node.
*
* @param node The node whose successor will be found.
* @return The successor node of the given node.
*/
public Node successorNode(Node node) {
if (node.right != nil) {
return minimum(node.right);
}
Node parent = node.parent;
while (parent != nil && node == parent.right) {
node = parent;
parent = parent.parent;
}
return parent;
}
/**
* Insert a node into the tree.
*
* @param newNode The node to be inserted.
*/
@Override
public void insert(Node newNode) {
Node parent = nil;
Node node = root;
while (node != nil) {
parent = node;
if (newNode.value < node.value) {
node = node.left;
} else {
node = node.right;
}
}
newNode.parent = parent;
if (parent == nil) {
root = newNode;
} else if (newNode.value < parent.value) {
parent.left = newNode;
} else {
parent.right = newNode;
}
}
/**
* Delete a node from the tree.
*
* @param node The node to be deleted.
*/
@Override
public void delete(Node node) {
if (node.left == nil) {
transplant(node.right, node);
} else if (node.right == nil) {
transplant(node.left, node);
} else {
Node y = minimum(node.right);
if (y.parent != node) {
transplant(y.right, y);
y.right = node.right;
y.right.parent = y;
}
transplant(y, node);
y.left = node.left;
y.left.parent = y;
}
}
/**
* Get the nil node.
*
* @return The nil node.
*/
public Node getNil() {
return nil;
}
/**
* Remove nil sentinel and return as a binary tree.
*
* @return A binary tree without nil.
*/
public BinaryTree toBinaryTree() {
BinarySearchTree tree = clone();
if (tree.root == nil) {
tree.root = null;
}
else {
removeNil(tree.root);
}
return tree;
}
/**
* Check if the tree is empty.
*
* @return {@code true} if the tree has no elements, {@code false} otherwise.
*/
@Override
public boolean empty() {
return root == null || root == nil;
}
/**
* Get the size of the tree.
*
* @return The size of the tree.
*/
@Override
public int size() {
if (root == null || root == nil) {
return 0;
}
return subtreeSize(root);
}
/**
* Get the size of the subtree.
*
* @param root The root of the subtree.
* @return The size of the subtree.
*/
@Override
protected int subtreeSize(Node root) {
if (root == null || root == nil) {
return 0;
}
int size = 1;
size += subtreeSize(root.left);
size += subtreeSize(root.right);
return size;
}
/**
* Clone this tree.
*
* @return A copy of this tree.
*/
@Override
protected BinarySearchTree clone() {
nil.parent = null;
nil.left = null;
nil.right = null;
BinarySearchTree tree = (BinarySearchTree) super.clone();
if (tree.root.left == null && tree.root.right == null) {
tree.root = nil;
} else {
useNil(tree.root);
}
return tree;
}
/**
* Substitute the target tree by the source tree.
*
* @param source The root node of the source tree.
* @param target The root node of the target tree.
*/
protected void transplant(Node source, Node target) {
if (target.parent == nil) {
root = source;
} else if (target == target.parent.left) {
target.parent.left = source;
} else {
target.parent.right = source;
}
source.parent = target.parent;
}
/**
* A sentinel node indicting all the external nodes.
*/
protected final Node nil = new Node(Node.Color.BLACK);
/**
* Use nil sentinel instead of extra leaf nodes.
* <p>
* This method is used to fix up the cloned tree.
*
* @param node The root of the current subtree.
*/
private void useNil(Node node) {
if (node.left.parent == null) {
node.left = nil;
} else {
useNil(node.left);
}
if (node.right.parent == null) {
node.right = nil;
} else {
useNil(node.right);
}
}
/**
* Remove nil sentinel recursively.
*
* @param node The root node of current subtree.
*/
private void removeNil(Node node) {
if (node.left == nil) {
node.left = null;
} else {
removeNil(node.left);
}
if (node.right == nil) {
node.right = null;
} else {
removeNil(node.right);
}
}
}