At each level, we divide the array segments into two parts. If the given array had [0, . . ., N-1] elements in it then the two parts of the array will be [0, . . ., N/2-1] and [N/2, . . ., N-1].
We will then recursively go on until the lower and upper bounds of the range become equal.
The structure of the segment tree looks like a binary tree.
The segment tree is generally represented using an array where the first value stores the value for the total array range and the child of the node at the ith index are at (2*i + 1) and (2*i + 2).
Constructing the segment tree:
There are two important points to be noted while constructing the segment tree:
Choosing what value to be stored in the nodes according to the problem definition
What should the merge operation do
If the problem definition states that we need to calculate the sum over ranges, then the value at nodes should store the sum of values over the ranges.
The child node values are merged back into the parent node to hold the value for that particular range, [i.e., the range covered by all the nodes of its subtree].
In the end, leaf nodes store information about a single element. All the leaf nodes store the array based on which the segment tree is built.
Following are the steps for constructing a segment tree:
Start from the leaves of the tree
Recursively build the parents from the merge operation
The merge operation will take constant time if the operator takes constant time. SO building the whole tree takes O(N) time.
Let us understand this with the help of the following problem
Given two integers L and R return the sum of the segment [L, R]
The first step is constructing the segment tree with the addition operator and 0 as the neutral element.
If the range is one of the node's range values then simply return the answer.
Otherwise, we will need to traverse the left and right children of the nodes and recursively continue the process till we find a node that covers a range that totally covers a part or whole of the range [L, R]
While returning from each call, we need to merge the answers received from each of its child.
As the height of the segment tree is logN the query time will be O(logN) per query.
Given an index, idx, update the value of the array at index idx with value V
The element's contribution is only in the path from its leaf to its parent. Thus only logN elements will get affected due to the update.
For updating, traverse till the leaf that stores the value of index idx and update the value. Then while tracing back in the path, modify the ranges accordingly.
Below is the implementation of construction, query, and point update for a segment tree:
Output
Sum of values in range 0-4 are: 7
Value at index 1 increased by 100
sum of value in range 1-3 are: 109
Time complexity: O(N)
The building operation takes O(N) time
The query operation takes O(logN) time
Each update is performed in O(logN) time
Auxiliary Space: O(n)
Note:
A segment tree with 2^x leaf nodes will have 2^(x+1)-1 total nodes due to its perfect binary tree structure. However, when dealing with a non-power-of-two number of elements, extra leaf nodes may be present. To represent all elements, the number of leaf nodes must be rounded up to the nearest power of two, resulting in a maximum of almost 2*n leaf nodes.
For instance, if n is 2^j + 1, 2^(j+1) leaf nodes will be required, leading to an O(2*n) space complexity. As the total number of nodes is about twice the number of leaf nodes, the total space complexity of the segment tree is O(4n). The space requirement can be substantial, but it is usually manageable for most practical applications.
Updating an interval (Lazy propagation):
Lazy Propagation: A speedup technique for range updates
We can delay some updates (avoid recursive calls in update) and do such updates only when necessary when there are several updates and updates are being performed on a range.
A node in a segment tree stores or displays the results of a query for a variety of indexes.
Additionally, all of the node's descendants must also be updated if the update operation's range includes this node.
Take the node with the value 27 in the picture above as an example. This node contains the sum of values at the indexes 3 to 5. This node and all of its descendants must be updated if our update query covers the range of 2 to 5.
By storing this update information in distinct nodes referred to as lazy nodes or values, we use lazy propagation to update only the node with value 27 and delay updates to its descendants.
We make an array called lazy[] to stand in for the lazy node. The size of lazy[] is the same as the array used to represent the segment tree in the code following, which is tree[].
The goal is to set all of the lazy[elements] to 0.
There are no pending changes on the segment tree node i if lazy[i] has a value of 0.
A non-zero value for lazy[i] indicates that before doing any queries on node i in the segment tree, this sum needs to be added to the node.
Below is the implementation to demonstrate the working of Lazy Propagation.
Output
Sum of values in given range = 15
Updated sum of values in given range = 45
Time Complexity: O(N) Auxiliary Space: O(MAX)
Interval scheduling: Segment trees can be used to efficiently schedule non-overlapping intervals, such as scheduling appointments or allocating resources.
Range-based statistics: Segment trees can be used to compute range-based statistics such as variance, standard deviation, and percentiles.
Image processing: Segment trees are used in image processing algorithms to divide an image into segments based on color, texture, or other attributes.
Efficient querying: Segment trees can be used to efficiently answer queries about the minimum, maximum, sum, or other aggregate value of a range of elements in an array.
Efficient updates: Segment trees can be used to efficiently update a range of elements in an array, such as incrementing or decrementing a range of values.
Flexibility: Segment trees can be used to solve a wide variety of problems involving range queries and updates.
Complexity: Segment trees can be complex to implement and maintain, especially for large arrays or high-dimensional data.
Time complexity: The time complexity of segment tree operations like build, update, and the query is O(log N) , which is higher than some other data structures like the Fenwick tree.
Space complexity: The space complexity of a segment tree is O(4N) which is relatively high.