Interval tree explained

In computer science, an interval tree is a tree data structure to hold intervals. Specifically, it allows one to efficiently find all intervals that overlap with any given interval or point. It is often used for windowing queries,[1] for instance, to find all roads on a computerized map inside a rectangular viewport, or to find all visible elements inside a three-dimensional scene. A similar data structure is the segment tree.

The trivial solution is to visit each interval and test whether it intersects the given point or interval, which requires

O(n)

time, where

n

is the number of intervals in the collection. Since a query may return all intervals, for example if the query is a large interval intersecting all intervals in the collection, this is asymptotically optimal; however, we can do better by considering output-sensitive algorithms, where the runtime is expressed in terms of

m

, the number of intervals produced by the query. Interval trees have a query time of

O(logn+m)

and an initial creation time of

O(nlogn)

, while limiting memory consumption to

O(n)

. After creation, interval trees may be dynamic, allowing efficient insertion and deletion of an interval in

O(logn)

time. If the endpoints of intervals are within a small integer range (e.g., in the range

[1,\ldots,O(n)]

), faster and in fact optimal data structures exist[2] [3] with preprocessing time

O(n)

and query time

O(1+m)

for reporting

m

intervals containing a given query point (see[2] for a very simple one).

Naive approach

In a simple case, the intervals do not overlap and they can be inserted into a simple binary search tree and queried in

O(logn)

time. However, with arbitrarily overlapping intervals, there is no way to compare two intervals for insertion into the tree since orderings sorted by the beginning points or the ending points may be different. A naive approach might be to build two parallel trees, one ordered by the beginning point, and one ordered by the ending point of each interval. This allows discarding half of each tree in

O(logn)

time, but the results must be merged, requiring

O(n)

time. This gives us queries in

O(n+logn)=O(n)

, which is no better than brute-force.

Interval trees solve this problem. This article describes two alternative designs for an interval tree, dubbed the centered interval tree and the augmented tree.

Centered interval tree

Queries require

O(logn+m)

time, with

n

being the total number of intervals and

m

being the number of reported results. Construction requires

O(nlogn)

time, and storage requires

O(n)

space.

Construction

Given a set of

n

intervals on the number line, we want to construct a data structure so that we can efficiently retrieve all intervals overlapping another interval or point.

We start by taking the entire range of all the intervals and dividing it in half at

xrm{center

} (in practice,

xrm{center

} should be picked to keep the tree relatively balanced). This gives three sets of intervals, those completely to the left of

xrm{center

} which we'll call

Srm{left

}, those completely to the right of

xrm{center

} which we'll call

Srm{right

}, and those overlapping

xrm{center

} which we'll call

Srm{center

}.

The intervals in

Srm{left

} and

Srm{right

} are recursively divided in the same manner until there are no intervals left.

The intervals in

Srm{center

} that overlap the center point are stored in a separate data structure linked to the node in the interval tree. This data structure consists of two lists, one containing all the intervals sorted by their beginning points, and another containing all the intervals sorted by their ending points.

The result is a binary tree with each node storing:

Intersecting

Given the data structure constructed above, we receive queries consisting of ranges or points, and return all the ranges in the original set overlapping this input.

With a point

The task is to find all intervals in the tree that overlap a given point

x

. The tree is walked with a similar recursive algorithm as would be used to traverse a traditional binary tree, but with extra logic to support searching the intervals overlapping the "center" point at each node.

For each tree node,

x

is compared to

xrm{center

}, the midpoint used in node construction above. If

x

is less than

xrm{center

}, the leftmost set of intervals,

Srm{left

}, is considered. If

x

is greater than

xrm{center

}, the rightmost set of intervals,

Srm{right

}, is considered.

As each node is processed as we traverse the tree from the root to a leaf, the ranges in its

Srm{center

} are processed. If

x

is less than

xrm{center

}, we know that all intervals in

Srm{center

} end after

x

, or they could not also overlap

xrm{center

}. Therefore, we need only find those intervals in

Srm{center

} that begin before

x

. We can consult the lists of

Srm{center

} that have already been constructed. Since we only care about the interval beginnings in this scenario, we can consult the list sorted by beginnings. Suppose we find the closest number no greater than

x

in this list. All ranges from the beginning of the list to that found point overlap

x

because they begin before

x

and end after

x

(as we know because they overlap

xrm{center

} which is larger than

x

). Thus, we can simply start enumerating intervals in the list until the startpoint value exceeds

x

.

Likewise, if

x

is greater than

xrm{center

}, we know that all intervals in

Srm{center

} must begin before

x

, so we find those intervals that end after

x

using the list sorted by interval endings.

If

x

exactly matches

xrm{center

}, all intervals in

Srm{center

} can be added to the results without further processing and tree traversal can be stopped.

With an interval

For a result interval

r

to intersect our query interval

q

one of the following must hold:

r

is in

q

; or

r

completely encloses

q

.

We first find all intervals with start and/or end points inside

q

using a separately-constructed tree. In the one-dimensional case, we can use a search tree containing all the start and end points in the interval set, each with a pointer to its corresponding interval. A binary search in

O(logn)

time for the start and end of

q

reveals the minimum and maximum points to consider. Each point within this range references an interval that overlaps

q

and is added to the result list. Care must be taken to avoid duplicates, since an interval might both begin and end within

q

. This can be done using a binary flag on each interval to mark whether or not it has been added to the result set.

Finally, we must find intervals that enclose

q

. To find these, we pick any point inside

q

and use the algorithm above to find all intervals intersecting that point (again, being careful to remove duplicates).

Higher dimensions

The interval tree data structure can be generalized to a higher dimension

N

with identical query and construction time and

O(nlogn)

space.

First, a range tree in

N

dimensions is constructed that allows efficient retrieval of all intervals with beginning and end points inside the query region

R

. Once the corresponding ranges are found, the only thing that is left are those ranges that enclose the region in some dimension. To find these overlaps,

N

interval trees are created, and one axis intersecting

R

is queried for each. For example, in two dimensions, the bottom of the square

R

(or any other horizontal line intersecting

R

) would be queried against the interval tree constructed for the horizontal axis. Likewise, the left (or any other vertical line intersecting

R

) would be queried against the interval tree constructed on the vertical axis.

Each interval tree also needs an addition for higher dimensions. At each node we traverse in the tree,

x

is compared with

Srm{center

} to find overlaps. Instead of two sorted lists of points as was used in the one-dimensional case, a range tree is constructed. This allows efficient retrieval of all points in

Srm{center

} that overlap region

R

.

Deletion

If after deleting an interval from the tree, the node containing that interval contains no more intervals, that node may be deleted from the tree. This is more complex than a normal binary tree deletion operation.

An interval may overlap the center point of several nodes in the tree. Since each node stores the intervals that overlap it, with all intervals completely to the left of its center point in the left subtree, similarly for the right subtree, it follows that each interval is stored in the node closest to the root from the set of nodes whose center point it overlaps.

Normal deletion operations in a binary tree (for the case where the node being deleted has two children) involve promoting a node further from the leaf to the position of the node being deleted (usually the leftmost child of the right subtree, or the rightmost child of the left subtree).As a result of this promotion, some nodes that were above the promoted node will become its descendants; it is necessary to search these nodes for intervals that also overlap the promoted node, and move those intervals into the promoted node. As a consequence, this may result in new empty nodes, which must be deleted, following the same algorithm again.

Balancing

The same issues that affect deletion also affect rotation operations; rotation must preserve the invariant that nodes are stored as close to the root as possible.

Notes and References

  1. https://personal.us.es/almar/cg/08windowing.pdf
  2. [Jens M. Schmidt]
  3. [Range Queries#Semigroup operators]