What is an AVL tree?
An AVL tree is a binary search tree that keeps itself balanced. As in any binary search tree, every key in a node’s left subtree is smaller than the node’s key, and every key in its right subtree is larger. The AVL tree adds one rule: at every node, the heights of the left and right subtrees may differ by at most 1.
To check that rule, each node gets a balance factor:
balance factor = height of right subtree - height of left subtree
A balanced node has a balance factor of -1, 0, or +1. A value of +2 or -2 means the node is out of balance, and the tree has to be repaired before the change is complete.
An AVL tree also gives you the usual abstract operations of an ordered set: insert a key, delete a key, and search for a key. The ordered set is the abstract data type, and the AVL tree is one concrete way to build it. Keeping those two ideas apart, as explained in adt vs data structure, makes it easier to compare AVL trees with hash tables or sorted arrays.
Why a plain binary search tree is not enough
An ordinary binary search tree is simple, but its shape depends on the order in which keys arrive. Insert 10, 20, 30, and 40 in that order, and each new key lands to the right of the one before it. The tree becomes a straight chain, and a search has to walk through every node. For n keys, that takes O(n) time.
An AVL tree avoids this by restructuring itself whenever a node becomes too lopsided.
How balance is restored: rotations
A rotation moves a few child links so that one child takes the place of its parent. The in-order sequence of keys stays the same, so the tree still holds the same values in sorted order. There are two kinds of rotation:
- A single rotation moves one node up one level, either to the left or to the right.
- A double rotation is two single rotations applied in a specific order.
Four cases cover the imbalances that come up:
- Left-left (LL): the node has a balance factor of -2 and its left child leans left. The fix is a single right rotation.
- Right-right (RR): the node has a balance factor of +2 and its right child leans right. The fix is a single left rotation.
- Left-right (LR): the node has a balance factor of -2, but its left child leans right. The fix is a left rotation on the child, then a right rotation on the node.
- Right-left (RL): the node has a balance factor of +2, but its right child leans left. The fix is a right rotation on the child, then a left rotation on the node.
Deletion can produce these cases too. It can also leave a child with a balance factor of 0, and in that situation a single rotation is still enough.
Search, insertion, and deletion
Search, insertion, and deletion each follow one path down the tree, so their cost depends on the tree’s height. The balance rule keeps the height close to log n, so each operation takes O(log n) time in the worst case.
- Search follows one path from the root to the key or to the point where it would be.
- Insertion attaches a new leaf, then checks balance factors on the way back up. One single or double rotation is enough to restore balance, so insertion stays O(log n).
- Deletion removes the node, then checks balance on the way up. A rotation may be needed at several levels, so deletion can perform up to about log n rotations. Each rotation takes constant time, so the total is still O(log n).
The height of an AVL tree with n nodes is at most about 1.44 times log2 n. The space needed is O(n), because the tree stores one node for each key plus a small amount of balance information per node.
If the O(log n) notation is new to you, big o data structures is a good place to start, since it explains how these bounds are written and compared.
AVL tree vs. red-black tree
Red-black trees are the other common self-balancing tree, and both give O(log n) worst-case operations. The difference is how strictly each one balances:
- AVL trees keep the heights of sibling subtrees within 1 of each other, so the tree is more tightly balanced. Lookups tend to be a little faster.
- Red-black trees allow more height difference, so they usually need fewer rotations when keys are inserted and deleted.
Many standard libraries use red-black trees for their ordered maps. Java’s TreeMap, for example, is built on one.
When AVL trees make sense
AVL trees suit data that is searched often and changed less often, especially when you need a guaranteed worst case and the keys must stay in order. They are also a useful teaching tool. The rules are short, and each rotation can be drawn and checked by hand.
How to implement one
A typical implementation stores the height in each node. After an insertion or deletion, the code updates heights on the way back up, computes each balance factor, and performs a rotation at any node where the factor reaches +2 or -2. Recursive code is a natural fit, because each call can return the root of the subtree it has just rebalanced.
Key takeaways
- An AVL tree is a binary search tree in which the subtrees of every node differ in height by at most 1.
- Single and double rotations restore balance after insertions and deletions.
- Search, insertion, and deletion take O(log n) time in the worst case, and the space used is O(n).