Lably
Skip to content

Interval tree

Tree data structure to hold intervals

  • 15 min read
  • 4 chapters

Related on Lably

Through Computer science, Interval (mathematics) and Binary tree

The trivial solution to finding all overlapping intervals 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, output-sensitive algorithms, where the runtime is expressed in terms of m (the number of intervals produced by the query) may also be considered. Interval trees have a query time of O(log⁡n+m) and an initial creation time of O(nlog⁡n), while limiting memory consumption to O(n). After creation, interval trees may be dynamic, allowing efficient insertion and deletion of an interval in O(log⁡n) time. If the endpoints of intervals are within a small integer range (e.g., in the range [1,…,O(n)]), faster and in fact optimal data structures exist with preprocessing time O(n) and query time O(1+m) for reporting m intervals containing a given query point (see for a very simple one).

Naïve 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(log⁡n) 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 naïve 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(log⁡n) time, but the results must be merged, requiring O(n) time. This returns queries in O(n+log⁡n)=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.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication