Aldin-SXR

get()

May 12th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.39 KB | None | 0 0
  1. /* Retrieve a value associated with a given key */
  2. public Value get(Key key) {
  3.     Node<Key, Value> x = root;              // 1
  4.        
  5.     while (x != null) {                     // 2
  6.         int cmp = key.compareTo(x.key);     // 3
  7.         if (cmp < 0) {                      // 4
  8.             x = x.left;                     // 4
  9.         } else if (cmp > 0) {               // 5
  10.             x = x.right;                    // 5
  11.         } else {                            // 6
  12.             return x.value;                 // 6
  13.         }
  14.     }
  15.        
  16.     return null;                            // 7
  17. }
Advertisement
Add Comment
Please, Sign In to add comment