ArticleslgStudy

computer science

Segment tree

Segment tree is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Segment tree rather than just read about it. In short: In computer science, the segment tree is a data structure used for storing information about intervals or segments. It allows querying which of the stored segments contain a given point.

Segment tree — main illustration
Segment tree — illustration

Key takeaways

  • Segment tree belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Segment tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Segment tree from memory before moving on to harder problems.

Reference excerpt

In computer science, the segment tree is a data structure used for storing information about intervals or segments. It allows querying which of the stored segments contain a given point. A similar data structure is the interval tree. A segment tree for a set I of n intervals uses O(n log n) storage and can be built in O(n log n) time. Segment trees support searching for all the intervals that contain a query point in time O(log n + k), k being the number of retrieved intervals or segments. Applications of the segment tree are in the areas of computational geometry, geographic information systems and machine learning. The segment tree can be generalized to higher dimension spaces.

Definition

Description Let I 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:

( − ∞ , p 1 ) , [ p 1 , p 1 ] , ( p 1 , p 2 ) , [ p 2 , p 2 ] , … , ( p m − 1 , p m ) , [ p m , p m ] , ( p m , + ∞ ) {\displaystyle (-\infty ,p_{1}),[p_{1},p_{1}],(p_{1},p_{2}),[p_{2},p_{2}],\dots ,(p_{m-1},p_{m}),[p_{m},p_{m}],(p_{m},+\infty )}

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 I of intervals, or segments, a segment tree T for I is structured as follows:

T is a binary tree. Its leaves correspond to the elementary intervals induced by the endpoints in I, 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(v). The internal nodes of T correspond to intervals that are the union of elementary intervals: the interval Int(N) corresponding to node N is the union of the intervals corresponding to the leaves of the tree rooted at N. That implies that Int(N) is the union of the intervals of its two children. Each node or leaf v in T stores the interval Int(v) and a set of intervals, in some data structure. This canonical subset of node v contains the intervals [x, x′] from I such that [x, x′] contains Int(v) and does not contain Int(parent(v)). That is, each node in T stores the segments that span through its interval, but do not span through the interval of its parent.

Construction A segment tree from the set of segments I, can be built as follows. First, the endpoints of the intervals in I 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(v) it represents. It remains to compute the canonical subsets for the nodes. To achieve this, the intervals in I are inserted one by one into the segment tree. An interval X = [x, x′] can be inserted in a subtree rooted at T, using the following procedure:

If Int(T) 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(n log n) time, n being the number of segments in I.

Query A query for a segment tree receives a point qx(should be one of the leaves of tree), and retrieves a list of all the segments stored which contain the point qx. Formally stated; given a node (subtree) v and a query point qx, the query can be done using the following algorithm:

Report all the intervals in I(v). If v is not a leaf: If qx is in Int(left child of v) then Perform a query in the left child of v. If qx is in Int(right child of v) then Perform a query in the right child of v. In a segment tree that contains n intervals, those containing a given query point can be reported in O(log n + k) time, where k is the number of reported intervals.

Storage requirements A segment tree T on a set I of n intervals uses O(n log n) storage.

The set I has at most 4n + 1 elementary intervals. Because T is a binary balanced tree with at most 4n + 1 leaves, its height is O(log n). Since any interval is stored at most twice at a given depth of the tree, that the total amount of storage is O(n log n).

Generalization for higher dimensions 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 (hyper-)rectangles, and can retrieve the rectangles that contain a given query point. The structure uses O(n logd n) storage, and answers queries in O(logd n) time. 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.

… excerpt ends here. Continue reading the full article.

Illustrations

Segment tree: Graphic example of the structure of the segment tree. This instance is built for the segments shown at the bottom.
Graphic example of the structure of the segment tree. This instance is built for the segments shown at the bottom.

Worked examples

Example 1 — a first encounter with Segment tree

Start with the simplest possible case. Write down what Segment tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Segment tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Segment tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Segment tree

In research
Segment tree appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Segment tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Segment tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary trees, Computer graphics data structures, Trees (data structures), so understanding it makes those chapters shorter.
In everyday life
Look for Segment tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Segment tree” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Segment tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Segment tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Segment tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Segment tree in simple terms?

In computer science, the segment tree is a data structure used for storing information about intervals or segments. It allows querying which of the stored segments contain a given point.

Why does Segment tree matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Segment tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Segment tree.

Tags

  • Binary trees
  • Computer graphics data structures
  • Trees (data structures)

Keep exploring