An AVL tree is a self-balancing Binary Search Tree (BST). The left subtree contains values smaller than the parent node, and the right subtree contains values larger.
For every single node in the tree, the heights of its left and right subtrees can differ by at most 1. The formula is calculated as:
A node is considered balanced if its BF is -1, 0, or 1. If the BF becomes 2 or -2, it is unbalanced and a rotation triggers immediately.
If a single insertion causes multiple ancestor nodes to become imbalanced simultaneously, the AVL engine evaluates them bottom-up.
Deepest Node Priority: The tree travels backward from the newly inserted leaf upward toward the root. It will immediately rotate and fix the lowest (deepest) imbalanced node first.
Once this lowest sub-tree is rotated, its height shrinks back down, which naturally fixes the balance factors of all higher ancestors further up the tree automatically!
When an item is inserted deep down an extended branch (stretching 3 or more nodes deep), the tree ignores any path history past the first two levels below the critical zone.
First Two Steps Rule: Imbalance classification is determined strictly by looking at the first two steps taken down from the node where the imbalance is caught.
No matter how deep the node is inserted, it will always map back to one of the 4 standard core rotations (LL, RR, LR, or RL) depending entirely on those initial two structural directions.