A binary search tree keeps keys in order: smaller to the left, larger to the right. Its speed is its height — a search walks one node per level. Feed it already-sorted keys and every new key hangs off the same side: the tree sags into a chain of height n, and search becomes O(n) — no better than a list.
In 1962 Adelson-Velsky and Landis gave the fix that carries their initials. Keep every node's balance factor — the height of its left subtree minus its right — within ±1. Whenever an insertion breaks that, a local rotation lifts a grandchild over its parent and re-levels the beam, in O(1) work.
Two of the four cases need a double rotation: when the new key lands on the inner side of a leaning subtree, one rotation alone would just tip it the other way, so the inner grandchild is first swung up, then over. The invariant guarantees height stays within about 1.44 log₂n — the whole tree can never sag. Balance is not bookkeeping; it is a shape you can hang.
The simulation stopped unexpectedly — the lesson continues without it. You can move on; nothing you did was wrong.