void rotateRight(P y) { // zig BT tmp; P x = tmp.root(); BT::assignFrom(x, -y); int& bx = (*x).balance(); int& by = (*y).balance(); BT::assignFrom(-y, +x); BT::assignFrom(+x, y); BT::assignFrom(y, x); if (bx >= 0) by++; else by -= bx - 1; if (by <= 0) bx++; else bx += by + 1; }
void rotateLeft(P x) { // zag BT tmp; P y = tmp.root(); BT::assignFrom(y, +x); int& bx = (*x).balance(); int& by = (*y).balance(); BT::assignFrom(+x, -y); BT::assignFrom(-y, x); BT::assignFrom(x, y); if (by <= 0) bx--; else bx -= by + 1; if (bx >= 0) by--; else by += bx - 1; }