AVL Trees
AVL trees
AVL is named after its inventors G. M. Adelson-Velsky and E. M. Landis. An AVL tree is a height-balanced binary search tree: at every node, the heights of the two subtrees differ by at most 1.
A node's balance factor is its left-subtree height minus its right-subtree height. A tree is AVL-balanced only when every balance factor is −1, 0 or 1.
Restoring balance
Insertion or deletion can break the balance condition.
Left and right rotations restore balance.
Left rotation
Rotate nodes X and Y counterclockwise: X's right child replaces X, and X becomes its left child.
Left rotation: preserve the middle subtree
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Right rotation
Rotate nodes X and Y clockwise: X's left child replaces X, and X becomes its right child.
Right rotation: preserve the middle subtree
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
There are four imbalance cases.
Left-left (LL): right rotation
LL: one right rotation
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Right-right (RR): left rotation
RR: one left rotation
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Left-right (LR): rotate left, then right
LR: left rotation, then right rotation
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Right-left (RL): rotate right, then left
RL: right rotation, then left rotation
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Inserting and deleting nodes
For example, insert 3, 2, 1, 4, 5, 6, 7, 16, 15 and 14 in order.
AVL insertion and deletion: rebalance ancestors
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
A double rotation and deletion repair
Insert 30, 10, 20, 40, 50, then delete 10. Identify each imbalance and rotation pivot/direction, draw intermediate trees, and examine the deletion case where the right child has bf=0.