ADS 0256 EXP 7
ADS 0256 EXP 7
THEORY:
o The leaf nodes represent elements of the array.
o Internal nodes of the tree are generated by merging their child nodes. Since we are using
an array to represent a segment tree, a node sitting at index j has the left child at 2 * j + 1
and right child at 2 * j + 2, and the parent of the node at the index j is ⌊(j - 1) / 2⌋.
Thus, for the array a[] = {2, 4, 7, 10, 12, 13}, the representation of segment tree as an array will
be:
segArr[] = {48, 13, 35, 6, 7, 22, 13, 2, 4, k, k, 10, 12, k, k}; where k is a dummy value. The
dummy values are of no use. In fact, it is the wastage of some space because of the array
representation. The segment tree will be:
Academic Year 2024-25 SAP ID: 60003220256
Segment Tree is a basically a binary tree used for storing the intervals or segments. Each node in
the Segment Tree represents an interval Consider an array A of size N and a corresponding
Segment Tree T
The root of T will represent the whole array A0N-1
Each leaf in the Segment Tree T will represent a single element A[i] such that 0≤1< N.
The internal nodes in the Segment Tree represents the union of elementary intervals
A[I:j] where 0 ≤1< N.
The root of the Segment Tree represents the whole array A[0:N-1]. Then it is broken down into
two half intervals or segments and the two children of the root in turn represent the A[0: (N-1)/2]
and A[(N-1)/2+1: (N-1)]. So in each step, the segment is divided into half and the two children
represent those two halves. So the height of the segment tree will be log2N. There are N leaves
representing the N elements of the array. The number of internal nodes is N-1 so a total number
of nodes are 2*N-1.
Once the Segment Tree is built its structure cannot be changed. We can update the values of
nodes but we cannot change its structure.
Building Tree: in this step, we create the structure of the segment tree variable and initialize it
Updating Tree: during this step, we change the tree by updating the value in the array at a point
or over an interval
Querying Tree: we may use this operation to run a range query on the array
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 child 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.
Academic Year 2024-25 SAP ID: 60003220256
The root node is represented by A[0,n-1] which stores the sum of all the elements of the
given array.
Then the segment A[0,n-1] is divided into two halves, namely A[0,n/2] and A[n/2+1,n-
1], and the sum of each half is computed and stored.
Each of these two halves is further split into half, and the sum is computed and stored.
This process continues until all the segments length becomes 1.
So, at the first level, there is one node that is the root. In the second level, there are two
nodes, third level four nodes, until the number of nodes in a level becomes n.
All the leaf nodes represent the individual elements of the array. As you can see in the
above segment tree, the highlighted sums are nothing but the element at that index.
Applications
Computational geometry.
Geographic information systems.
Machine learning.
Academic Year 2024-25 SAP ID: 60003220256
In its early days, the Segment Tree was used to efficiently list all pairs of intersecting
rectangles from a list of rectangles in the plane.
We can use this method to report the list of all rectilinear line segments in the plane
which intersect a query line segment.
We use this technique to report the perimeter of a set of rectangles in the plane.
More recently, the segment tree has become popular for use in pattern recognition and
image processing.
Finding range sum/product, range max/min, prefix sum/product, etc.
Code:
class SegmentTree {
class Node {
int min;
int max;
int sum;
public Node(int min, int max, int sum) {
this.min = min;
this.max = max;
this.sum = sum;
}
}
Node[] segmentTree;
int n;
private void build(int[] arr, int node, int start, int end) {
if (start == end) {
private Node query(int node, int start, int end, int L, int R) {
if (R < start || L > end) {
return new Node(Integer.MAX_VALUE, Integer.MIN_VALUE, 0);
}
if (L <= start && end <= R) {
return segmentTree[node];
}
int mid = (start + end) / 2;
Node leftResult = query(2 * node + 1, start, mid, L, R);
Node rightResult = query(2 * node + 2, mid + 1, end, L, R);
private void update(int node, int start, int end, int index, int value) {
if (start == end) {
System.out.println("Min: " + result.min + ", Max: " + result.max + ", Sum: " + result.sum);
CONCLUSION:
Used for efficiently solving problems involving range query operations.
A divide and conquer algorithm.
Time Complexity for tree construction is O(n).
Time complexity for querying and updation are O(log n).
Applicable where pre-computation fails or is costly.
Alternatives to segment tree are Binary Indexed Tree and SQRT tree.
We learned what a Segment Tree is, and why do we need one. We also implemented it in C++ and
discussed about the applications.
Website References:
[1] https://www.scaler.com/topics/data-structures/segment-trees-in-data-structure/
[2] https://www.baeldung.com/cs/segment-tree