Sub topic 1
Intervals and Their Endpoints
An interval tree stores a changing set of intervals and answers the question "which stored intervals overlap this one?" without scanning all of them. This chapter covers the vocabulary and the basic data you will work with. Later chapters build the tree.
Prerequisites
This chapter assumes you know the following:
- Binary search trees: ordering, insertion, search, and in-order traversal.
- Big-O notation and tree height.
- Basic recursion.
- How to read short Python code.
What an Interval Is
An interval is a range of values on a line, such as a time span or a range of positions. It is described by two numbers:
- The low endpoint, written
lo, is where the interval starts. - The high endpoint, written
hi, is where it ends.
We always require lo <= hi. In this tutorial, intervals are closed. A closed interval [lo, hi] includes both endpoints. So [5, 9] contains 5, 9, and every value between them.
Real data often looks like this:
- A meeting from 9 to 10.
- A reservation on days 3 to 7.
- A gene occupying positions 1200 to 1850.
Representing Intervals in Python
The simplest representation is a tuple (lo, hi). The code in this tutorial uses tuples.
meeting = (9, 10)
lo, hi = meeting
length = hi - lo
print(lo, hi, length) # 9 10 1A single point is a valid interval where lo == hi, for example (7, 7). Its length is 0, but it still contains one value.
Why Closed Intervals Matter
Whether endpoints count changes the answers you get. Take [5, 9] and [9, 12]. Both contain the value 9. Because the intervals are closed, they share a point and therefore overlap. If they were half-open, written [5, 9) and [9, 12), they would only touch and would not overlap.
Many libraries use half-open intervals, so check which convention your data uses before you reuse any code. This tutorial sticks to closed intervals throughout.
What We Will Ask About Intervals
Two kinds of questions come up repeatedly:
- Overlap query: given a query interval, which stored intervals share at least one point with it?
- Stabbing query: given a single point, which stored intervals contain it? This is an overlap query where the query interval is
(x, x).
A fast structure must handle these while intervals are inserted and deleted. Sorting the intervals once is not enough, because the set changes.
Common Mistakes
- Mixing conventions. Treating some data as closed and some as half-open gives wrong results at the endpoints.
- Allowing
lo > hi. Later tests assumelo <= hi. Validate input when intervals enter the system. - Ignoring duplicates. Two stored intervals can be identical, and the tree must accept that.
Exercise
Do the closed intervals [5, 9] and [9, 12] overlap? Would the half-open intervals [5, 9) and [9, 12) overlap?
Show answer
The closed intervals overlap, because both contain the value 9. The half-open intervals do not overlap, because the first one excludes 9.
Exercise
Write a Python expression that checks whether a tuple (lo, hi) is a valid closed interval, and say what a stabbing query for the point 12 looks like as an overlap query.
Show answer
The check is lo <= hi. The stabbing query for 12 is an overlap query with the interval (12, 12).