Algorithm Search⌘K

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 TT is a binary search tree of closed intervals ordered by the left end lolo: every interval in the left subtree of xx has lo≤x.lolo \le x.lo, and every interval in the right subtree has lo≥x.lolo \ge x.lo. Each node has x.maxx.\mathit{max} equal to the largest right end in its subtree, and nil.max=−∞\mathit{nil}.\mathit{max} = -\infty. Intervals [lo,hi][lo, hi] and [a,b][a, b] overlap iff lo≤blo \le b and a≤hia \le hi. Let hh be the height of TT (the number of nodes on a longest path from the root).

Lemma 1. Let xx be a node, and suppose x.left≠nilx.\mathit{left} \ne \mathit{nil} and x.left.max≥ax.\mathit{left}.\mathit{max} \ge a. If no interval of the left subtree overlaps [a,b][a, b], then no interval of the right subtree overlaps [a,b][a, b].

Proof. Some interval [lo′,hi′][lo', hi'] of the left subtree has hi′=x.left.max≥ahi' = x.\mathit{left}.\mathit{max} \ge a. It does not overlap, so lo′>blo' > b. Every interval [lo′′,hi′′][lo'', hi''] of the right subtree has lo′′≥x.lo≥lo′>blo'' \ge x.lo \ge lo' > b, so it does not overlap either. □\square

Lemma 2. If x.left=nilx.\mathit{left} = \mathit{nil} or x.left.max<ax.\mathit{left}.\mathit{max} < a, then no interval of the left subtree overlaps [a,b][a, b].

Proof. Each of them has hi≤x.left.max<ahi \le x.\mathit{left}.\mathit{max} < a. □\square

Theorem 1 (SEARCH). SEARCH(T,a,b)(T, a, b) returns an interval that overlaps [a,b][a, b] if one exists, and nil otherwise, after at most hh passes of the loop.

Proof. Invariant: if some interval of TT overlaps [a,b][a, b], then one lies in the subtree of xx (with the subtree of nil empty). It holds at the start with xx the root. Suppose it holds and the loop runs, so xx 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 xx itself does not overlap, so no interval of the subtree of xx overlaps, and by the invariant none of TT 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 xx has none, so an overlapping interval of the subtree of xx is in the right subtree. Each pass goes one level down, so the loop stops after at most hh 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. □\square

Theorem 2 (REPORT). REPORT(x,a,b,out)(x, a, b, out) appends exactly the intervals of the subtree of xx that overlap [a,b][a, b], each once.

Proof. By induction on the size of the subtree. For nil nothing is appended, which is right. If x.max<ax.\mathit{max} < a, every interval of the subtree ends before aa, and nothing is right. Otherwise line L7 appends the overlapping intervals of the left subtree, by the induction hypothesis, and line L8 appends xx iff it overlaps. If x.lo≤bx.lo \le b, line L9 appends those of the right subtree. If x.lo>bx.lo > b, every interval of the right subtree has lo≥x.lo>blo \ge x.lo > b, and none overlaps. The three parts are disjoint. □\square

Theorem 3 (cost of REPORT). If REPORT(T.root,a,b)(T.\mathit{root}, a, b) outputs kk intervals, at most (k+1)h(k + 1) h of its calls pass line L6, and it makes at most min⁡(2(k+1)h+1,2n+1)\min(2 (k + 1) h + 1, 2n + 1) calls in total.

Proof. Let PP be the search path of bb: start at the root, and at a node yy go right if y.lo≤by.lo \le b and left otherwise. Call a node expanded if a call on it passes line L6.

Let xx be expanded. By line L9, every right turn on the path from the root to xx is at a node yy with y.lo≤by.lo \le b. Suppose the path also turns left at some yy with y.lo≤by.lo \le b. Then every interval below xx has lo≤y.lo≤blo \le y.lo \le b. Since xx passes line L6, an interval below xx (or xx itself) has hi=x.max≥ahi = x.\mathit{max} \ge a, so it overlaps [a,b][a, b], and by Theorem 2 it is output; so xx lies on the path from the root to an output. Otherwise every left turn is at a yy with y.lo>by.lo > b and every right turn at a yy with y.lo≤by.lo \le b, so xx lies on PP.

So the expanded nodes lie on the union of PP and the kk root paths of the outputs, which has at most (k+1)h(k + 1) h nodes. Each call is on the root or on a child of an expanded node, so there are at most 2(k+1)h+12 (k + 1) h + 1 calls. Each node is called at most once, and the calls on nil are at most n+1n + 1, so there are at most 2n+12n + 1 calls. □\square

Back to Interval tree