kevin-wayne / algs4

Algorithms, 4th edition textbook code and libraries
http://algs4.cs.princeton.edu/code/
GNU General Public License v3.0
7.41k stars 2.68k forks source link

BST ceiling should return null or throw exception? #78

Closed thorehusfeldt closed 4 years ago

thorehusfeldt commented 4 years ago

In the reference implementation, if a ceiling is called on a non-empty BST with an argument that is larger than the maximum element, the BST returns a null reference.

https://github.com/kevin-wayne/algs4/blob/ae02313405283bfa82b92bdbaad00d0a88f37d3d/src/main/java/edu/princeton/cs/algs4/BST.java#L325

The documentation instead indicates that the operation should throw an exception.

(The operation does throw an exception if the BST is empty.)

A case can be made for both behaviours, but currently the implementation and documentation contradict each other.

kevin-wayne commented 4 years ago

Thanks. Updated to throw a NoSuchElementException in such cases (to match documentation and behavior of ST.java). Also updated RedBlackBST.java.