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, 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 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(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,…,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(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 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(logn) time, but the results must be merged, requiring O(n) time. This returns 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, a data structure needs to be constructed so that all intervals overlapping another interval or point can be efficiently retrieved. First, the entire range of all the intervals is taken and divided in half at xcenter (in practice, xcenter should be picked to keep the tree relatively balanced). This gives three sets of intervals, those completely to the left of xcenter, called Sleft, those completely to the right of xcenter, called Sright, and those overlapping xcenter, called Scenter. The intervals in Sleft and Sright are recursively divided in the same manner until there are no intervals left. The intervals in Scenter 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:
A center point A pointer to another node containing all intervals completely to the left of the center point A pointer to another node containing all intervals completely to the right of the center point All intervals overlapping the center point sorted by their beginning point All intervals overlapping the center point sorted by their ending point
Intersecting Interval trees allow for quickly computing all the ranges overlapping any 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 xcenter, the midpoint used in node construction above. If x is less than xcenter, the leftmost set of intervals, Sleft, is considered. If x is greater than xcenter, the rightmost set of intervals, Sright, is considered.
As each node is processed as the tree is traversed from the root to a leaf, the ranges in its Scenter are processed. If x is less than xcenter, all intervals in Scenter must end after x, or they could not also overlap xcenter. Therefore, it is only necessary find those intervals in Scenter that begin before x. The lists of Scenter, which have already been constructed, can be consulted. Since only the interval beginnings are relevant in this scenario, the list can be sorted by beginnings. If the closest number no greater than x is found 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 (because they overlap xcenter which is larger than x). Thus, the intervals in the list can be enumerated until the startpoint value exceeds x. Likewise, if x is greater than xcenter, all intervals in Scenter must begin before x, so the intervals that end after x can be found using the list sorted by interval endings. If x exactly matches xcenter, all intervals in Scenter 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 the query interval q one of the following must hold:
either the start or end point of r is in q; or r completely encloses q.
… excerpt ends here. Continue reading the full article.


