Sub topic 15
An Alternative Design: The Centered Interval Tree
This chapter assumes you know the augmented-BST interval tree from earlier chapters, the stabbing query (which stored intervals contain a point x), and Big-O basics. The centered interval tree is a second design for the same problem. It builds the tree around a split point instead of ordering nodes by low endpoint.
The Idea
Each node picks a center value. Every interval then falls into one of three groups:
- Intervals entirely left of the center go to the left subtree.
- Intervals entirely right of the center go to the right subtree.
- Intervals that contain the center stay in the node itself.
The node keeps its own intervals in two sorted lists: one by low endpoint ascending, one by high endpoint descending.
Building the Tree
This version uses the median of all endpoints as the center. That center is an endpoint of some interval, so every node stores at least one interval and the recursion ends.
class CNode:
def __init__(self, intervals):
endpoints = sorted(p for iv in intervals for p in iv)
self.center = endpoints[len(endpoints) // 2]
left = [iv for iv in intervals if iv[1] < self.center]
right = [iv for iv in intervals if iv[0] > self.center]
mid = [iv for iv in intervals if iv[0] <= self.center <= iv[1]]
self.by_low = sorted(mid)
self.by_high = sorted(mid, key=lambda iv: iv[1], reverse=True)
self.left = CNode(left) if left else None
self.right = CNode(right) if right else None
def stab(node, x, out):
# Appends every stored interval containing point x to out.
if node is None:
return
if x < node.center:
for iv in node.by_low:
if iv[0] > x:
break
out.append(iv)
stab(node.left, x, out)
elif x > node.center:
for iv in node.by_high:
if iv[1] < x:
break
out.append(iv)
stab(node.right, x, out)
else:
out.extend(node.by_low)
tree = CNode([(15, 20), (10, 30), (17, 19), (5, 20), (12, 15), (30, 40)])
result = []
stab(tree, 16, result)
print(sorted(result)) # [(5, 20), (10, 30), (15, 20)]Why the Query Works
Suppose x is less than the center. Every interval stored in the node reaches at least the center, so its high endpoint is already greater than x. Such an interval contains x exactly when its low endpoint is at most x. Scanning by_low from the smallest low endpoint, you can stop at the first interval that starts after x. Every interval before that point is a hit, so the scan never wastes work on a miss. The case x greater than the center is symmetric and uses by_high. Intervals in the right subtree all start after the center, so they cannot contain a smaller x. The search therefore descends into only one child.
Trade-offs
| Property | Augmented BST | Centered tree |
|---|---|---|
| Stabbing query | O(log n + k) with the all-overlaps traversal | O(log n + k) |
| Insert and delete | O(log n) with rebalancing | Awkward, since the center is fixed at build time |
| Best fit | Dynamic sets | Static or rarely changing sets |
Here k is the number of reported intervals. The centered tree's weakness is updates. Adding an interval can require a new center or a rebuild, and a rebuild costs O(n log n). Choose it when the set is built once and queried many times.
Exercise
In a centered node with center 50, the stored intervals are (40, 60), (45, 90) and (10, 55). For the query point x = 42, which intervals does the node report, and which list does it scan?
Show answer
It scans by_low, which is (10, 55), (40, 60), (45, 90), because x < 50. It reports (10, 55) and (40, 60), since their low endpoints are at most 42. It stops at (45, 90) because 45 > 42. It then continues into the left subtree only.