AVL trees are self-balancing binary search trees that automatically maintain a balanced structure after every insertion and deletion. This balance guarantee is what makes them useful: while a plain BST can degrade to height if you insert sorted data, an AVL tree always stays at height, keeping search, insertion, and deletion fast.
AVL Tree Properties
An AVL tree is a BST with one extra rule: for every node in the tree, the heights of its left and right subtrees can differ by at most 1. This is called the height-balance property.
- Balance factor of a node is defined as
- For a tree to be AVL-valid, every node's balance factor must be , , or
- Stated formally: for all nodes
Why does this matter? This constraint keeps the tree's overall height at for nodes. You can prove this using Fibonacci-like minimum AVL trees: the minimum number of nodes in an AVL tree of height satisfies , which grows exponentially. That means height grows only logarithmically with .
A null (empty) subtree has height by convention, and a leaf node has height . Getting this convention right matters when you're computing balance factors during implementation.
AVL Tree Operations
Insertion
Insertion is a two-phase process:
- Insert like a normal BST. Walk down the tree comparing keys, and place the new node at the appropriate leaf position.
- Walk back up to the root, updating heights and checking the balance factor at each ancestor node.
- If any node's balance factor becomes or , the tree is unbalanced at that node. Perform the appropriate rotation (see below) to fix it.
One key detail: after an insertion, you only ever need one rotation (single or double) to restore balance. Once you fix the first unbalanced ancestor, all ancestors above it will also be balanced. This is not true for deletion.
![Properties of AVL trees, Laborator 10 - Treap, AVL Tree, Red-Black Tree [CS Open CourseWare]](https://storage.googleapis.com/static.prod.fiveable.me/search-images%2F%22AVL_tree_properties_self-balancing_binary_search_tree_balance_factor_height-balance_efficient_operations_diagram%22-avl-tree-wbalance_k.svg.png%3Fw%3D500%26tok%3D6d55d9.png)
Deletion
Deletion works similarly but with an important difference:
- Delete like a normal BST. If the node has two children, replace it with its in-order successor (or predecessor), then delete that successor node.
- Walk back up from the deleted node's parent to the root, updating heights and checking balance factors.
- If any node is unbalanced, perform the appropriate rotation.
Unlike insertion, a single deletion can require multiple rotations along the path to the root. Each rotation can reduce the height of a subtree, which may unbalance an ancestor further up.
Time Complexity
All three core operations run in worst-case time:
- Search: Standard BST search. Since the height is , you visit at most nodes.
- Insertion: The BST insertion walk is . Rebalancing requires at most one rotation, which is . Total: .
- Deletion: The BST deletion walk is . Rebalancing may require up to rotations in the worst case, but each rotation is , so the total is still .
Rotations for AVL Rebalancing
Rotations are the mechanism that restores balance. There are four cases, determined by where the imbalance is and which direction the heavy subtree leans. Think of it as: first identify the unbalanced node, then look at its heavy child's balance factor to pick the right rotation.
Single Rotations:
- Right Rotation (RR): The node has balance factor and its left child has balance factor or (left-left case). The left child becomes the new subtree root, and the original node becomes its right child.
- Left Rotation (LL): The node has balance factor and its right child has balance factor or (right-right case). The right child becomes the new subtree root, and the original node becomes its left child.
Double Rotations:
-
Left-Right Rotation (LR): The node has balance factor and its left child has balance factor (left-right case).
- Perform a left rotation on the left child.
- Perform a right rotation on the unbalanced node.
-
Right-Left Rotation (RL): The node has balance factor and its right child has balance factor (right-left case).
- Perform a right rotation on the right child.
- Perform a left rotation on the unbalanced node.
A quick way to remember: if the imbalance and the heavy grandchild are on the same side (left-left or right-right), you need a single rotation. If they're on opposite sides (left-right or right-left), you need a double rotation that first straightens the path, then rotates.