private AvlNode<E> deleteMe() { int oldElemCount = this.elemCount; this.elemCount = 0; successor(pred, succ); if (left == null) { return right; } else if (right == null) { return left; } else if (left.height >= right.height) { AvlNode<E> newTop = pred; // newTop is the maximum node in my left subtree newTop.left = left.removeMax(newTop); newTop.right = right; newTop.distinctElements = distinctElements - 1; newTop.totalCount = totalCount - oldElemCount; return newTop.rebalance(); } else { AvlNode<E> newTop = succ; newTop.right = right.removeMin(newTop); newTop.left = left; newTop.distinctElements = distinctElements - 1; newTop.totalCount = totalCount - oldElemCount; return newTop.rebalance(); } }
private AvlNode<T> rotateWithRightChild(AvlNode<T> k2) { AvlNode<T> k1 = k2.right; k2.right = k1.left; k1.left = k2; k2.height = Math.max(height(k2.left), height(k2.right)) + 1; k1.height = Math.max(k2.height, height(k1.right)) + 1; return k1; }
/** * Rotate binary tree node with right child. For AVL trees, this is a single rotation for case 4. * Update heights, then return new root. */ private static AvlNode rotateWithRightChild(AvlNode k1) { AvlNode k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max(height(k1.left), height(k1.right)) + 1; k2.height = max(height(k2.right), k1.height) + 1; return k2; }
private AvlNode<E> rotateLeft() { checkState(right != null); AvlNode<E> newTop = right; this.right = newTop.left; newTop.left = this; newTop.totalCount = this.totalCount; newTop.distinctElements = this.distinctElements; this.recompute(); newTop.recomputeHeight(); return newTop; }
/** * Internal method to insert into a subtree. * * @param x the item to insert. * @param t the node that roots the tree. * @return the new root. */ private AvlNode insert(Comparable x, AvlNode t) { if (t == null) t = new AvlNode(x, null, null); else if (x.compareTo(t.element) < 0) { t.left = insert(x, t.left); if (height(t.left) - height(t.right) == 2) if (x.compareTo(t.left.element) < 0) t = rotateWithLeftChild(t); else t = doubleWithLeftChild(t); } else if (x.compareTo(t.element) > 0) { t.right = insert(x, t.right); if (height(t.right) - height(t.left) == 2) if (x.compareTo(t.right.element) > 0) t = rotateWithRightChild(t); else t = doubleWithRightChild(t); } else ; // Duplicate; do nothing t.height = max(height(t.left), height(t.right)) + 1; return t; }
public AvlNode<T> insert(T x, AvlNode<T> t) { if (t == null) return new AvlNode<>(x, null, null); int compareResult = compare(x, t.element); if (compareResult < 0) { t.left = insert(x, t.left); if (height(t.left) - height(t.right) == 2) if (compare(x, t.left.element) < 0) t = rotateWithLeftChild(t); else t = doubleWithLeftChild(t); } else if (compareResult > 0) { t.right = insert(x, t.right); if (height(t.right) - height(t.left) == 2) if (compare(x, t.right.element) > 0) t = rotateWithRightChild(t); else t = doubleWithRightChild(t); } else ; t.height = Math.max(height(t.left), height(t.right)) + 1; return t; }
private AvlNode<T> doubleWithLeftChild(AvlNode<T> k3) { k3.left = rotateWithRightChild(k3.left); return rotateWithLeftChild(k3); }
/** * Double rotate binary tree node: first left child with its right child; then node k3 with new * left child. For AVL trees, this is a double rotation for case 2. Update heights, then return * new root. */ private static AvlNode doubleWithLeftChild(AvlNode k3) { k3.left = rotateWithRightChild(k3.left); return rotateWithLeftChild(k3); }