Applications, Advantages and Disadvantages of Segment Tree
Last Updated :
20 Jan, 2023
First, let us understand why we need it prior to landing on the introduction so as to get why this concept was introduced. Suppose we are given an array and we need to find out the subarray
Purpose of Segment Trees:
A segment tree is a data structure that deals with a range of queries over an array. It has a divide and conquers approach. Used to solve range minimum and maximum & Sum Queries and Range Update Queries in O (log n) time complexity.
Construction of Segment Tree:
array[]: 5, 3, 2, 4, 1, 8, 6 10
we'll use divide and conquer
Now we'll see how to segment the tree Construction is done.
Number of nodes will be = n + n/2 + n/4 + ...... + 2+1
Geometric progression: common difference will be 2
let, number of terms be 'x', a=first term and r = common ratio.
(ar)^x-1 = n
a=1,r=2
(2)^x-1=n
log2(2)^x-1=log2n
x=1+log2n=number of levels
(2) - number of nodes = 1+2+4+....+n/2+n/4+n
1[(2)^1+logn - 1]/2-1 => (2n-1)
Let us take an example of returning and updating the sum of the subarray a[i.....j] of size n.
Example:
Query: output the sum from i=1 to i=5
Approach 1: Iterate from i = 1 to i = 5 and calculate the sum, update the element at i'th index, we'll update a[i] = updated element. now, the time complexity of the query is O(n) and the update is O(1).
Approach 2: Prefix Sum approach
First, we'll build the prefix sum array.
Query: Output the sum from i to j
sum[i....j] = {pref[j] - pref[i-1]} (if i!=0)
pref[j] (if i=0)
Time Complexity : O(1)
Updating value: Put a[i] = updated value.
To update the prefix array we need to change all prefix[i].
Array:
i=4,
Prefix sum array:
Let's take an example to understand this.
Example: Update the 4th index element to 13.
Original array: arr[]
Prefix sum: arr[] all elements get changed because the 4th index is updated so it will affect all elements after the 3rd index.
Time Complexity comparison table:
| Query | Update |
Approach-1 | O(n) | O(1) |
Approach-2 | O(1) | O(n) |
Segment Tree | O(log(n)) | O(log(n)) |
Requirement of log(n) time complexity: Many times, the number of queries and number of updates are of the order of 105-106, we will get TLE if we use Approach 1 or Approach 2.
Building a segment tree:
It is very simple to build a segment tree, we use the divide and conquer approach to build the segment tree.
pseudocode for implementation of Segment tree:
const int N = 1e5+2;
int a[N];
int tree[4*N];
void build(int node, int st, int en) {
if(st==en) {
tree[node] = a[st];
return;
}
int mid = (st+en)/2;
build(2*node, st, mid);
build(2*node+1, mid+1, en);
tree[node] = tree[2*node] + tree[2*node+1];
}
Applications of Segment trees:
Segment tree data structure can be used to solve various problems like:
- Range Min, Max & Sum Queries, and Range Update Queries
- segment tree with the max build, query, and update.
- segment tree with min build, query, and update.
- Computational geometry: Computational geometry is a mathematical field that includes the design, and analysis of efficient algorithms for solving geometric I/O problems. It is also used for pattern recognition and describes solid modeling algorithms.
- Geographic information systems: A Geographic Information System is a system that uses data that is attached to a unique location, and analyzes and generates geographically referenced information.
- Storing segments in an arbitrary manner.
- Used in competitive programming.
- Segment trees can be used to count the frequency of elements in a given range.
- : Segment trees can be used for image processing tasks.
Advantages and Disadvantages of segment trees:
S.No | Advantages | Disadvantages |
1 | There is no need to know tree rotation because in the test cases a divide and conquer algorithm. | Time Complexity of each and every query is O(log^2 max(n)) |
2 | Fast execution of code in general test cases. | Source code is longer than source using a balanced tree. |
3 | Allows processing interval or range queries in logarithmic time. | Segment trees require a large amount of memory to store all the nodes of the tree. |
4 | It performs well for large datasets. | The implementation of segment trees can be complex and difficult to understand. |
5 | Segment trees can be used to perform both static and dynamic operations on an array. | Slower for point update. |
Related Topic: Segment Tree
Similar Reads
Segment Tree
Segment Tree is a data structures that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tre
3 min read
Segment tree meaning in DSA
A segment tree is a data structure used to effectively query and update ranges of array members. It's typically implemented as a binary tree, with each node representing a segment or range of array elements. Segment tree Characteristics of Segment Tree:A segment tree is a binary tree with a leaf nod
2 min read
Introduction to Segment Trees - Data Structure and Algorithm Tutorials
A Segment Tree is used to stores information about array intervals in its nodes.It allows efficient range queries over array intervals.Along with queries, it allows efficient updates of array items.For example, we can perform a range summation of an array between the range L to R in O(Log n) while a
15+ min read
Persistent Segment Tree | Set 1 (Introduction)
Prerequisite : Segment Tree Persistency in Data Structure Segment Tree is itself a great data structure that comes into play in many cases. In this post we will introduce the concept of Persistency in this data structure. Persistency, simply means to retain the changes. But obviously, retaining the
15+ min read
Segment tree | Efficient implementation
Let us consider the following problem to understand Segment Trees without recursion.We have an array arr[0 . . . n-1]. We should be able to, Find the sum of elements from index l to r where 0 <= l <= r <= n-1Change the value of a specified element of the array to a new value x. We need to d
12 min read
Iterative Segment Tree (Range Maximum Query with Node Update)
Given an array arr[0 . . . n-1]. The task is to perform the following operation: Find the maximum of elements from index l to r where 0 <= l <= r <= n-1.Change value of a specified element of the array to a new value x. Given i and x, change A[i] to x, 0 <= i <= n-1. Examples: Input:
14 min read
Range Sum and Update in Array : Segment Tree using Stack
Given an array arr[] of N integers. The task is to do the following operations: Add a value X to all the element from index A to B where 0 ? A ? B ? N-1.Find the sum of the element from index L to R where 0 ? L ? R ? N-1 before and after the update given to the array above.Example: Input: arr[] = {1
15+ min read
Dynamic Segment Trees : Online Queries for Range Sum with Point Updates
Prerequisites: Segment TreeGiven a number N which represents the size of the array initialized to 0 and Q queries to process where there are two types of queries: 1 P V: Put the value V at position P.2 L R: Output the sum of values from L to R. The task is to answer these queries. Constraints: 1 ? N
15+ min read
Applications, Advantages and Disadvantages of Segment Tree
First, let us understand why we need it prior to landing on the introduction so as to get why this concept was introduced. Suppose we are given an array and we need to find out the subarray Purpose of Segment Trees: A segment tree is a data structure that deals with a range of queries over an array.
4 min read
Lazy Propagation
Lazy Propagation in Segment Tree
Segment tree is introduced in previous post with an example of range sum problem. We have used the same "Sum of given Range" problem to explain Lazy propagation  How does update work in Simple Segment Tree? In the previous post, update function was called to update only a single value in array. Ple
15+ min read
Lazy Propagation in Segment Tree | Set 2
Given an array arr[] of size N. There are two types of operations: Update(l, r, x) : Increment the a[i] (l <= i <= r) with value x.Query(l, r) : Find the maximum value in the array in a range l to r (both are included).Examples: Input: arr[] = {1, 2, 3, 4, 5} Update(0, 3, 4) Query(1, 4) Output
15+ min read
Flipping Sign Problem | Lazy Propagation Segment Tree
Given an array of size N. There can be multiple queries of the following types. update(l, r) : On update, flip( multiply a[i] by -1) the value of a[i] where l <= i <= r . In simple terms, change the sign of a[i] for the given range.query(l, r): On query, print the sum of the array in given ran
15+ min read