Proofs: Interval tree
These are the human proofs of Interval tree, at full length.
SEARCH and REPORT of the interval tree are correct, and REPORT visits O((k + 1) h) nodes full
This proof shows: correctness, complexity.
The tree is a binary search tree of closed intervals ordered by the left end : every interval in the left subtree of has , and every interval in the right subtree has . Each node has equal to the largest right end in its subtree, and . Intervals and overlap iff and . Let be the height of (the number of nodes on a longest path from the root).
Lemma 1. Let be a node, and suppose and . If no interval of the left subtree overlaps , then no interval of the right subtree overlaps .
Proof. Some interval of the left subtree has . It does not overlap, so . Every interval of the right subtree has , so it does not overlap either.
Lemma 2. If or , then no interval of the left subtree overlaps .
Proof. Each of them has .
Theorem 1 (SEARCH). SEARCH returns an interval that overlaps if one exists, and nil otherwise, after at most passes of the loop.
Proof. Invariant: if some interval of overlaps , then one lies in the subtree of (with the subtree of nil empty). It holds at the start with the root. Suppose it holds and the loop runs, so does not overlap. If line L3 moves to the left child and the left subtree holds no overlapping interval, then by Lemma 1 the right subtree holds none, and itself does not overlap, so no interval of the subtree of overlaps, and by the invariant none of does; the invariant holds for the left child, vacuously or not. If line L4 moves right, by Lemma 2 the left subtree has no overlap and has none, so an overlapping interval of the subtree of is in the right subtree. Each pass goes one level down, so the loop stops after at most passes. If it stops at a node, the loop test says that node overlaps. If it stops at nil, the subtree of nil is empty, so by the invariant no interval overlaps.
Theorem 2 (REPORT). REPORT appends exactly the intervals of the subtree of that overlap , each once.
Proof. By induction on the size of the subtree. For nil nothing is appended, which is right. If , every interval of the subtree ends before , and nothing is right. Otherwise line L7 appends the overlapping intervals of the left subtree, by the induction hypothesis, and line L8 appends iff it overlaps. If , line L9 appends those of the right subtree. If , every interval of the right subtree has , and none overlaps. The three parts are disjoint.
Theorem 3 (cost of REPORT). If REPORT outputs intervals, at most of its calls pass line L6, and it makes at most calls in total.
Proof. Let be the search path of : start at the root, and at a node go right if and left otherwise. Call a node expanded if a call on it passes line L6.
Let be expanded. By line L9, every right turn on the path from the root to is at a node with . Suppose the path also turns left at some with . Then every interval below has . Since passes line L6, an interval below (or itself) has , so it overlaps , and by Theorem 2 it is output; so lies on the path from the root to an output. Otherwise every left turn is at a with and every right turn at a with , so lies on .
So the expanded nodes lie on the union of and the root paths of the outputs, which has at most nodes. Each call is on the root or on a child of an expanded node, so there are at most calls. Each node is called at most once, and the calls on nil are at most , so there are at most calls.