1212 * search(T key)
1313 * predecessor(T key)
1414 * successor(T key)
15- * findMax ()
16- * findMin ()
15+ * searchMin ()
16+ * searchMax ()
1717 * printInorder()
1818 * printPreorder()
1919 * printPostorder()
@@ -97,36 +97,46 @@ private Node<T, V> delete(Node<T, V> node, T key) {
9797 }
9898
9999 /**
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
103105 */
104- private Node <T , V > getMostLeft (Node <T , V > n ) {
106+ private Node <T , V > searchMin (Node <T , V > n ) {
105107 if (n .left == null ) {
106108 return n ;
107109 } else {
108- return getMostLeft (n .left );
110+ return searchMin (n .left );
109111 }
110112 }
111113
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 ) {
113122 if (n .right == null ) {
114123 return n ;
115124 } else {
116- return getMostRight (n .right );
125+ return searchMax (n .right );
117126 }
118127 }
119128
120129 /**
121130 * Find the key of the predecessor of a specified node that exists in the tree
122131 * NOTE: the input node is assumed to be in the tree
132+ *
123133 * @param node node that exists in the tree
124134 * @return key value; null if node has no predecessor
125135 */
126136 private T predecessor (Node <T , V > node ) {
127137 Node <T , V > curr = node ;
128138 if (curr .left != null ) { // predecessor in children
129- return getMostRight (curr .left ).key ;
139+ return searchMax (curr .left ).key ;
130140 } else { // predecessor in ancestor
131141 while (curr != null ) {
132142 if (curr .key .compareTo (node .key ) < 0 ) {
@@ -147,7 +157,7 @@ private T predecessor(Node<T, V> node) {
147157 private T successor (Node <T , V > node ) {
148158 Node <T , V > curr = node ;
149159 if (curr .right != null ) { // successor in children
150- return getMostLeft (curr .right ).key ;
160+ return searchMin (curr .right ).key ;
151161 } else { // successor in ancestor
152162 while (curr != null ) {
153163 if (curr .key .compareTo (node .key ) > 0 ) { // finds the cloests
@@ -305,7 +315,7 @@ public T predecessor(T key) {
305315 }
306316 }
307317
308- return predecessor (curr ); // pred could be an ancestor or child of curr node and hence handled separately
318+ return predecessor (curr );
309319 }
310320
311321 /**
@@ -325,39 +335,53 @@ public T successor(T key) {
325335 }
326336 }
327337
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+ }
329352 }
330353
331354 /**
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
333358 */
359+ public Node <T , V > searchMax () {
360+ if (root == null ) {
361+ return null ;
362+ } else {
363+ return searchMax (root );
364+ }
365+ }
366+
334367 public void printInorder () {
335368 System .out .print ("In-order: " );
336369 printInorder (root );
337370 System .out .println ();
338371 }
339372
340- /**
341- * prints pre-order traversal of the entire tree
342- */
343373 public void printPreorder () {
344374 System .out .print ("Pre-order: " );
345375 printPreorder (root );
346376 System .out .println ();
347377 }
348378
349- /**
350- * prints post-order traversal of the entire tree
351- */
352379 public void printPostorder () {
353380 System .out .print ("Post-order: " );
354381 printPostorder (root );
355382 System .out .println ();
356383 }
357384
358- /**
359- * prints level-order traversal of the entire tree
360- */
361385 public void printLevelorder () {
362386 System .out .print ("Level-order: " );
363387 printLevelorder (root );
0 commit comments