A Segment Tree is a data structure in C that helps us quickly perform operations (like finding the sum, minimum, or maximum) on a range of elements in an array. It’s like a tree that stores information about parts of the array in each node.
In this article, we will learn what are segement trees, how they work and how to implement them in C language.
Segment Trees in C
A Segment Tree is a data structure that stores information about a range of elements in its nodes. It is mostly used to handle range queries with updates in an efficient manner. For example, we can perform a range summation of an array between the range L to R while also modifying the array from range L to R all in log (N) time complexity
.png)
The tree is built recursively by dividing the array into segments until each segment represents a single element. This structure enables fast query and update operations with a time complexity of O (log n), making it a powerful tool in algorithm design and optimization.
Basic Operations on Segement Trees in C
The basic processes provided by segment trees can be outlined as the construction, query, and update.
S.No
| Operation
| Description
| Time Complexity
| Space Complexity
|
---|
1 | Building Tree
| Creating the structure of the segment tree and initializing it. | O(n)
| O(n)
|
---|
2 | Updating Tree
| Changing the tree by updating the value in the array at a point or over an interval. | O(log n)
| O(n)
|
---|
3 | Querying Tree
| Running a range query on the array. | O(log n)
| O(n)
|
---|
Implementation of Basic Operations
Algorithm of Build Segement Tree
Build_Segment_Tree(arr, tree, start, end, treeNode):
If start == end:
tree[treeNode] = arr[start]
Return
mid = (start + end) / 2
Build_Segment_Tree(arr, tree, start, mid, 2*treeNode)
Build_Segment_Tree(arr, tree, mid+1, end, 2*treeNode+1)
tree[treeNode] = tree[2*treeNode] + tree[2*treeNode+1]
Algorithm of Update
Update_Segment_Tree(arr, tree, start, end, treeNode, idx, value):
If start == end:
arr[idx] = value
tree[treeNode] = value
Return
mid = (start + end) / 2
If idx > mid:
Update_Segment_Tree(arr, tree, mid+1, end, 2*treeNode+1, idx, value)
Else:
Update_Segment_Tree(arr, tree, start, mid, 2*treeNode, idx, value)
tree[treeNode] = tree[2*treeNode] + tree[2*treeNode+1]
Algorithm of Query
Query_Segment_Tree(tree, start, end, treeNode, left, right):
If start > right or end < left: // No overlap
Return 0
If start >= left and end <= right: // Complete overlap
Return tree[treeNode]
mid = (start + end) / 2 // Partial overlap
ans1 = Query_Segment_Tree(tree, start, mid, 2*treeNode, left, right)
ans2 = Query_Segment_Tree(tree, mid+1, end, 2*treeNode+1, left, right)
Return ans1 + ans2
In the above:
arr
 is the input array.tree
 is the segment tree.start
 and end
 are the start and end indices of the segment of the input array that the current node of the segment tree represents.treeNode
 is the index of the current node in the segment tree.idx
 is the index of the element to be updated in the input array.value
 is the new value to be updated.left
 and right
 are the range of the query.
C Program to Implement Segment Tree
C
// C program to demonstrate Segment Tree implementation
#include <stdio.h>
#define MAX_SIZE 1000
int segmentTree[MAX_SIZE];
// Function to build the segment tree
void buildSegmentTree(int arr[], int node, int start,
int end)
{
if (start == end) {
segmentTree[node] = arr[start];
return;
}
int mid = (start + end) / 2;
buildSegmentTree(arr, 2 * node, start, mid);
buildSegmentTree(arr, 2 * node + 1, mid + 1, end);
segmentTree[node]
= segmentTree[2 * node] + segmentTree[2 * node + 1];
}
// Function to query the segment tree
int query(int node, int start, int end, int l, int r)
{
if (r < start || end < l)
return 0;
if (l <= start && end <= r)
return segmentTree[node];
int mid = (start + end) / 2;
return query(2 * node, start, mid, l, r)
+ query(2 * node + 1, mid + 1, end, l, r);
}
// Function to update the segment tree
void update(int node, int start, int end, int idx, int val)
{
if (start == end) {
segmentTree[node] = val;
return;
}
int mid = (start + end) / 2;
if (idx <= mid)
update(2 * node, start, mid, idx, val);
else
update(2 * node + 1, mid + 1, end, idx, val);
segmentTree[node]
= segmentTree[2 * node] + segmentTree[2 * node + 1];
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5, 7, 9, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
buildSegmentTree(arr, 1, 0, n - 1);
printf("Sum of elements in range [1, 3] is %d\n",
query(1, 0, n - 1, 1, 3));
update(1, 0, n - 1, 2, 10);
printf("Sum of elements in range [1, 3] after update "
"is %d\n",
query(1, 0, n - 1, 1, 3));
return 0;
}
OutputSum of elements in range [1, 3] is 15
Sum of elements in range [1, 3] after update is 20
Applications of Segment Trees
- Segment Trees are used to answer range queries like finding the minimum, maximum, sum, greatest common divisor, least common multiple, etc., in an array in logarithmic time.
- Segment Trees can handle updates and modifications in the data structure.
- Segment Trees can be used with a technique called Lazy Propagation to perform range updates in logarithmic time.
- Segment Trees are used to solve the Range Minimum Query problem, which aims to find the minimum element from a range in an array.
- Segment Trees can also solve the Range Maximum Query problem, which aims to find the maximum element from a range in an array.
- Segment Trees can be used to find the count of distinct elements in a range.
- Segment Trees can be used to find the K-th number in a sequence.
Conclusion
In summary, a Segment Tree is a versatile data structure 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.
Similar Reads
Segment Tree in C++
A Segment Tree is a data structure that is used for various range query problems, particularly in competitive programming. It allows for efficient query operations on arrays, such as finding the minimum, maximum, or sum of elements in a given range. In this article, we will learn how to implement a
7 min read
Segment Tree in Java
A Segment Tree is a binary tree used for storing intervals or segments. It is allowed to query sum, minimum, maximum or other associative operations over the range of elements in the array efficiently. Each node in the segment tree represents the interval of an array. Leaves of the segment tree corr
6 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. Characteristics of Segment Tree:A segment tree is a binary tree with a leaf node for each el
2 min read
Introduction to Segment Trees
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
15+ min read
Line Segment
A line segment is a finite section of a straight line with two endpoints and a fixed length. In contrast, a line extends infinitely in both directions without specific endpoints. This article explores the definition, properties, formulas, and terminology of line segments, along with methods for meas
12 min read
Segmentation Fault in C++
Segmentation faults C++ is an error that occurs when a program attempts to access a memory location it does not have permission to access. Generally, this error occurs when memory access is violated and is a type of general protection fault. Segfaults are the abbreviation for segmentation faults. Ta
7 min read
Persistent Segment Tree in Python
Persistent data structures are a powerful tool in computer science, enabling us to maintain and access multiple versions of a data structure over time. One such structure is the Persistent Segment Tree. Segment trees are versatile data structures that allow efficient querying and updating of array i
9 min read
Break Statement in C
The break in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time. Let's take a look at an example: [GFGTABS] C #include
5 min read
Generalizing Segment Trees with Rust
Segment Tree A segment tree is a data structure that can be used to efficiently store and query information about ranges in an array. It is a balanced binary tree, where each node represents a range of the array and stores some aggregated information about that range. Segment trees can be used to su
10 min read
Segment of a Circle
Segment of a Circle is one of the important parts of the circle other than the sector. As we know, the circle is a 2-D shape in which points are equidistant from the point and the line connecting the two points lying on the circumference of the circle is called the chord of the circle. The area form
7 min read