Proofs: Cartesian tree
These are the human proofs of Cartesian tree, at full length.
The stack build gives the Cartesian tree, and range minima are lowest common ancestors full
This proof shows: correctness, termination.
Let be an array over a totally ordered set. Compare positions by the pairs , which are distinct; "the minimum of a range" means the position with the least pair, which is the leftmost minimum of the values. For let be the binary tree on the positions defined by: the root is the minimum of , the left subtree is and the right subtree is , where is empty. The Cartesian tree of is .
Lemma 1 (characterization). A binary tree on the positions is if and only if (1) its inorder is and (2) every node has a smaller pair than each of its descendants.
Proof. has both properties, by induction on : the inorder is , and has the least pair in . Conversely, let have both properties and root . By (2), has the least pair, so . By (1), the left subtree holds exactly the positions before in inorder, , and has both properties, so by induction it is ; the same holds on the right.
Lemma 2 (subtrees are ranges). In the subtree of each node is a range of positions, and is its minimum.
Proof. By the recursive definition, each subtree is some , whose node set is and whose root is the minimum of .
Lemma 3 (the spine). In the right spine, the path from the root that always takes the right child, consists of the positions such that for all with , in increasing order of position and of pair.
Proof. The spine of is the root followed by the spine of . By induction on , its nodes are and the positions of that beat every later position in . A position is not on the spine and is beaten by , which is later. The position beats everything after it. A position beats every later position of , which is all of its later positions. So the spine is as stated.
Lemma 4 (one round). Suppose that before round (lines L3 to L8) the arrays , , restricted to the positions describe , and the stack holds its right spine with the root at the bottom. Then after the round they describe , and holds its right spine.
Proof. Let the spine be , bottom to top. By Lemma 3 the pairs increase along it. Line L4 compares , which, since , is the same as . The loop pops the spine nodes with a larger pair than ; they form a top segment , because the pairs increase. Afterwards (or NIL if nothing was popped), and the top is (or the stack is empty if ).
Consider the tree produced by lines L6 and L7: becomes the right child of , replacing , and with its whole subtree becomes the left child of ; has no right child. If , is the root and the whole old tree is its left subtree.
Check property (1) of Lemma 1. The old right subtree of was the range , the end of the inorder. Now the right subtree of is with that range as its left subtree, so the inorder is the old one followed by . Check property (2). The ancestors of are , all with smaller pairs. The descendants of are the subtree of , the range ; its minimum is by Lemma 2, and has a larger pair than . All other ancestor relations are as before, except that was inserted between and , and in pairs. So by Lemma 1, .
Line L8 pushes , so the stack is . In the spine goes from the root along , then to the right child of , which has no right child. So the stack holds the spine of .
Termination and the result. Before round 0 the tree and the stack are empty, which describes . By Lemma 4 and induction on , after round the arrays describe , and the bottom of the stack is its root, which line L9 returns. Every round ends, because the loop of line L4 pops a node each time and the stack is finite; each position is pushed exactly once, so there are at most pops in all.
Lemma 5 (range minimum). For , the minimum of is in .
Proof. Let . By Lemma 2 the subtree of is a range that contains and , so it contains , and is the minimum of that range. If were outside , say , then and would both be in the right subtree of by property (1), and the right child of would be a deeper common ancestor. The case is the same with the left subtree. So , and as the minimum of a range that contains , it is the minimum of .