Sub topic 9
Searching for One Overlapping Interval
This chapter assumes you know how a binary search tree (BST) works, can read short Python, and understand tree height and Big-O. It also assumes you have built the tree from the previous chapters. Each node stores an interval (lo, hi). The tree is ordered by lo. Each node's max field holds the largest hi in its subtree.
The goal
Given a query interval [lo, hi], return any one stored interval that overlaps it, or None if there is none. The search follows one path from the root downward, so it costs O(h), where h is the tree height.
The decision rule
At each node, the search does three things in order:
- If the node's interval overlaps the query, return it.
- If the left child exists and
left.max >= lo, go left. - Otherwise go right.
The code
class Node:
def __init__(self, lo, hi):
self.lo = lo
self.hi = hi
self.max = hi # largest high endpoint in this node's subtree
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_overlap(node, lo, hi):
# Returns one stored interval overlapping [lo, hi], or None.
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
root = None
for lo, hi in [(15, 20), (10, 30), (17, 19), (5, 20), (12, 15), (30, 40)]:
root = insert(root, lo, hi)
print(find_overlap(root, 21, 23)) # (10, 30)
print(find_overlap(root, 45, 50)) # NoneTracing the two queries
For the query [21, 23], the root (15, 20) does not overlap, because 21 is greater than 20. The left child (10, 30) has max 30, which is at least 21, so the search goes left. That node overlaps the query, so the search returns (10, 30).
For the query [45, 50], the root does not overlap. The left child's max is 30, which is below 45, so no interval in the left subtree can reach the query. The search goes right to (30, 40). That node does not overlap either, and it has no children, so the search returns None.
Common errors
- Comparing
node.left.maxwithhiinstead oflo. The left subtree can only help if some interval in it ends at or after the query starts. - Forgetting the
node.left is not Nonecheck. An empty left subtree has nomaxto read. - Using strict inequalities. For closed intervals,
(5, 9)and(9, 12)overlap, so the tests must use<=and>=. - Reading stale
maxvalues. The search trusts the invariant, so any code that changes the tree must keep everymaxcorrect.
The next chapter explains why going right is safe when the left test fails.
Exercise
Using the tree built above, trace find_overlap(root, 1, 3). Which nodes are visited, and what is returned?
Show answer
The search visits (15, 20), then (10, 30), then (5, 20), and returns None. At (15, 20), no overlap, and left.max is 30, which is at least 1, so it goes left. At (10, 30), no overlap, and left.max is 20, which is at least 1, so it goes left. At (5, 20), 5 <= 3 is false, so there is no overlap. It has no left child, so the search goes right, and the right child is empty. The result is None.