An AVL tree is a binary search tree whose left and right subtree heights differ by at most one at every node. This balance condition keeps height O(log n), so lookup, insertion, and deletion each take O(log n) in the worst case. A rotation changes a few references while retaining in-order key order. Height metadata must be recomputed from children after every structural change. The demonstration isolates the left-left insertion case; a complete implementation must also handle right-right, left-right, and right-left cases and rebalance after deletion.
AVL trees: restore height balance after insertion
Operational case
A depot index receives numeric bay keys 61, 52, then 47. An ordinary binary search tree forms a three-node left chain. Rotating right around 61 makes 52 the root, with 47 and 61 as children. The in-order sequence remains 47, 52, 61, which is the search invariant. The height difference at the new root becomes zero. For a large stream of monotonic bay IDs, the balance guarantee is the reason to choose a self-balancing tree rather than a plain tree whose height can grow linearly.
Working Python program
class BayNode:
def __init__(self, bay_key, left=None, right=None):
self.bay_key = bay_key
self.left = left
self.right = right
self.height = 1
def height(node):
return 0 if node is None else node.height
def refresh(node):
node.height = 1 + max(height(node.left), height(node.right))
def rotate_right(old_root):
new_root = old_root.left
old_root.left = new_root.right
new_root.right = old_root
refresh(old_root)
refresh(new_root)
return new_root
root = BayNode(61, BayNode(52, BayNode(47)))
refresh(root.left)
refresh(root)
root = rotate_right(root)
print(root.bay_key, root.left.bay_key, root.right.bay_key, root.height)Output
52 47 61 2Time, space, and tradeoff
The shown rotation changes a constant number of pointers and height fields, so the rotation itself costs O(1) time and O(1) extra space. Finding the insertion position and walking back through ancestors costs O(log n) when the AVL invariant already holds. The example constructs one known imbalance to expose the operation; it is not a complete insertion API. Deletion can trigger balance repair on multiple ancestors. Store heights consistently or a later operation may select the wrong rotation despite the tree looking correct locally.
Common Mistakes
- Do not confuse one rotation example with a complete AVL implementation.
- Do not update parent height before recomputing its changed child.
- Do not lose the transferred subtree during a rotation.
Connected lessons
- Trees and Heaps
- Data Structures
- Binary search trees: preserve order through every branch
- Binary heaps: select the next priority with a tie rule
- Tries: make prefix search distinct from complete-key lookup
- Projects
- Quizzes
AVL order statistics: maintain subtree sizes for rank and select adds a related operation contract.
Splay Trees: Access Rotations and Join Invariants extends this operation comparison.
Scapegoat trees: rebuild a deep insertion subtree adds a distinct structure contract to compare.
