Advertisement
Aldin-SXR

put()

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