Proofs: Chtholly tree
These are the human proofs of Chtholly tree, at full length.
The Chtholly tree keeps its runs correct, and assignments cost O(log n) amortized full
This proof shows: correctness, complexity.
The structure is a set of runs ordered by . The invariant is:
(C) The runs are non-empty and disjoint, their union is , and for each run and each .
At the start holds one run per position, or one run per maximal block of equal values, and (C) holds.
Lemma 1 (SPLIT). Let (C) hold and . After SPLIT, (C) holds for the same array , and if some run starts at ; the result is that run. Every run that started at some point before still starts at .
Proof. If nothing changes. Otherwise, by (C), exactly one run has , and it is the run with the largest start , since the runs are disjoint and sorted by start. If it is returned. Else , and the two runs and are non-empty, cover the same positions as , and give them the same value. The other runs do not change.
Lemma 2 (ASSIGN). Let (C) hold and . After ASSIGN, (C) holds for the array with for and otherwise.
Proof. By Lemma 1, after line L6 a run starts at or , and after line L7 a run starts at ; the split at does not remove the start at . The runs with starts in are then those from lo up to hi, and since the run boundaries include and , their union is exactly . Line L8 deletes them and line L9 inserts , which covers the same positions, is non-empty, and has the value . The runs outside keep their positions and values.
Lemma 3 (EACH). After lines L10 and L11, the runs from lo up to hi tile in order. A change of in a visited run changes only on that run.
Proof. As in Lemma 2.
Theorem (amortized cost). Let the set be a balanced search tree, and let be the number of runs, so . Each split costs . Let an ASSIGN delete runs. Its actual cost is , and it changes by at most . With the potential for a large enough constant , the amortized cost is . An EACH that visits runs costs and raises by at most , so its amortized cost is . Starting from runs, and always, so operations cost in total.
Remark (no bound for EACH). The term cannot be bounded in general: with there are runs, and calls of EACH without assignments visit runs.