Short answer: An AVL tree is a binary search tree whose balance factor, left height minus right height, stays in −1, 0, +1 at every node. An insertion or deletion that pushes a factor to ±2 is repaired by a single or a double rotation. The height is at most about 1.44 log₂ n, so search, insert, and delete are O(log n) in the worst case. The tree is the strict height-balanced binary search tree.
What the problem actually asks
A binary search tree is O(n) when keys arrive sorted, because the tree becomes a path. An AVL tree forbids that shape. The balance factor is stored, or recomputed from heights, and a node with factor 2 or −2 has exactly one legal repair: a rotation toward the heavy side, preceded by a rotation of the child when the heavy side is itself bent the other way.
Those four pictures are LL, LR, RR, and RL. LL and RR are one rotation. LR and RL are two. After the rotation the subtree height is what it was before the insertion that caused the trouble, so a further ancestor does not also need repair on the way up from an insertion. Deletion is different: the height can drop, and the walk toward the root may rotate at several nodes.
What the algorithm guarantees
Search, insert, and delete are O(log n) worst case. The balance factors stay in range, and the height bound follows from the Fibonacci recurrence that describes the smallest AVL tree of a given height. Extra memory is a height or a balance factor per node, plus the pointers. In-order traversal is the sorted order, as in any binary search tree.
Rotations preserve the search-tree order. The middle node of the three nodes involved becomes the new subtree root, and the left and right subtrees are reattached so every key on the left stays smaller. A rotation that moves the wrong child breaks the order even if the heights look balanced. Red-black trees are a looser balance with a different color invariant and the same logarithmic bound. AVL is stricter and can perform more rotations on a heavy update mix.
The working steps
- Insert the key as in a binary search tree. Walk back toward the root, updating heights.
- At the first node whose balance factor is +2 or −2, look at the child’s factor to choose LL, LR, RR, or RL.
- LL means the left child is heavy on its left: rotate the node right. RR is the mirror. LR is a left rotation of the left child, then a right rotation of the node. RL is the mirror.
- After an insertion repair, stop. The subtree height is restored. After a deletion, continue up to the root, because a height drop can unbalance an ancestor.
- Search and in-order walk ignore the balance factors and use the search-tree links only.
The logarithmic bound needs the repair. A binary search tree with a height field and no rotations is still a path on sorted input. Red-black trees guarantee a weaker height, at most 2 log₂(n+1), with different fixup cases. AVL does not make range queries faster than a walk of the output, and an order-statistic rank needs a subtree size as well as a height.
Approaches that look simpler and fail
| Approach | Why it fails when a double rotation is replaced by a single one |
|---|---|
| Rotate right whenever the balance factor is +2 | If the left child is heavy on its right, one right rotation leaves a factor of 2 on the other side. That case is LR and needs the extra rotation first. |
| Stop walking up after a deletion is locally repaired | A deletion can shorten the subtree. Ancestors that were balanced can become heavy only after that shortening is visible. |
| Balance by key order instead of by height | The search property is already the key order. The AVL constraint is on heights. Moving the median key to the root is a different rebuild. |
| Omit the height update on the nodes inside the rotation | The next insert reads a stale factor and rotates the wrong way, or not at all. |
A looser logarithmic binary search tree is the red-black tree, which is what many standard libraries ship. A tree that rebalances by arbitrary priorities is a treap. AVL is the height-balance rule with factors in {−1, 0, +1}.
A working example
This Python fragment restores the AVL balance factor to the set {-1, 0, +1} with the four rotations. Inserting 1, 2, 3 in that order rotates left at the root. The inorder walk is 1 2 3 and the root is 2.
class N:
def __init__(self, key):
self.key, self.left, self.right, self.h = key, None, None, 1
def height(n):
return n.h if n else 0
def fix(n):
n.h = 1 + max(height(n.left), height(n.right))
return n
def bf(n):
return height(n.left) - height(n.right)
def rot_right(y):
x = y.left
y.left = x.right
x.right = y
fix(y); fix(x)
return x
def rot_left(x):
y = x.right
x.right = y.left
y.left = x
fix(x); fix(y)
return y
def balance(n):
n = fix(n)
if bf(n) > 1:
if bf(n.left) < 0:
n.left = rot_left(n.left)
return rot_right(n)
if bf(n) < -1:
if bf(n.right) > 0:
n.right = rot_right(n.right)
return rot_left(n)
return n
def insert(n, key):
if n is None:
return N(key)
if key < n.key:
n.left = insert(n.left, key)
else:
n.right = insert(n.right, key)
return balance(n)
def inorder(n, out):
if n:
inorder(n.left, out)
out.append(n.key)
inorder(n.right, out)
root = None
for key in (1, 2, 3):
root = insert(root, key)
order = []
inorder(root, order)
assert order == [1, 2, 3] and root.key == 2 and abs(bf(root)) <= 1
print(*order, "root", root.key)
Where it is used
Windows supplies an RTL AVL table for in-memory indexes that want a strict height bound. The balance factor forces a rotation sooner than a red-black tree. Databases that care about disk pages use a B-tree, not an AVL tree.
When you can drop this algorithm
Drop AVL when a library red-black tree or a hash table already provides the required operations. Keep AVL when the worst-case height must be the tighter Fibonacci bound and the four rotation cases are acceptable code.
Why this stays published
We publish this so a search for an AVL tree returns the balance factor, the four rotation cases, and the reason deletion keeps walking up. An AVL tree keeps every balance factor in −1, 0, or +1 by single and double rotations, which guarantees search, insert, and delete in O(log n) worst-case time.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
