A small, dependency-free interval tree in Python: store a set of time ranges (or any numeric ranges) and answer two questions fast — which intervals overlap this one? and which intervals contain this point?
Useful wherever ranges collide: calendar conflict detection, booking systems, genomic feature lookup, memory-allocation maps.
A naive overlap search scans every stored interval — O(n) per query. An interval tree answers the same query in O(log n + k) average time, where k is the number of matches, by pruning whole subtrees that cannot overlap.
- Augmented BST keyed on start, with
max_endper node. Each node stores the largest end value in its subtree. During an overlap search, a left subtree whosemax_end <= query.startis skipped entirely — that single value is what turns a linear scan into a logarithmic one. - Half-open intervals
[start, end). Touching endpoints do not count as overlapping ([1, 3)and[3, 5)are conflict-free). This matches how bookings and schedules behave and removes a whole class of off-by-one bugs. - Balanced build for sorted input. Plain insertion degrades on
pre-sorted data, so
IntervalTree.from_intervalsinserts in median-first order, keeping depth near log n without a full self-balancing implementation. - Payloads are data, not keys. Intervals sort and compare on endpoints only, so any hashable payload (a meeting name, a record id) can ride along without breaking ordering.
from interval_tree import Interval, IntervalTree
tree = IntervalTree([
Interval(9, 10, "standup"),
Interval(9.5, 11, "deep work"),
Interval(13, 14, "lunch"),
])
tree.find_overlapping(Interval(9.75, 10.5))
# [Interval(9, 10, 'standup'), Interval(9.5, 11, 'deep work')]
tree.find_containing(13.5)
# [Interval(13, 14, 'lunch')]
tree.has_conflict(Interval(10, 11)) # True — overlaps "deep work"
tree.remove(Interval(9, 10, "standup"))pip install .
interval-tree -f schedule.txt overlap 9.75 10.5
interval-tree -f schedule.txt at 13.5
interval-tree -f schedule.txt conflict 10 11; echo $? # 0 = conflictInput file format — one interval per line, start end [label]:
9 10 standup
9.5 11 deep work
13 14 lunch
pip install pytest
pytestThe suite covers overlap/stabbing edge cases (touching endpoints, half-open end), removal of two-child nodes, and randomized cross-checks against a brute-force scan.
Python 3.9+ standard library only. No dependencies.