Example #1
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;
    }
Example #2
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
    }
Example #3
0
  protected BSTVertex delete(BSTVertex T, int v) {
    if (T == null) return T; // cannot find the item to be deleted

    if (T.key == v) { // 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
        BSTVertex temp = T;
        T.right.parent = T.parent;
        T = T.right; // bypass T
        temp = null;
      } else if (T.left != null && T.right == null) { // only one child at left
        BSTVertex temp = T;
        T.left.parent = T.parent;
        T = T.left; // bypass T
        temp = null;
      } else { // has two children, find successor
        int 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 < v) // search to the right
    T.right = delete(T.right, v);
    else // search to the left
    T.left = delete(T.left, v);
    return T; // return the updated BST
  }
Example #4
0
  protected BSTVertex insert(BSTVertex T, int v) {
    if (T == null) return new BSTVertex(v); // insertion point is found

    if (T.key < v) { // 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;
    }

    return T; // return the updated BST
  }
Example #5
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
    }
Example #6
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;
 }
Example #7
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
    }
Example #8
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;
    }