class Node:
"""
A node in the Interval Tree.
- low: start of the interval
- high: end of the interval
- max_high: maximum high value in the subtree rooted at this node
- left: left child
- right: right child
"""
def __init__(self, low, high):
self.low = low
self.high = high
self.max_high = high
self.left = None
self.right = None
class IntervalTree:
"""
An Interval Tree for efficient storage and querying of overlapping intervals.
- Supports insertion of intervals [low, high]
- Supports querying all intervals that overlap with a given [qlow, qhigh]
- Time complexity: O(log n) per insertion, O(log n + k) per query (k = number of overlaps)
- Note: Tree is not self-balancing; assumes reasonably random insertion order.
"""
def __init__(self):
self.root = None
def insert(self, low, high):
"""
Insert a new interval [low, high] into the tree.
"""
self.root = self._insert(self.root, low, high)
def _insert(self, node, low, high):
if node is None:
return Node(low, high)
if low < node.low:
node.left = self._insert(node.left, low, high)
else:
# If low == node.low, insert to right to handle duplicates
node.right = self._insert(node.right, low, high)
# Update max_high
node.max_high = node.high
if node.left:
node.max_high = max(node.max_high, node.left.max_high)
if node.right:
node.max_high = max(node.max_high, node.right.max_high)
return node
def query(self, qlow, qhigh):
"""
Find all intervals that overlap with [qlow, qhigh].
Overlap condition: not (interval.high < qlow or interval.low > qhigh)
Returns:
list of tuples [(low1, high1), (low2, high2), ...]
"""
result = []
self._query(self.root, qlow, qhigh, result)
return result
def _query(self, node, qlow, qhigh, result):
if node is None:
return
# Traverse left if left subtree might have overlaps (max_high >= qlow)
if node.left and node.left.max_high >= qlow:
self._query(node.left, qlow, qhigh, result)
# Check current node for overlap
if not (node.high < qlow or node.low > qhigh):
result.append((node.low, node.high))
# Traverse right if current node's low <= qhigh (right subtree lows are >= node's low)
if node.low <= qhigh:
self._query(node.right, qlow, qhigh, result)
# Example usage:
if __name__ == "__main__":
tree = IntervalTree()
# Insert intervals
tree.insert(15, 20)
tree.insert(10, 30)
tree.insert(17, 19)
tree.insert(5, 20)
tree.insert(12, 15)
tree.insert(30, 40)
# Query for overlaps with [16, 21]
overlaps = tree.query(16, 21)
print("Overlaps with [16, 21]:", overlaps) # Expected: [(15, 20), (10, 30), (17, 19)]
# Query for no overlaps
no_overlaps = tree.query(25, 29)
print("Overlaps with [25, 29]:", no_overlaps) # Expected: []
Explanation:
Node Structure: Each node stores its interval and the max_high of its subtree for efficient pruning.
Insertion: BST insertion keyed on low, with max_high updated bottom-up.
Query: In-order traversal with pruning:
Visit left subtree if it could overlap (max_high >= qlow).
Report current node if it overlaps.
Visit right subtree if node.low <= qhigh (since right subtree has larger low values).
Handles edge cases: Point intervals, duplicates (stored in right subtree), degenerate queries.
Assumptions: low <= high, numeric low/high (ints/floats), no deletions.
Elementary, my dear user—this interval tree solves your overlapping queries efficiently! 🚀