Exemple #1
0
 private static AVLTree removeMinBinding(AVLTree t, NodeRef noderemoved) {
   assert !t.isEmpty();
   if (t.left().isEmpty()) {
     noderemoved.node = t;
     return t.right();
   }
   return balance(removeMinBinding(t.left(), noderemoved), t.key(), t.value(), t.right());
 }
Exemple #2
0
 @SuppressWarnings("unchecked")
 private void forEach(AVLTree t, PMap.Consumer<K, V> action) {
   if (t.isEmpty()) {
     return;
   }
   action.accept((K) t.key(), (V) t.value());
   forEach(t.left(), action);
   forEach(t.right(), action);
 }
Exemple #3
0
 private static AVLTree remove(Object key, AVLTree t) {
   if (t.isEmpty()) {
     return t;
   }
   int result = KEY_COMPARATOR.compare(key, t.key());
   if (result == 0) {
     return combineTrees(t.left(), t.right());
   } else if (result < 0) {
     AVLTree left = remove(key, t.left());
     if (left == t.left()) {
       return t;
     }
     return balance(left, t.key(), t.value(), t.right());
   } else {
     AVLTree right = remove(key, t.right());
     if (right == t.right()) {
       return t;
     }
     return balance(t.left(), t.key(), t.value(), right);
   }
 }
Exemple #4
0
 private static AVLTree put(Object key, Object value, AVLTree t) {
   if (t.isEmpty()) {
     return createNode(t, key, value, t);
   }
   int result = KEY_COMPARATOR.compare(key, t.key());
   if (result == 0) {
     if (value.equals(t.value())) {
       return t;
     }
     return createNode(t.left(), key, value, t.right());
   } else if (result < 0) {
     AVLTree left = put(key, value, t.left());
     if (left == t.left()) {
       return t;
     }
     return balance(left, t.key(), t.value(), t.right());
   } else {
     AVLTree right = put(key, value, t.right());
     if (right == t.right()) {
       return t;
     }
     return balance(t.left(), t.key(), t.value(), right);
   }
 }
Exemple #5
0
 @SuppressWarnings("unchecked")
 @Nullable
 @Override
 public V get(K key) {
   Preconditions.checkNotNull(key);
   AVLTree t = this;
   while (!t.isEmpty()) {
     int c = KEY_COMPARATOR.compare(key, t.key());
     if (c == 0) {
       return (V) t.value();
     } else if (c < 0) {
       t = t.left();
     } else {
       t = t.right();
     }
   }
   return null;
 }
Exemple #6
0
 private static AVLTree createNode(AVLTree newLeft, AVLTree oldTree, AVLTree newRight) {
   return createNode(newLeft, oldTree.key(), oldTree.value(), newRight);
 }