Sub topic 10
Why the Search Never Misses an Overlap
This chapter assumes you have read the previous chapters. You should know the overlap test, the max-endpoint invariant, and the find_overlap loop. Here we prove that the loop is correct.
The Claim
find_overlap checks the current node. If that node doesn't overlap the query, it moves to one child and never looks at the other. We need to show that this never skips the only overlap.
Let the query be [lo, hi]. Assume the current node x does not overlap it. Recall two invariants:
- The tree is ordered by low endpoint. Every interval in the left subtree has a low endpoint smaller than
x.lo. Every interval in the right subtree has a low endpoint at leastx.lo. left.maxis the largest high endpoint of any interval in the left subtree.
Case 1: The Left Subtree Cannot Overlap
Suppose the left child is missing, or left.max < lo. Every interval in the left subtree has a high endpoint at most left.max. That is below lo, so each of those intervals ends before the query starts. None of them overlaps.
The only place left to look is the right subtree, and the search goes right.
Case 2: The Left Subtree May Overlap
Suppose left.max >= lo. Let m be an interval in the left subtree whose high endpoint equals left.max. Because m.hi >= lo, the interval m does not end before the query starts. There are two possibilities.
moverlaps the query. The left subtree contains an overlap, and going left finds one.mdoes not overlap the query. Sincem.hi >= lo, the only way to miss ism.lo > hi, somstarts after the query ends.
In the second possibility, look at the right subtree. Each interval r there has r.lo >= x.lo. Because m is in the left subtree, x.lo > m.lo. So r.lo > m.lo > hi. Every interval in the right subtree starts after the query ends, and none of them overlaps.
In both possibilities, going left is safe. If any overlap exists anywhere below x, the left subtree contains one.
Checking the Argument by Brute Force
A proof is worth confirming with a test. This script compares find_overlap with a linear scan on random data. It uses the node class, insert, and find_overlap from the earlier chapters, repeated here so it runs on its own.
import random
class Node:
def __init__(self, lo, hi):
self.lo, self.hi, self.max = lo, hi, hi
self.left = self.right = None
def insert(node, lo, hi):
if node is None:
return Node(lo, hi)
if lo < node.lo:
node.left = insert(node.left, lo, hi)
else:
node.right = insert(node.right, lo, hi)
node.max = max(node.max, hi)
return node
def find_overlap(node, lo, hi):
while node is not None:
if node.lo <= hi and lo <= node.hi:
return (node.lo, node.hi)
if node.left is not None and node.left.max >= lo:
node = node.left
else:
node = node.right
return None
random.seed(1)
for _ in range(2000):
items = []
root = None
for _ in range(random.randint(0, 12)):
a = random.randint(0, 50)
b = a + random.randint(0, 10)
items.append((a, b))
root = insert(root, a, b)
q_lo = random.randint(0, 60)
q_hi = q_lo + random.randint(0, 10)
expected = any(a <= q_hi and q_lo <= b for a, b in items)
found = find_overlap(root, q_lo, q_hi)
assert (found is not None) == expected
print("search agrees with the linear scan")Exercise
In Case 2, why must every interval in the right subtree start after hi when m does not overlap the query?
Show answer
m has m.hi >= lo but does not overlap, so m.lo > hi. Every right-subtree interval has a low endpoint at least x.lo, and x.lo is greater than m.lo because m is in the left subtree. So each one starts after m.lo, which is already after hi.