In the composer AVL tree, Haydn has children Händel and Milhaud, and Milhaud has children Mahler and Monteverdi. Why does inserting Mendelssohn need a double rotation?
Mendelssohn becomes Mahler's right child. Haydn is the lowest unbalanced node (−2): z = Haydn, y = Milhaud (a right child), x = Mahler (a left child). The path bends, so one right rotation around Milhaud and one left rotation around Haydn lift Mahler to the top of the subtree.
* Mahler, the middle name of the three, rises two levels and ends up above both Haydn and Milhaud. *
Search: Mendelssohn < Mozart (Me before Mo), > Dvorak, > Haydn, < Milhaud (Me before Mi), > Mahler (Me after Ma). So it is Mahler's right child.
Balance factors: Mahler −1, Milhaud +1, Haydn −2, Dvorak −2, Mozart +2. The lowest violation is Haydn, so z = Haydn. Its taller child y = Milhaud lies to the right; Milhaud's taller child x = Mahler lies to the left. Right, then left: a zig-zag (right-left case).
Trinode view: sorted, a = Haydn, b = Mahler, c = Milhaud. The subtrees are T0 = Händel, T1 = Mahler's empty left side, T2 = Mendelssohn, T3 = Monteverdi. So Mahler goes on top, Haydn on its left (with Händel), Milhaud on its right (with Mendelssohn and Monteverdi).
As two rotations: a right rotation around Milhaud lifts Mahler into Milhaud's place, then a left rotation around Haydn lifts Mahler into Haydn's place. The subtree is back to height 2, and Dvorak (−1) and Mozart (+1) are balanced again.