AVL tree (insert)
A binary search tree that, after each insert, keeps the heights of every node's two subtrees within 1 of each other using one or two rotations.
A plain binary search tree can become a chain (height n). An AVL tree (Adelson-Velsky and Landis, 1962) requires
that at every node the heights of the two subtrees differ by at most 1; so the height is always O(log n).
- Time:
O(log n) per insert - Space:
O(log n) - Horowitz–Sahni, chapter 10
- CLRS, chapter 13
Insert 10, 20, 30, 40, 50, 25; both single and double rotations show up.
class Node:def __init__(self, key):self.key = keyself.left = Noneself.right = Noneself.height = 1def avl(root, ops):for op, key in ops:root = insert(root, key)return rootdef height(node):if node is None:return 0return node.heightdef insert(node, key):if node is None:return Node(key)if key < node.key:node.left = insert(node.left, key)else:node.right = insert(node.right, key)node.height = 1 + max(height(node.left), height(node.right))balance = height(node.left) - height(node.right)if balance > 1:if key > node.left.key:node.left = rotate_left(node.left)return rotate_right(node)if balance < -1:if key < node.right.key:node.right = rotate_right(node.right)return rotate_left(node)return nodedef rotate_right(y):x = y.lefty.left = x.rightx.right = yy.height = 1 + max(height(y.left), height(y.right))x.height = 1 + max(height(x.left), height(x.right))return xdef rotate_left(x):y = x.rightx.right = y.lefty.left = xx.height = 1 + max(height(x.left), height(x.right))y.height = 1 + max(height(y.left), height(y.right))return y
Start
∅
Recursion tree
Filled orange = running call · Orange outline = still on the stack · Grey = finished
Local variables
Orange = changed in this step
ops list
Call stack
- avl()line 9
How the work grows with input size
Average of 8 random graphs per n (fixed seeds). Hollow marker = the input on screen.
- comparisons
- rotate_left
- rotate_right
- current input (n=6)
Show as a table
| n | comparisons | rotate_left | rotate_right |
|---|---|---|---|
| 3 | 2.8 | 0.3 | 0.6 |
| 5 | 7 | 0.8 | 0.8 |
| 7 | 12.9 | 1.6 | 2 |
| 9 | 19.1 | 2.5 | 1.8 |
| 11 | 25 | 2.1 | 1.9 |
| 13 | 34 | 3.9 | 4.5 |
| 15 | 42.5 | 4.5 | 4.5 |
Explanation
The idea
A plain binary search tree can become a chain (height n). An AVL tree (Adelson-Velsky and Landis, 1962) requires
that at every node the heights of the two subtrees differ by at most 1; so the height is always O(log n).
Insert
- Insert as in a plain tree.
- On the way back up to the root, update each node's height and compute its balance (left height − right height).
- If the balance becomes 2 or −2, fix it with rotations:
- left–left or right–right: one single rotation.
- left–right or right–left: two rotations (first on the child, then on the node itself).
A rotation does not change the order of the keys (the inorder sequence), only the shape of the tree.
What to watch in the trace
- After each insert, the balance of the nodes on the way back up is computed; each node's
heightis in the memory panel. - Compare "sorted keys" with the same input in the plain binary search tree algorithm.