Proofs: Eytzinger layout
These are the human proofs of Eytzinger layout, at full length.
The Eytzinger lower bound search returns the slot of the first item not less than x, or 0 full
This proof shows: correctness, termination, complexity.
Let , , be built by EYTZINGER-BUILD from a sorted array . The implicit tree has the slots , and slot has the children and when they are at most . For a slot , write for its position in the in-order walk (left subtree, node, right subtree) from the root. Let be a key and .
The build. FILL visits the slots in in-order and writes in that order, so and . Hence items in in-order are sorted, and the slot with , when , holds the lower bound of . We prove that EYTZINGER-LOWER-BOUND returns that slot when and 0 when .
In-order facts. For a slot , every slot in the left subtree of has a smaller than , and every slot in the right subtree a larger one. Since and is sorted, holds exactly when .
The walk. Let be the values of at the tests of line L9, so and , and let , so . The walk stops: at least doubles in each pass, so . Each with is a child of , so is a path from the root; and is the number of a missing child of . If then , , and the result below is 0, which is correct.
Claim. Let be the set of steps with . If is empty then . Otherwise, with , .
Proof of the claim. Every slot of the tree is either some with , or lies in the subtree of the child of some that the walk did not take; for the walk leaves the tree, so the other child of is the only one not taken there. Take such a .
- If with , then .
- If is in the left subtree of and the walk went right at (), then .
- If with , then .
- If is in the right subtree of and the walk went left (), then .
So the slots with are exactly the nodes with and the right subtrees of those nodes. If is empty, no slot has , so . Otherwise, let . For with , the walk went left at , so is in the left subtree of , and so is the right subtree of ; hence and is less than every in the right subtree of . Also every slot in the right subtree of has . So has the smallest among the slots with . Since takes every value once and the slots with are those with , this smallest value is .
The bits. By and , the binary form of is , and the binary form of is . If is empty, has all its bits equal to 1, so it has trailing 1 bits, and line L11 gives . If is not empty, the bits after are all 1, since is the last left turn, so has exactly trailing 1 bits, preceded by the 0 bit . Shifting right by removes and leaves .
With the claim, line L12 returns , the slot with , when , and 0 when .
The count. The walk makes comparisons, where is the depth of the external position . Since , we get for .