Sub topic 15
Keeping a BST Balanced: AVL Trees
Assume you know binary search trees, recursion, references, and Big-O notation. The key issue is that a BST operation follows one root-to-leaf path, so its cost is proportional to the tree's height. An AVL tree is a BST that automatically limits this height after every insertion and deletion.
The balance condition
For each node, define its balance factor as:
height(left subtree) - height(right subtree)
An AVL tree requires every node's balance factor to be -1, 0, or 1. A node with factor 2 is too heavy on the left. A node with factor -2 is too heavy on the right.
The usual height convention is that an empty subtree has height -1 and a leaf has height 0. Other conventions are possible, but the balance calculation must use one convention consistently.
Rotations that restore balance
A rotation changes a few child references locally. It preserves the BST ordering while moving nodes from a taller side to a shorter side.
| Imbalance | Shape of the heavy path | Repair |
|---|---|---|
| LL | left child has a left-heavy subtree | right rotation |
| RR | right child has a right-heavy subtree | left rotation |
| LR | left child is right-heavy | left rotation on child, then right rotation |
| RL | right child is left-heavy | right rotation on child, then left rotation |
The first two cases use one rotation. The last two are double rotations.
For example, inserting 30, then 20, then 10 creates an LL imbalance at 30. A right rotation makes 20 the new subtree root, with 10 on its left and 30 on its right.
Insertion and deletion
AVL insertion first follows ordinary BST insertion. On the way back up the recursion, it updates each node's height and checks its balance factor. If a node becomes unbalanced, the appropriate rotation restores the condition.
Deletion also begins as ordinary BST deletion. However, deletion can make several ancestors unbalanced, so the algorithm may need to rebalance at multiple levels while returning toward the root.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 0
def height(node):
return -1 if node is None else node.height
def update_height(node):
node.height = 1 + max(height(node.left), height(node.right))
def rotate_right(z):
y = z.left
middle = y.right
y.right = z
z.left = middle
update_height(z)
update_height(y)
return y
def rotate_left(z):
y = z.right
middle = y.left
y.left = z
z.right = middle
update_height(z)
update_height(y)
return yCost and trade-offs
AVL search, insertion, and deletion each take O(log n) time because AVL height is logarithmic in the number of nodes. A normal BST can degrade to height n, making these operations O(n).
AVL trees maintain stricter balance than some alternatives, such as red-black trees. This often gives faster searches, but rotations and height updates add implementation complexity. AVL trees are useful when searches greatly outnumber updates. Red-black trees are often preferred when insertions and deletions are frequent.
Exercise
Inserting keys 50, 30, and 40 creates which imbalance at node 50, and which rotations repair it?
Show answer
It creates an LR imbalance. Rotate left at node 30, then rotate right at node 50.