@@ -53,6 +53,19 @@ private void insert(Node<T, V> node, T key, V value) {
53
53
}
54
54
}
55
55
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
+
56
69
/**
57
70
* Delete a key from the binary search tree rooted at a specified node.
58
71
* 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) {
102
115
return node ;
103
116
}
104
117
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
+
105
127
/**
106
128
* Find the node with the minimum key in the tree rooted at a specified node.
107
129
*
@@ -117,6 +139,19 @@ private Node<T, V> searchMin(Node<T, V> n) {
117
139
}
118
140
}
119
141
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
+
120
155
/**
121
156
* Find the node with the maximum key in the tree rooted at a specified node.
122
157
*
@@ -132,6 +167,19 @@ private Node<T, V> searchMax(Node<T, V> n) {
132
167
}
133
168
}
134
169
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
+
135
183
/**
136
184
* Find the key of the predecessor of a specified node that exists in the tree
137
185
* NOTE: the input node is assumed to be in the tree
@@ -154,6 +202,27 @@ private T predecessor(Node<T, V> node) {
154
202
return null ;
155
203
}
156
204
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
+
157
226
/**
158
227
* Find the key of the successor of a specified node that exists in the tree
159
228
* NOTE: the input node is assumed to be in the tree
@@ -176,6 +245,27 @@ private T successor(Node<T, V> node) {
176
245
return null ;
177
246
}
178
247
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
+
179
269
/**
180
270
* Stores in-order traversal of tree rooted at node into a list
181
271
*
@@ -197,6 +287,12 @@ private void getInorder(Node<T, V> node, List<String> result) {
197
287
}
198
288
}
199
289
290
+ public List <String > getInorder () {
291
+ List <String > result = new ArrayList <>();
292
+ getInorder (root , result );
293
+ return result ;
294
+ }
295
+
200
296
/**
201
297
* Stores in-order traversal of tree rooted at node into a list
202
298
*
@@ -218,6 +314,12 @@ private void getPreorder(Node<T, V> node, List<String> result) {
218
314
}
219
315
}
220
316
317
+ public List <String > getPreorder () {
318
+ List <String > result = new ArrayList <>();
319
+ getPreorder (root , result );
320
+ return result ;
321
+ }
322
+
221
323
/**
222
324
* Stores post-order traversal of tree rooted at node into a list
223
325
*
@@ -239,6 +341,12 @@ private void getPostorder(Node<T, V> node, List<String> result) {
239
341
result .add (node .toString ());
240
342
}
241
343
344
+ public List <String > getPostorder () {
345
+ List <String > result = new ArrayList <>();
346
+ getPostorder (root , result );
347
+ return result ;
348
+ }
349
+
242
350
/**
243
351
* Stores level-order traversal of tree rooted at node into a list
244
352
*
@@ -264,6 +372,12 @@ private void getLevelorder(Node<T, V> node, List<String> result) {
264
372
}
265
373
}
266
374
375
+ public List <String > getLevelorder () {
376
+ List <String > result = new ArrayList <>();
377
+ getLevelorder (root , result );
378
+ return result ;
379
+ }
380
+
267
381
/**
268
382
* Get root of tree.
269
383
*
@@ -273,28 +387,6 @@ public Node<T, V> root() {
273
387
return root ;
274
388
}
275
389
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
-
298
390
/**
299
391
* Search for a node with the specified key.
300
392
*
@@ -315,95 +407,4 @@ public Node<T, V> search(T key) {
315
407
return null ;
316
408
}
317
409
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
- }
409
410
}
0 commit comments