12
12
* search(T key)
13
13
* predecessor(T key)
14
14
* successor(T key)
15
- * findMax ()
16
- * findMin ()
15
+ * searchMin ()
16
+ * searchMax ()
17
17
* printInorder()
18
18
* printPreorder()
19
19
* printPostorder()
@@ -97,36 +97,46 @@ private Node<T, V> delete(Node<T, V> node, T key) {
97
97
}
98
98
99
99
/**
100
- * Find the left-most child of the (sub)tree rooted at a specified node
101
- * @param n tree is rooted at this node
102
- * @return left-most node
100
+ * Find the node with the minimum key in the tree rooted at a specified node.
101
+ *
102
+ * @param n the (sub)tree rooted at node which the minimum key will be searched for
103
+ * @return node with the minimum key
104
+ * NOTE: ASSUMPTION THAT TREE IS NOT EMPTY
103
105
*/
104
- private Node <T , V > getMostLeft (Node <T , V > n ) {
106
+ private Node <T , V > searchMin (Node <T , V > n ) {
105
107
if (n .left == null ) {
106
108
return n ;
107
109
} else {
108
- return getMostLeft (n .left );
110
+ return searchMin (n .left );
109
111
}
110
112
}
111
113
112
- private Node <T , V > getMostRight (Node <T , V > n ) {
114
+ /**
115
+ * Find the node with the maximum key in the tree rooted at a specified node.
116
+ *
117
+ * @param n the (sub)tree rooted at node which the maximum key will be searched for
118
+ * @return node with the maximum key
119
+ * NOTE: ASSUMPTION THAT TREE IS NOT EMPTY
120
+ */
121
+ private Node <T , V > searchMax (Node <T , V > n ) {
113
122
if (n .right == null ) {
114
123
return n ;
115
124
} else {
116
- return getMostRight (n .right );
125
+ return searchMax (n .right );
117
126
}
118
127
}
119
128
120
129
/**
121
130
* Find the key of the predecessor of a specified node that exists in the tree
122
131
* NOTE: the input node is assumed to be in the tree
132
+ *
123
133
* @param node node that exists in the tree
124
134
* @return key value; null if node has no predecessor
125
135
*/
126
136
private T predecessor (Node <T , V > node ) {
127
137
Node <T , V > curr = node ;
128
138
if (curr .left != null ) { // predecessor in children
129
- return getMostRight (curr .left ).key ;
139
+ return searchMax (curr .left ).key ;
130
140
} else { // predecessor in ancestor
131
141
while (curr != null ) {
132
142
if (curr .key .compareTo (node .key ) < 0 ) {
@@ -147,7 +157,7 @@ private T predecessor(Node<T, V> node) {
147
157
private T successor (Node <T , V > node ) {
148
158
Node <T , V > curr = node ;
149
159
if (curr .right != null ) { // successor in children
150
- return getMostLeft (curr .right ).key ;
160
+ return searchMin (curr .right ).key ;
151
161
} else { // successor in ancestor
152
162
while (curr != null ) {
153
163
if (curr .key .compareTo (node .key ) > 0 ) { // finds the cloests
@@ -305,7 +315,7 @@ public T predecessor(T key) {
305
315
}
306
316
}
307
317
308
- return predecessor (curr ); // pred could be an ancestor or child of curr node and hence handled separately
318
+ return predecessor (curr );
309
319
}
310
320
311
321
/**
@@ -325,39 +335,53 @@ public T successor(T key) {
325
335
}
326
336
}
327
337
328
- return successor (curr ); // same exp as in the pred fn
338
+ return successor (curr );
339
+ }
340
+
341
+ /**
342
+ * Search for the minimum key in the tree.
343
+ *
344
+ * @return node with the minimum key; null if tree is empty
345
+ */
346
+ public Node <T , V > searchMin () {
347
+ if (root == null ) {
348
+ return null ;
349
+ } else {
350
+ return searchMin (root );
351
+ }
329
352
}
330
353
331
354
/**
332
- * prints in order traversal of the entire tree.
355
+ * Search for the maximum key in the tree.
356
+ *
357
+ * @return node with the maximum key; null if tree is empty
333
358
*/
359
+ public Node <T , V > searchMax () {
360
+ if (root == null ) {
361
+ return null ;
362
+ } else {
363
+ return searchMax (root );
364
+ }
365
+ }
366
+
334
367
public void printInorder () {
335
368
System .out .print ("In-order: " );
336
369
printInorder (root );
337
370
System .out .println ();
338
371
}
339
372
340
- /**
341
- * prints pre-order traversal of the entire tree
342
- */
343
373
public void printPreorder () {
344
374
System .out .print ("Pre-order: " );
345
375
printPreorder (root );
346
376
System .out .println ();
347
377
}
348
378
349
- /**
350
- * prints post-order traversal of the entire tree
351
- */
352
379
public void printPostorder () {
353
380
System .out .print ("Post-order: " );
354
381
printPostorder (root );
355
382
System .out .println ();
356
383
}
357
384
358
- /**
359
- * prints level-order traversal of the entire tree
360
- */
361
385
public void printLevelorder () {
362
386
System .out .print ("Level-order: " );
363
387
printLevelorder (root );
0 commit comments