Named after its inventors Adelson-Velsky and Landis, an AVL Tree rigorously maintains balance by ensuring that for every node, the difference between the heights of its left and right subtrees is never more than 1. If an operation violates this condition, the tree automatically rebalances itself through a series of rotations. This ensures that operations like search, insert, and delete have a worst-case time complexity of O(log n).
Every time a node is inserted or deleted, the AVL tree checks the balance factor of each affected node. If a node becomes unbalanced, rotations are performed to restore balance. There are four types of rotations:
[−1,0,1].function rebalance(node):
updateHeight(node)
nodeBf = balanceFactor(node)
if nodeBf > 1:
if balanceFactor(node.left) >= 0:
rotateRight(node)
else:
rotateLeft(node.left)
rotateRight(node)
if nodeBf < -1:
if balanceFactor(node.right) <= 0:
rotateLeft(node)
else:
rotateRight(node.right)
rotateLeft(node)
if node.parent:
rebalance(node.parent)
(5 credits)
See what happens with your input
Get a short, step-by-step explanation of this visualization. Turn on AI Summary, then start a fresh run with your input.
Whenever a node is inserted or removed, the AVL tree recalculates the balance factor (left subtree height − right subtree height) of each ancestor node on the return path to the root. If the balance factor deviates outside the range [-1, 1], local rotations restore balance immediately. For an insertion, at most one single or double rotation is needed to restore the global AVL property. For deletion, however, rebalancing one node can reduce the height of its subtree, which may propagate an imbalance to its parent, potentially triggering up to O(log n) rotations along the entire ancestry to the root.
AVL Trees are the optimal choice for read-heavy workloads where lookups far outnumber insertions and deletions. Because AVL trees enforce a tighter height bound than Red-Black Trees, average search paths are approximately 12% shorter, delivering measurably faster lookup latency. Common applications include in-memory language dictionaries, routing table lookups, and static search indexes where the dataset is constructed once and queried repeatedly.
Metric / Operation | Complexity | Description |
|---|---|---|
| Search | O(log n) | Strictly bounded height (≤ 1.44 log₂ n) ensures faster lookups than red-black trees. |
| Insertion | O(log n) | Standard BST insertion followed by at most 1 single or double rotation to restore balance. |
| Deletion | O(log n) | Standard BST deletion; height changes can trigger up to O(log n) cascading rotations to the root. |
| Space Complexity | O(n) | Stores n nodes with child pointers plus an integer height or balance factor per node. |
The balance factor is calculated as: height(left) - height(right). In an AVL tree, balance factor for every node must be within [−1, 0, 1]. If any insertion or deletion causes the factor to become +2 or −2, the tree restores balance using rotations.
LR imbalance occurs when a node is left-heavy, but its left child is right-heavy. Fixed by first performing a Left Rotation on the left child, followed by a Right Rotation on the imbalanced parent node.
to join the discussion
Hand-picked resources to deepen your understanding
© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0