Exemple #1
0
    @Override
    protected BSTVertex delete(BSTVertex T, Name v) {
      if (T == null) return T; // cannot find the item to be deleted

      if (T.key.babyName.equals(v.babyName)) { // this is the node to be deleted
        if (T.left == null && T.right == null) { // this is a leaf
          T = null; // simply erase this node
        } else if (T.left == null && T.right != null) { // only one child at right
          T.right.parent = T.parent;
          T = T.right; // bypass T
        } else if (T.left != null && T.right == null) { // only one child at left
          T.left.parent = T.parent;
          T = T.left; // bypass T
        } else { // has two children, find successor
          Name successorV = successor(v);
          T.key = successorV; // replace this key with the successor's key
          T.right = delete(T.right, successorV); // delete the old successorV
        }
      } else if (T.key.babyName.compareTo(v.babyName) < 0) { // search to the right
        T.right = delete(T.right, v);
      } else { // search to the left
        T.left = delete(T.left, v);
      }
      updateHeight(T);
      updateSize(T);
      // Balance the tree after deletion
      T = balance(T);
      return T; // return the updated BST
    }
Exemple #2
0
    private BSTVertex rotateLeft(BSTVertex T) {
      BSTVertex w = T.right;
      // Connect the right node of T (which is w) to the parent of T
      w.parent = T.parent;
      // Put w above T
      T.parent = w;
      // Left node of w is shifted to right of T to replace w
      T.right = w.left;
      // Need to set the parent of w.left unless w.left does not exist
      if (w.left != null) {
        w.left.parent = T;
      }
      // Set T, which is under w, to w.left
      w.left = T;

      if (w.parent != null) {
        if (w.parent.left == T) {
          w.parent.left = w;
        } else {
          w.parent.right = w;
        }
      }
      updateHeight(T);
      updateHeight(w);
      // Only T and w has new sizes
      w.size = T.size;
      T.size = getSize(T.left) + getSize(T.right) + 1;
      return w;
    }
Exemple #3
0
    protected BSTVertex insert(BSTVertex T, Name v) {
      if (T == null) return new BSTVertex(v); // insertion point is found

      if (T.key.babyName.compareTo(v.babyName) < 0) { // search to the right
        T.right = insert(T.right, v);
        T.right.parent = T;
      } else { // search to the left
        T.left = insert(T.left, v);
        T.left.parent = T;
      }

      T.height = 1 + Math.max(getHeight(T.left), getHeight(T.right));
      return T; // return the updated BST
    }
Exemple #4
0
 public BSTVertex balance(BSTVertex T) {
   // Check the balance factor
   if (getBalanceFactor(T) == 2 && getBalanceFactor(T.left) == 1) {
     T = rotateRight(T);
   } else if (getBalanceFactor(T) == 2 && getBalanceFactor(T.left) == -1) {
     T.left = rotateLeft(T.left);
     T = rotateRight(T);
   } else if (getBalanceFactor(T) == -2 && getBalanceFactor(T.right) == 1) {
     T.right = rotateRight(T.right);
     T = rotateLeft(T);
   } else if (getBalanceFactor(T) == -2 && getBalanceFactor(T.right) == -1) {
     T = rotateLeft(T);
   } else {; // No need to do anything
   }
   return T;
 }
Exemple #5
0
    @Override
    protected BSTVertex insert(BSTVertex T, Name v) {
      if (T == null) return new BSTVertex(v); // insertion point is found

      if (T.key.babyName.compareTo(v.babyName) < 0) { // search to the right
        T.right = insert(T.right, v);
        T.right.parent = T;
      } else { // search to the left
        T.left = insert(T.left, v);
        T.left.parent = T;
      }
      updateHeight(T);
      T.size++;
      // Balance the tree after insertion
      T = balance(T);
      return T; // return the updated BST
    }
Exemple #6
0
    private BSTVertex rotateRight(BSTVertex T) {
      BSTVertex w = T.left;
      w.parent = T.parent;
      T.parent = w;
      T.left = w.right;
      if (w.right != null) {
        w.right.parent = T;
      }
      w.right = T;

      if (w.parent != null) {
        if (w.parent.left == T) {
          w.parent.left = w;
        } else {
          w.parent.right = w;
        }
      }
      updateHeight(T);
      updateHeight(w);
      w.size = T.size;
      T.size = getSize(T.left) + getSize(T.right) + 1;
      return w;
    }