Sub topic 6
AVL deletion and rebalancing on the way up
This chapter assumes you can read the AVL node, rebalance, and insert code from the previous chapters. You should also know how a plain BST deletes a node (leaf, one child, two children).
Deletion starts like a BST delete
AVL deletion has two phases. First, remove the node exactly as a plain binary search tree would. Second, walk back up the recursion path and call rebalance on every ancestor.
A plain BST delete has three cases:
- The node has no left child. Return its right child (which may be
None). - The node has no right child. Return its left child.
- The node has two children. Copy the key of the in-order successor (the smallest key in the right subtree) into this node, then delete that successor from the right subtree.
def min_node(n):
while n.left:
n = n.left
return n
def delete(n, key):
if n is None:
return None
if key < n.key:
n.left = delete(n.left, key)
elif key > n.key:
n.right = delete(n.right, key)
else:
if n.left is None:
return n.right
if n.right is None:
return n.left
successor = min_node(n.right)
n.key = successor.key
n.right = delete(n.right, successor.key)
return rebalance(n)This code relies on Node, update, balance_factor, the rotations, and rebalance from the earlier chapters.
Notice the last line. Every call that did not return early passes its node through rebalance. Because of recursion, that means each ancestor of the removed node is checked from the bottom up. The early returns skip rebalance only for the removed node itself, which is gone.
Why deletion needs more work than insertion
Removing a node makes one subtree shorter. That can push the parent's balance factor to 2 or -2. After the fix, the subtree might still be shorter than before the deletion, which can unbalance the grandparent too.
Insertion behaves differently. One rotation after an insertion restores the subtree to its old height, so no ancestor above it is affected. A deletion has no such guarantee. In the worst case you rotate at every level on the way to the root, which is O(log n) rotations in total.
A new case: the balanced child
During insertion, a heavy child always leans the same way as the parent, or the opposite way. During deletion, the taller child can have a balance factor of 0. Look again at rebalance:
def rebalance(n):
update(n)
bf = balance_factor(n)
if bf > 1:
if balance_factor(n.left) < 0:
n.left = rotate_left(n.left)
return rotate_right(n)
if bf < -1:
if balance_factor(n.right) > 0:
n.right = rotate_right(n.right)
return rotate_left(n)
return nThe tests use < 0 and > 0, not <= 0 and >= 0. When the heavy child has balance factor 0, the code takes the single-rotation path. That is correct. A double rotation in that case would leave the tree unbalanced. This is why the same rebalance function works for both insertion and deletion.
Worked example
Build an AVL tree from the keys 1 to 7. It is perfectly balanced, with 4 at the root. Now delete 7.
root = None
for k in range(1, 8):
root = insert(root, k)
root = delete(root, 7)
print(root.key, root.height) # 4 3
root = delete(root, 6)
print(root.key, root.height) # 4 3
root = delete(root, 5)
print(root.key, root.height) # 2 3After deleting 7 and 6, node 4 has left subtree height 2 and right subtree height 1, so it is still balanced. Deleting 5 leaves 4 with a left subtree of height 2 and no right subtree. The balance factor is 2. The left child (2) has a balance factor of 0, so a single right rotation fixes it. Node 2 becomes the root with 1 on its left and 4 on its right, with 3 as the left child of 4. The height remains 3.
Common errors
- Calling
rebalanceonly on the node where the deletion happened, not on every ancestor. - Forgetting to recompute the height before reading the balance factor.
rebalancehandles this with its first line,update(n). - Using
<= 0for the left-right check. This turns the balanced-child case into an unnecessary double rotation. - Deleting the successor from the wrong subtree. After copying its key, remove it from
n.right, not fromn.
Exercise
In an AVL tree, a node has balance factor 2 after a deletion, and its left child has balance factor 0. Which fix does rebalance apply, and why is it enough?
Show answer
It applies a single right rotation. The check balance_factor(n.left) < 0 is false for a child with balance factor 0, so no left rotation happens first. The single rotation restores a balance factor within one at the node, so no double rotation is needed.
Exercise
Why can a deletion require rotations at several ancestors, while an insertion needs at most one rotation?
Show answer
A rotation after an insertion restores the subtree to the height it had before the insertion, so ancestors above see no change. A rotation after a deletion can leave the subtree one level shorter than before, so the parent's balance factor can change, and the imbalance can propagate up to the root.