Sub topic 11
Reporting Every Overlapping Interval
This chapter assumes you have read the previous chapters. You should know the overlap test and the max-endpoint invariant, where each node's max is the largest high endpoint in its subtree. You should also know the single-result search. Here you extend that search to return every overlapping interval.
Why the single-result search is not enough
find_overlap stops at the first match. It can also commit to one direction, because one overlap is all it needs. To report all matches, you may have to visit both subtrees of a node. Two rules decide which subtrees you can skip.
- Skip a subtree whose
maxis below the query's low endpoint. No interval in it ends late enough to reachlo. - Skip the right subtree when the current node's low endpoint is above the query's high endpoint. Every interval on the right starts at or after
node.lo, so none can start before the query ends.
The left subtree needs no extra check beyond rule 1. Intervals on the left have low endpoints no greater than node.lo, so rule 2 never rules them out.
The traversal
The function is an in-order traversal with pruning. It checks the node itself between the two recursive calls.
class Node:
def __init__(self, lo, hi):
self.lo = lo
self.hi = hi
self.max = hi
self.left = None
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_all(node, lo, hi, out):
# Appends every stored interval overlapping [lo, hi] to out.
if node is None or node.max < lo:
return
find_all(node.left, lo, hi, out)
if node.lo <= hi and lo <= node.hi:
out.append((node.lo, node.hi))
if node.lo <= hi:
find_all(node.right, lo, hi, out)
root = None
for lo, hi in [(15, 20), (10, 30), (17, 19), (5, 20), (12, 15), (30, 40)]:
root = insert(root, lo, hi)
result = []
find_all(root, 14, 16, result)
print(sorted(result)) # [(5, 20), (10, 30), (12, 15), (15, 20)]Tracing the query [14, 16]
The tree built above has root (15, 20). The query visits these nodes.
| Node | Subtree max | Action |
|---|---|---|
| (15, 20) | 40 | Go left first |
| (10, 30) | 30 | Go left first |
| (5, 20) | 20 | Overlaps, so report it |
| (10, 30) | 30 | Overlaps, so report it, then go right |
| (12, 15) | 15 | Overlaps, so report it |
| (15, 20) | 40 | Overlaps, so report it, then go right |
| (17, 19) | 40 | 17 is above 16, so no overlap. The right subtree, including (30, 40), is skipped. |
The traversal reports the intervals in order of low endpoint. The sorted call in the example is only there to make the printed list obvious.
Cost
Each reported interval costs at most one root-to-node path of visits. In a balanced tree of n intervals with k results, the worst case is O(min(n, k log n)). Many queries run closer to O(k + log n). When k is 0, the query behaves like a failed single search and takes O(log n). When k is close to n, the cost is O(n), and no method can beat that because the output itself has size k.
Exercise
Using the tree above, trace find_all for the query [45, 50]. How many nodes are visited, and what is returned?
Show answer
The root has max 40, which is below 45, so the first check prunes the whole tree. Only the root is visited, and nothing is reported. This agrees with find_overlap returning None for the same query.