Pointer-machine AVL balancing #
balance_correct proves that a finite pointer-machine program implements the existing
AVLTree.balance in at most 121 instructions. It permits an arbitrary old root cache and
requires height-consistent represented children. The implementation allocates no nodes;
it preserves every scalar payload field except the height, and all records outside the tree.
Balance.readHeights, Balance.inChild, and Balance.heavyCase give the shared assembly
blocks for cached-height selection, child reconnection, and single/double rotations.
Read the two cached child heights into scalar registers one and two.
Run a child-subtree block, reconnect its result, then return to the original parent.
Handle a heavy child: compare its two child heights and perform a single or double
rotation. side = true selects the left-heavy case and its final right rotation.
Balance at cursor zero using cached child/grandchild heights. heavy a b means
b + 1 < a; le compares heights. Pointer register one and scalar register zero
are client registers; the other declared scratch registers may change.
The concrete balance program implements AVLTree.balance in at most 121 instructions,
without allocation. Children must have consistent encoded caches; the old root cache may
be arbitrary. Register six retains the former root for the caller's subsequent reconnection.