43
43
* value associated with a key to {@code null} is equivalent to deleting the key
44
44
* from the symbol table.
45
45
* <p>
46
- * This implementation uses an (unbalanced) binary search tree. It requires that
46
+ * It requires that
47
47
* the key type implements the {@code Comparable} interface and calls the
48
48
* {@code compareTo()} and method to compare two keys. It does not call either
49
49
* {@code equals()} or {@code hashCode()}.
50
+ * <p>
51
+ * This implementation uses an (unbalanced) <em>binary search tree</em>.
50
52
* The <em>put</em>, <em>contains</em>, <em>remove</em>, <em>minimum</em>,
51
53
* <em>maximum</em>, <em>ceiling</em>, <em>floor</em>, <em>select</em>, and
52
- * <em>rank</em> operations each take
53
- * linear time in the worst case, if the tree becomes unbalanced.
54
- * The <em>size</em>, and <em>is-empty</em> operations take constant time.
55
- * Construction takes constant time.
54
+ * <em>rank</em> operations each take Θ(<em>n</em>) time in the worst
55
+ * case, where <em>n</em> is the number of key-value pairs.
56
+ * The <em>size</em> and <em>is-empty</em> operations take Θ(1) time.
57
+ * The keys method takes Θ(<em>n</em>) time in the worst case.
58
+ * Construction takes Θ(1) time.
56
59
* <p>
57
- * For additional documentation, see <a href="https://algs4.cs.princeton.edu/32bst">Section 3.2</a> of
58
- * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
59
- * For other implementations, see {@link ST}, {@link BinarySearchST},
60
- * {@link SequentialSearchST}, {@link RedBlackBST},
60
+ * For alternative implementations of the symbol table API, see {@link ST},
61
+ * {@link BinarySearchST}, {@link SequentialSearchST}, {@link RedBlackBST},
61
62
* {@link SeparateChainingHashST}, and {@link LinearProbingHashST},
63
+ * For additional documentation, see
64
+ * <a href="https://algs4.cs.princeton.edu/32bst">Section 3.2</a> of
65
+ * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
62
66
*
63
67
* @author Robert Sedgewick
64
68
* @author Kevin Wayne
@@ -284,7 +288,7 @@ public Key floor(Key key) {
284
288
if (key == null ) throw new IllegalArgumentException ("argument to floor() is null" );
285
289
if (isEmpty ()) throw new NoSuchElementException ("calls floor() with empty symbol table" );
286
290
Node x = floor (root , key );
287
- if (x == null ) return null ;
291
+ if (x == null ) throw new NoSuchElementException ( "argument to floor() is too small" ) ;
288
292
else return x .key ;
289
293
}
290
294
@@ -299,7 +303,10 @@ private Node floor(Node x, Key key) {
299
303
}
300
304
301
305
public Key floor2 (Key key ) {
302
- return floor2 (root , key , null );
306
+ Key x = floor2 (root , key , null );
307
+ if (x == null ) throw new NoSuchElementException ("argument to floor() is too small" );
308
+ else return x ;
309
+
303
310
}
304
311
305
312
private Key floor2 (Node x , Key key , Key best ) {
@@ -322,7 +329,7 @@ public Key ceiling(Key key) {
322
329
if (key == null ) throw new IllegalArgumentException ("argument to ceiling() is null" );
323
330
if (isEmpty ()) throw new NoSuchElementException ("calls ceiling() with empty symbol table" );
324
331
Node x = ceiling (root , key );
325
- if (x == null ) return null ;
332
+ if (x == null ) throw new NoSuchElementException ( "argument to floor() is too large" ) ;
326
333
else return x .key ;
327
334
}
328
335
0 commit comments