In computer science, a segment tree, also known as a statistic tree, is a tree data structure used for storing information about intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, it's a structure that cannot be modified once it's built. A similar data structure is the interval tree. A segment tree for a set of n intervals uses O storage and can be built in O time. Segment trees support searching for all the intervals that contain a query point in O, k being the number of retrieved intervals or segments. Applications of the segment tree are in the areas of computational geometry, and geographic information systems. The segment tree can be generalized to higher dimension spaces.
This section describes the structure of a segment tree in a one-dimensional space. Let S be a set of intervals, or segments. Let p1, p2,..., pm be the list of distinct interval endpoints, sorted from left to right. Consider the partitioning of the real line induced by those points. The regions of this partitioning are called elementary intervals. Thus, the elementary intervals are, from left to right: That is, the list of elementary intervals consists of open intervals between two consecutive endpoints pi and pi+1, alternated with closed intervals consisting of a single endpoint. Single points are treated themselves as intervals because the answer to a query is not necessarily the same at the interior of an elementary interval and its endpoints. Given a set of intervals, or segments, a segment tree T for is structured as follows:
Its leaves correspond to the elementary intervals induced by the endpoints in, in an ordered way: the leftmost leaf corresponds to the leftmost interval, and so on. The elementary interval corresponding to a leaf v is denoted Int.
The internal nodes of T correspond to intervals that are the union of elementary intervals: the interval Int corresponding to node N is the union of the intervals corresponding to the leaves of the tree rooted at N. That implies that Int is the union of the intervals of its two children.
Each node or leaf v in T stores the interval Int and a set of intervals, in some data structure. This canonical subset of node v contains the intervals from such that contains Int and does not contain Int. That is, each node in T stores the segments that span through its interval, but do not span through the interval of its parent.
Storage requirements
This section analyzes the storage cost of a segment tree in a one-dimensional space. A segment tree T on a set of n intervals uses O storage.
Construction
This section describes the construction of a segment tree in a one-dimensional space. A segment tree from the set of segments, can be built as follows. First, the endpoints of the intervals in are sorted. The elementary intervals are obtained from that. Then, a balanced binary tree is built on the elementary intervals, and for each node v it is determined the interval Int it represents. It remains to compute the canonical subsets for the nodes. To achieve this, the intervals in are inserted one by one into the segment tree. An interval X = can be inserted in a subtree rooted at T, using the following procedure:
If Int is contained in X then store X at T, and finish.
Else:
* If X intersects the interval of the left child of T, then insert X in that child, recursively.
* If X intersects the interval of the right child of T, then insert X in that child, recursively.
The complete construction operation takes O time, n being the number of segments in.
Query
This section describes the query operation of a segment tree in a one-dimensional space. A query for a segment tree, receives a point qx, and retrieves a list of all the segments stored which contain the point qx. Formally stated; given a node v and a query point qx, the query can be done using the following algorithm:
In a segment tree that contains n intervals, those containing a given query point can be reported in O time, where k is the number of reported intervals.
The segment tree can be generalized to higher dimension spaces, in the form of multi-level segment trees. In higher dimensional versions, the segment tree stores a collection of axis-parallel rectangles, and can retrieve the rectangles that contain a given query point. The structure uses O storage, and answers queries in O. The use of fractional cascading lowers the query time bound by a logarithmic factor. The use of the interval tree on the deepest level of associated structures lowers the storage bound by a logarithmic factor.
History
The segment tree was invented by Jon Louis Bentley in 1977; in "Solutions to Klee’s rectangle problems".