Stored heights
Each node stores its subtree height. Heights are updated after recursive insertion, deletion, and every rotation.
An AVL Tree is a self-balancing Binary Search Tree. For every node, the height difference between the left and right subtrees—its balance factor—must be −1, 0, or 1.
Example AVL Tree loaded. Choose an operation.
LL: rotate right
RR: rotate left
LR: rotate left on child,
then rotate right
RL: rotate right on child,
then rotate left
balance factor =
height(left) - height(right)Each node stores its subtree height. Heights are updated after recursive insertion, deletion, and every rotation.
LL imbalance uses a right rotation. RR imbalance uses a left rotation.
LR and RL imbalances require two rotations because the heavy child leans in the opposite direction.
Deletion can unbalance several ancestors, so balance checks continue all the way back to the root.
| Invariant | Requirement |
|---|---|
| BST ordering | Left values are smaller; right values are larger. |
| Correct stored height | 1 + max(left height, right height) |
| Balance | Absolute balance factor is at most 1. |
| Duplicate policy | Duplicate keys are rejected. |