diff --git a/src/main/java/edu/princeton/cs/algs4/BinarySearchST.java b/src/main/java/edu/princeton/cs/algs4/BinarySearchST.java index bdb90a63e..cf98526b9 100644 --- a/src/main/java/edu/princeton/cs/algs4/BinarySearchST.java +++ b/src/main/java/edu/princeton/cs/algs4/BinarySearchST.java @@ -313,6 +313,7 @@ public Key select(int k) { */ public Key floor(Key key) { if (key == null) throw new IllegalArgumentException("argument to floor() is null"); + if (isEmpty()) throw new NoSuchElementException("called floor() with empty symbol table"); int i = rank(key); if (i < n && key.compareTo(keys[i]) == 0) return keys[i]; if (i == 0) return null; @@ -329,6 +330,7 @@ public Key floor(Key key) { */ public Key ceiling(Key key) { if (key == null) throw new IllegalArgumentException("argument to ceiling() is null"); + if (isEmpty()) throw new NoSuchElementException("called ceiling() with empty symbol table"); int i = rank(key); if (i == n) return null; else return keys[i];