Invariants
t: Node reference to subtree that contains floor of k or else contains no keys smaller than k. candidate: Node not in t whose key is the largest key less than k in the whole tree or null if there is no such Node in the tree.
-
(Base case) If t is null, floor is candidate
-
If k is equal to t.key, floor is just k.
-
If k is smaller than t.key, then candidate doesn't change, but t should be updated to be the left subtree, t.left.
-
If k is larger than t.key, then candidate should be updated to be t.key and t should be updated to be the right subtree, t.right.
Since the subtree referenced by t gets smaller with each step, this will eventually find the floor.