Skip to content

Commit 2730d17

Browse files
committed
updates floor/ceiling to throw NoSuchElementException in corner cases
1 parent ae02313 commit 2730d17

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

src/main/java/edu/princeton/cs/algs4/BST.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,26 @@
4343
* value associated with a key to {@code null} is equivalent to deleting the key
4444
* from the symbol table.
4545
* <p>
46-
* This implementation uses an (unbalanced) binary search tree. It requires that
46+
* It requires that
4747
* the key type implements the {@code Comparable} interface and calls the
4848
* {@code compareTo()} and method to compare two keys. It does not call either
4949
* {@code equals()} or {@code hashCode()}.
50+
* <p>
51+
* This implementation uses an (unbalanced) <em>binary search tree</em>.
5052
* The <em>put</em>, <em>contains</em>, <em>remove</em>, <em>minimum</em>,
5153
* <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 &Theta;(<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 &Theta;(1) time.
57+
* The keys method takes &Theta;(<em>n</em>) time in the worst case.
58+
* Construction takes &Theta;(1) time.
5659
* <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},
6162
* {@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.
6266
*
6367
* @author Robert Sedgewick
6468
* @author Kevin Wayne
@@ -284,7 +288,7 @@ public Key floor(Key key) {
284288
if (key == null) throw new IllegalArgumentException("argument to floor() is null");
285289
if (isEmpty()) throw new NoSuchElementException("calls floor() with empty symbol table");
286290
Node x = floor(root, key);
287-
if (x == null) return null;
291+
if (x == null) throw new NoSuchElementException("argument to floor() is too small");
288292
else return x.key;
289293
}
290294

@@ -299,7 +303,10 @@ private Node floor(Node x, Key key) {
299303
}
300304

301305
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+
303310
}
304311

305312
private Key floor2(Node x, Key key, Key best) {
@@ -322,7 +329,7 @@ public Key ceiling(Key key) {
322329
if (key == null) throw new IllegalArgumentException("argument to ceiling() is null");
323330
if (isEmpty()) throw new NoSuchElementException("calls ceiling() with empty symbol table");
324331
Node x = ceiling(root, key);
325-
if (x == null) return null;
332+
if (x == null) throw new NoSuchElementException("argument to floor() is too large");
326333
else return x.key;
327334
}
328335

src/main/java/edu/princeton/cs/algs4/RedBlackBST.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@
5959
* &Theta;(log <em>n</em>) time in the worst case, where <em>n</em> is the
6060
* number of key-value pairs in the symbol table.
6161
* The <em>size</em>, and <em>is-empty</em> operations take &Theta;(1) time.
62-
* The <em>keys</em> method takes time proportional to the number of keys
63-
* returned by the iterator.
62+
* The <em>keys</em> methods take
63+
* <em>O</em>(log <em>n</em> + <em>m</em>) time, where <em>m</em> is
64+
* the number of keys returned by the iterator.
6465
* Construction takes &Theta;(1) time.
6566
* <p>
67+
* For alternative implementations of the symbol table API, see {@link ST},
68+
* {@link BinarySearchST}, {@link SequentialSearchST}, {@link BST},
69+
* {@link SeparateChainingHashST}, {@link LinearProbingHashST}, and
70+
* {@link AVLTreeST}.
6671
* For additional documentation, see
6772
* <a href="https://algs4.cs.princeton.edu/33balanced">Section 3.3</a> of
6873
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
69-
* For other implementations of the same API, see {@link ST}, {@link BinarySearchST},
70-
* {@link SequentialSearchST}, {@link BST},
71-
* {@link SeparateChainingHashST}, {@link LinearProbingHashST}, and {@link AVLTreeST}.
7274
*
7375
* @author Robert Sedgewick
7476
* @author Kevin Wayne
@@ -480,7 +482,7 @@ public Key floor(Key key) {
480482
if (key == null) throw new IllegalArgumentException("argument to floor() is null");
481483
if (isEmpty()) throw new NoSuchElementException("calls floor() with empty symbol table");
482484
Node x = floor(root, key);
483-
if (x == null) return null;
485+
if (x == null) throw new NoSuchElementException("argument to floor() is too small");
484486
else return x.key;
485487
}
486488

@@ -506,7 +508,7 @@ public Key ceiling(Key key) {
506508
if (key == null) throw new IllegalArgumentException("argument to ceiling() is null");
507509
if (isEmpty()) throw new NoSuchElementException("calls ceiling() with empty symbol table");
508510
Node x = ceiling(root, key);
509-
if (x == null) return null;
511+
if (x == null) throw new NoSuchElementException("argument to ceiling() is too small");
510512
else return x.key;
511513
}
512514

src/main/java/edu/princeton/cs/algs4/ST.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
* value associated with a key to {@code null} is equivalent to deleting the key
3232
* from the symbol table.
3333
* <p>
34-
* This implementation uses a red-black BST. It requires that
34+
* It requires that
3535
* the key type implements the {@code Comparable} interface and calls the
3636
* {@code compareTo()} and method to compare two keys. It does not call either
3737
* {@code equals()} or {@code hashCode()}.
38+
* <p>
39+
* This implementation uses a <em>red-black BST</em>.
3840
* The <em>put</em>, <em>get</em>, <em>contains</em>, <em>remove</em>,
3941
* <em>minimum</em>, <em>maximum</em>, <em>ceiling</em>, and <em>floor</em>
4042
* operations each take &Theta;(log <em>n</em>) time in the worst case,
@@ -211,7 +213,7 @@ public Key max() {
211213
public Key ceiling(Key key) {
212214
if (key == null) throw new IllegalArgumentException("argument to ceiling() is null");
213215
Key k = st.ceilingKey(key);
214-
if (k == null) throw new NoSuchElementException("all keys are less than " + key);
216+
if (k == null) throw new NoSuchElementException("argument to ceiling() is too large");
215217
return k;
216218
}
217219

@@ -226,7 +228,7 @@ public Key ceiling(Key key) {
226228
public Key floor(Key key) {
227229
if (key == null) throw new IllegalArgumentException("argument to floor() is null");
228230
Key k = st.floorKey(key);
229-
if (k == null) throw new NoSuchElementException("all keys are greater than " + key);
231+
if (k == null) throw new NoSuchElementException("argument to floor() is too small");
230232
return k;
231233
}
232234

0 commit comments

Comments
 (0)