Proofs: Range min-max tree
These are the human proofs of Range min-max tree, at full length.
Correctness of the range min-max tree searches full
This proof shows: correctness.
Let be the excess of , with . For a tree node with range , the stored values are , and ; padding leaves have empty ranges, and .
Lemma 1 (the build). If has children over and over , then , and . For in , , so the minimum over the right half, relative to , is ; the rest is immediate. Padding values are neutral for and .
Lemma 2 (discrete continuity). Let . An integer equals for some in if and only if . Necessity is the definition of and . For sufficiency, the values change by exactly from one to the next, and they include and ; the walk between these two indices passes through every integer between them.
FWDSEARCH. Let and let be the block of . Line L2 checks every position after in block , in order, so it returns the first occurrence if it lies there.
Otherwise, consider the climb. Let be the leaf of and its ancestors. Claim: at the start of each iteration with current node , (a) , and (b) no position after and up to the end of the range of has excess . Initially is set to the excess at the end of block , and (b) is what line L2 established. If is a right child, the range of its parent ends where the range of ends, so moving up keeps (a) and (b). If is a left child with right sibling , the range of starts right after the range of , so is the excess just before . By Lemma 2, contains a position with excess if and only if the test of line L5 succeeds. If it does, every position from after to the end of is excluded by (b), so the first occurrence after lies in , and DESCEND is called with the correct . If it does not, the range of the parent ends where ends, line L6 adds to make the excess there, and (b) extends to the range of the parent. If the root is reached, (b) covers all of after , so there is no occurrence, and line L8 returns none.
DESCEND. Claim: at each step, is the excess just before the range of , and the first occurrence of in the range of the node first passed to DESCEND lies in the range of . Initially this holds by the above. At an internal with children and , Lemma 2 tells whether contains an occurrence; if it does, the first occurrence in is in , which starts with the same . If not, it is in , and is the excess just before . At a leaf, line L12 scans in order and returns the first occurrence, which exists by the claim.
BWDSEARCH. The mirror argument: scanning the block of leftward finds the last occurrence before there; on the climb, is the excess at the end of the stretch to the left of what has been excluded, the left sibling starts with excess , and by Lemma 2 the test decides whether contains an occurrence; the descent prefers right children and scans the leaf from its end, keeping equal to the excess at the end of the current range. If the root is reached with no occurrence in , the only remaining candidate is the virtual position , whose excess is 0.
FINDCLOSE, FINDOPEN, ENCLOSE. For an opening parenthesis at , every position strictly inside its pair has excess at least , and its matching close is the first position after with excess , which is . For a closing parenthesis at with match , and every with has , so . For an opening parenthesis at with , the enclosing pair opens at the greatest with and ; the position then has excess , and no position in does, because the pair opened at is still open there, so . If , no position before has excess , and none is returned.
RMQ. The partial blocks are scanned exactly, and the canonical nodes of the full blocks are folded left to right with equal to the excess before each, so by the definition of the fold gives the minimum over . If , every position in before the first one with excess has excess above , and the first such position after is , which lies in because is reached there.