Advertisement
Aldin-SXR

rank()

May 12th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. /* Find the rank of a given key */
  2. public int rank(Key key) {                     
  3.     return rank(root, key);                                 // 1
  4. }  
  5.    
  6. /* Private rank() method */
  7. private int rank(Node<Key, Value> x, Key key) {
  8.     if (x == null) {                                        // 2
  9.         return 0;                                           // 2
  10.     }
  11.        
  12.     int cmp = key.compareTo(x.key);                         // 3
  13.     if (cmp < 0) {                                          // 4
  14.         return rank(x.left, key);                           // 4
  15.     } else if (cmp > 0) {                                   // 5
  16.         return 1 + size(x.left) + rank(x.right, key);       // 5
  17.     } else {                                                // 6
  18.         return size(x.left);                                // 6
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement