AVL tree
A binary search tree that keeps itself balanced: after every change it checks each node's balance factor and, if it goes past 1, fixes it with rotations.
Example
Inserting 1,000 numbers in order, a regular search tree ends up with height 999; an AVL, with a height of about 10.
Test yourself: AVL tree
AVL trees
What problem does an AVL tree solve?
Inserting 1,000 numbers in order, a regular search tree ends up with height 999. An AVL keeps it at about 10.
AVL trees
A node's balance factor is the height of its left subtree minus the height of its ___.
With a left subtree of height 2 and a right one of height 0, the factor is 2 − 0 = 2: unbalanced.
AVL trees
A rotation changes the order of the tree's inorder traversal.
It keeps it: before and after rotating 3 → 2 → 1, the inorder is 1, 2, 3. That's why the tree is still a search tree.
0/3
â–¶ Play