@@ -53,6 +53,19 @@ private void insert(Node<T, V> node, T key, V value) {
5353 }
5454 }
5555
56+ /**
57+ * Inserts a key into the tree
58+ *
59+ * @param key to be inserted
60+ */
61+ public void insert (T key , V value ) {
62+ if (root == null ) {
63+ root = new Node <>(key , value );
64+ } else {
65+ insert (root , key , value );
66+ }
67+ }
68+
5669 /**
5770 * Delete a key from the binary search tree rooted at a specified node.
5871 * Find the node that holds the key and remove the node from the tree.
@@ -102,6 +115,15 @@ private Node<T, V> delete(Node<T, V> node, T key) {
102115 return node ;
103116 }
104117
118+ /**
119+ * Removes a key from the tree, if it exists
120+ *
121+ * @param key to be removed
122+ */
123+ public void delete (T key ) {
124+ root = delete (root , key );
125+ }
126+
105127 /**
106128 * Find the node with the minimum key in the tree rooted at a specified node.
107129 *
@@ -117,6 +139,19 @@ private Node<T, V> searchMin(Node<T, V> n) {
117139 }
118140 }
119141
142+ /**
143+ * Search for the minimum key in the tree.
144+ *
145+ * @return node with the minimum key; null if tree is empty
146+ */
147+ public Node <T , V > searchMin () {
148+ if (root == null ) {
149+ return null ;
150+ } else {
151+ return searchMin (root );
152+ }
153+ }
154+
120155 /**
121156 * Find the node with the maximum key in the tree rooted at a specified node.
122157 *
@@ -132,6 +167,19 @@ private Node<T, V> searchMax(Node<T, V> n) {
132167 }
133168 }
134169
170+ /**
171+ * Search for the maximum key in the tree.
172+ *
173+ * @return node with the maximum key; null if tree is empty
174+ */
175+ public Node <T , V > searchMax () {
176+ if (root == null ) {
177+ return null ;
178+ } else {
179+ return searchMax (root );
180+ }
181+ }
182+
135183 /**
136184 * Find the key of the predecessor of a specified node that exists in the tree
137185 * NOTE: the input node is assumed to be in the tree
@@ -154,6 +202,27 @@ private T predecessor(Node<T, V> node) {
154202 return null ;
155203 }
156204
205+ /**
206+ * Search for the predecessor of a given key.
207+ *
208+ * @param key find predecessor of this key
209+ * @return generic type value; null if key has no predecessor
210+ */
211+ public T predecessor (T key ) {
212+ Node <T , V > curr = root ;
213+ while (curr != null ) {
214+ if (curr .getKey ().compareTo (key ) == 0 ) {
215+ break ;
216+ } else if (curr .getKey ().compareTo (key ) < 0 ) {
217+ curr = curr .getRight ();
218+ } else {
219+ curr = curr .getLeft ();
220+ }
221+ }
222+
223+ return predecessor (curr );
224+ }
225+
157226 /**
158227 * Find the key of the successor of a specified node that exists in the tree
159228 * NOTE: the input node is assumed to be in the tree
@@ -176,6 +245,27 @@ private T successor(Node<T, V> node) {
176245 return null ;
177246 }
178247
248+ /**
249+ * Search for the successor of a given key.
250+ *
251+ * @param key find successor of this key
252+ * @return generic type value; null if key has no successor
253+ */
254+ public T successor (T key ) {
255+ Node <T , V > curr = root ;
256+ while (curr != null ) {
257+ if (curr .getKey ().compareTo (key ) == 0 ) {
258+ break ;
259+ } else if (curr .getKey ().compareTo (key ) < 0 ) {
260+ curr = curr .getRight ();
261+ } else {
262+ curr = curr .getLeft ();
263+ }
264+ }
265+
266+ return successor (curr );
267+ }
268+
179269 /**
180270 * Stores in-order traversal of tree rooted at node into a list
181271 *
@@ -197,6 +287,12 @@ private void getInorder(Node<T, V> node, List<String> result) {
197287 }
198288 }
199289
290+ public List <String > getInorder () {
291+ List <String > result = new ArrayList <>();
292+ getInorder (root , result );
293+ return result ;
294+ }
295+
200296 /**
201297 * Stores in-order traversal of tree rooted at node into a list
202298 *
@@ -218,6 +314,12 @@ private void getPreorder(Node<T, V> node, List<String> result) {
218314 }
219315 }
220316
317+ public List <String > getPreorder () {
318+ List <String > result = new ArrayList <>();
319+ getPreorder (root , result );
320+ return result ;
321+ }
322+
221323 /**
222324 * Stores post-order traversal of tree rooted at node into a list
223325 *
@@ -239,6 +341,12 @@ private void getPostorder(Node<T, V> node, List<String> result) {
239341 result .add (node .toString ());
240342 }
241343
344+ public List <String > getPostorder () {
345+ List <String > result = new ArrayList <>();
346+ getPostorder (root , result );
347+ return result ;
348+ }
349+
242350 /**
243351 * Stores level-order traversal of tree rooted at node into a list
244352 *
@@ -264,6 +372,12 @@ private void getLevelorder(Node<T, V> node, List<String> result) {
264372 }
265373 }
266374
375+ public List <String > getLevelorder () {
376+ List <String > result = new ArrayList <>();
377+ getLevelorder (root , result );
378+ return result ;
379+ }
380+
267381 /**
268382 * Get root of tree.
269383 *
@@ -273,28 +387,6 @@ public Node<T, V> root() {
273387 return root ;
274388 }
275389
276- /**
277- * Inserts a key into the tree
278- *
279- * @param key to be inserted
280- */
281- public void insert (T key , V value ) {
282- if (root == null ) {
283- root = new Node <>(key , value );
284- } else {
285- insert (root , key , value );
286- }
287- }
288-
289- /**
290- * Removes a key from the tree, if it exists
291- *
292- * @param key to be removed
293- */
294- public void delete (T key ) {
295- root = delete (root , key );
296- }
297-
298390 /**
299391 * Search for a node with the specified key.
300392 *
@@ -315,95 +407,4 @@ public Node<T, V> search(T key) {
315407 return null ;
316408 }
317409
318- /**
319- * Search for the predecessor of a given key.
320- *
321- * @param key find predecessor of this key
322- * @return generic type value; null if key has no predecessor
323- */
324- public T predecessor (T key ) {
325- Node <T , V > curr = root ;
326- while (curr != null ) {
327- if (curr .getKey ().compareTo (key ) == 0 ) {
328- break ;
329- } else if (curr .getKey ().compareTo (key ) < 0 ) {
330- curr = curr .getRight ();
331- } else {
332- curr = curr .getLeft ();
333- }
334- }
335-
336- return predecessor (curr );
337- }
338-
339- /**
340- * Search for the successor of a given key.
341- *
342- * @param key find successor of this key
343- * @return generic type value; null if key has no successor
344- */
345- public T successor (T key ) {
346- Node <T , V > curr = root ;
347- while (curr != null ) {
348- if (curr .getKey ().compareTo (key ) == 0 ) {
349- break ;
350- } else if (curr .getKey ().compareTo (key ) < 0 ) {
351- curr = curr .getRight ();
352- } else {
353- curr = curr .getLeft ();
354- }
355- }
356-
357- return successor (curr );
358- }
359-
360- /**
361- * Search for the minimum key in the tree.
362- *
363- * @return node with the minimum key; null if tree is empty
364- */
365- public Node <T , V > searchMin () {
366- if (root == null ) {
367- return null ;
368- } else {
369- return searchMin (root );
370- }
371- }
372-
373- /**
374- * Search for the maximum key in the tree.
375- *
376- * @return node with the maximum key; null if tree is empty
377- */
378- public Node <T , V > searchMax () {
379- if (root == null ) {
380- return null ;
381- } else {
382- return searchMax (root );
383- }
384- }
385-
386- public List <String > getInorder () {
387- List <String > result = new ArrayList <>();
388- getInorder (root , result );
389- return result ;
390- }
391-
392- public List <String > getPreorder () {
393- List <String > result = new ArrayList <>();
394- getPreorder (root , result );
395- return result ;
396- }
397-
398- public List <String > getPostorder () {
399- List <String > result = new ArrayList <>();
400- getPostorder (root , result );
401- return result ;
402- }
403-
404- public List <String > getLevelorder () {
405- List <String > result = new ArrayList <>();
406- getLevelorder (root , result );
407- return result ;
408- }
409410}
0 commit comments