Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this tutorial, we’ll discuss the Segment Tree method, which is a tree data structure. Then we’ll explore some examples.
The segment tree is a type of data structure from computational geometry. Bentley proposed this well-known technique in 1977. A segment tree is essentially a binary tree in whose nodes we store the information about the segments of a linear data structure such as an array.
Furthermore, it helps us solve questions on range queries along with updates. This method enables the execution of interval or range queries in logarithmic time, and we can utilize it when we must process numerous such queries.
It’s also possible to make this method a dynamic structure.
Segment Trees can be used to solve Range Min/Max & Sum Queries and Range Update Queries in O (log n) time. These problems can be easily solved with the Segment Tree technique. We can study this method by solving such a problem efficiently. Basically, a Segment Tree has only three operations:
In a Segment Tree, the array is stored at the leaves of the tree, while the internal nodes store information about segments represented by its children. The internal nodes are formed in a bottom-up manner by merging the information from its children nodes.
Segment Trees are useful whenever we’re frequently working with ranges of numerical data. In this context, let’s look at an example to better understand the Segment Tree by describing each step. We want to construct a Segment Tree array that allows us to find the sum of the elements in an array.
Let’s start with the first step of building the Tree.
We have an array , which has the values [
,
,
,
,
,
,
], and we want to query the sum of a range. So every node in the segment tree will be the sum of its child nodes in this scenario.
Since it’s a binary tree, we know that the child nodes in array representation for node are
and
. For the sake of simplicity, we’ll treat the entire array, including
and
, as one indexed array. As a result, each element in the Segment tree
will be
, where
is the node’s index, and the leaf node represents the elements in the array
.
Below is the tree we have for the given array :
The array is visible on the leaf of the tree:
The following illustration explains how a segment tree is updated.
To update an array element, we must also update the segment tree. In fact, given that an element in the segment tree only contributes to its parent element, changing the value may be done in complexity since we only need to traverse the height.
Therefore, to update a value, we can simply determine the diff, which is the difference between the new value of an element and the current value.
As we can see, updating the value of the th member in the array
,
, necessitates updating all of its parent nodes:
The following illustration goes over how creating to run a query on a specific range.
Now let’s look at creating a range query using the following array and the Segment Tree
from the previous section.
Let’s say we want to run a sum query on a range of to
in the array
. The Segment Tree can also use the
complexity. A sum query for the range of
to
will operate as shown:
We know that node in
has the total of
and
, and that
lies in
. Thus, we can write:
Since the traversal is always depending on the tree’s height, it’s always .
Now let’s discuss the different cases for finding the sum of a range.
The sum of the nodes in can always be split down for a given range, such as
.
As a result, while calculating the sum of a range, we can expect three scenarios:
A Segment Tree is an important data structure to have in our repertoire, not only for an aspiring computer science engineer but also for any person coding as a hobby. Moreover, the Segment Tree has a vast array of possibilities and applications.
Let’s look at some applications in different areas:
We can also specify other applications that are very well known:
In this article, we learned how to create a segment tree, update a value, and make a range query that can be used to solve a range query-based problem in an efficient time. This type of data structure is advantageous and has numerous applications in different areas.