previous | start | next

Algorithms for ceiling and floor Methods

As for many of the binary search tree methods, these two methods can easily be implemented either iteratively (i.e. with loops) or recursively (one public and one private, recursive method).

Either type of implementation is facilitated by a closer examination of examples.

Suppose we wish to find

 floor(45)  // largest key in the tree less than or equal to 45 (or null)
   

Example 1

      60
     /  \
    50  70

floor(45) = ?
   

Candidates? Which keys (if any) are smaller than 45? Which one of these candidates (if any) is largest?

Example 2

             40
            /  \
           /    \
          20     60     
         /  \   /  \ 
        10  30 50  70 

    
floor(45) = ?
   

Candidates? Which keys (if any) are smaller than 45? Which one of these candidates is largest?



previous | start | next