Sub topic 3
Rotations: changing shape without breaking order
This chapter assumes you know binary search tree (BST) insertion and search, recursion on trees, and basic Python. A BST keeps every key in a node's left subtree smaller than the node, and every key in its right subtree larger.
The idea
Balancing schemes need a way to make a tree shorter on one side without breaking that ordering. That operation is a rotation. It is a local change of a few pointers. It takes constant time, O(1), and it never changes the in-order sequence of keys.
Rotations are the building block for both AVL and red-black trees. Later chapters decide when to rotate. This chapter covers how.
Right rotation
Take a node y with a left child x. Call the subtrees A (left of x), B (right of x) and C (right of y). Before the rotation, the order is A < x < B < y < C. A right rotation lifts x above y.
Only three links change:
y.leftnow points toB.x.rightnow points toy.- The parent of
ynow points tox.
Subtree B holds keys greater than x and less than y. It was the right child of x, and it becomes the left child of y. Both positions allow exactly those keys. The in-order sequence is still A, x, B, y, C.
A left rotation is the mirror image. It lifts the right child above its parent.
Code
The rotation functions return the new root of the subtree. The caller must store that return value in the parent's link, or in the tree's root variable. Each function also recomputes heights, child first, because the old parent is now the child.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 1 # height of a single node is 1; empty tree is 0
def height(n):
return n.height if n else 0
def update(n):
n.height = 1 + max(height(n.left), height(n.right))
def rotate_right(y):
x = y.left
y.left = x.right
x.right = y
update(y)
update(x)
return x
def rotate_left(x):
y = x.right
x.right = y.left
y.left = x
update(x)
update(y)
return y
def in_order(n):
return in_order(n.left) + [n.key] + in_order(n.right) if n else []
y = Node(20)
y.left = Node(10)
y.left.left = Node(5)
y.left.right = Node(15)
y.right = Node(30)
update(y.left)
update(y)
before = in_order(y)
root = rotate_right(y)
print(before) # [5, 10, 15, 20, 30]
print(in_order(root)) # [5, 10, 15, 20, 30]
print(root.key, root.height) # 10 3
back = rotate_left(root)
print(back.key, in_order(back) == before) # 20 TrueThe two in-order lists are identical, so the ordering survived. A left rotation at the new root undoes the right rotation, which shows the two operations are inverses.
What a rotation does to height
A rotation moves height from one side to the other. In the example, x had height 2 on the left of y. After the rotation, the subtree C is one level deeper than before, and A is one level shallower. Rotating toward the heavy side lowers the heavy side and raises the light side.
That is why rotations repair imbalance. If the left side is too tall, a right rotation shortens it.
Common errors
- Forgetting to reattach the result.
rotate_right(node.left)alone does nothing useful. Writenode.left = rotate_right(node.left). - Rotating when the child is
None.rotate_rightneedsy.left, androtate_leftneedsx.right. Rebalancing code must only call them when that child exists. - Losing subtree
B. If you setx.right = ybefore savingx.rightintoy.left,Bis lost. - Expecting one rotation to fix every case. If the heavy child leans the opposite way, a single rotation just moves the problem. The next chapters handle that with a double rotation.
Exercise
A node y has left child x, and x has right child b. After rotate_right(y), where does b end up, and why is the BST ordering still valid?
Show answer
b becomes the left child of y. Every key in b is greater than x and less than y, since b was in x's right subtree and y's left subtree. The new left slot of y accepts exactly those keys, and the in-order sequence is unchanged.