An AVL tree is a binary search tree that re-balances itself after every insert and delete so the heights of the left and right subtrees of every node differ by at most one. When that invariant breaks, exactly one of four rotation patterns (LL, LR, RL, RR) restores it โ and you can watch each one fire live below.
Each node shows key in the centre, height (h) above-right, and balance factor (bf) below-right.
imbalanced (|bf| โฅ 2)
rotated this step
search hit
Imbalance is named by where the new key landed relative to the unbalanced ancestor's two-edge ladder:
Because AVL re-balances eagerly, lookups are guaranteed O(log n) โ the height is bounded by roughly 1.44 ยท logโ(n + 2). Compare this to inserting 1, 2, 3, 4, 5 into a plain BST, which would degenerate to a height-5 linked list; click the worst-case button above to watch AVL keep that flat.