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; }
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; }