What is an AVL Tree?
An AVL Tree (named after Adelson-Velsky and Landis) was the first self-balancing Binary Search Tree (BST) to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one. If at any time they differ by more than one, rebalancing is performed to restore this property.
This self-balancing property ensures that the tree height remains O(log n), guaranteeing efficient Search, Insert, and Delete operations even in the worst case (unlike a standard BST which can degenerate into a linked list).
Time Complexity
- SearchO(log n)
- InsertO(log n)
- DeleteO(log n)
- Space ComplexityO(n)
Balancing Rotations
- Balance Factor (BF)Calculated as
Height(Left) - Height(Right). Valid values are -1, 0, 1. - Single RotationsLeft (LL) and Right (RR) rotations fix simple imbalances.
- Double RotationsLeft-Right (LR) and Right-Left (RL) rotations fix complex "dog-leg" imbalances.