Introduction To Segment Trees

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Introduction to Segment Trees

Segment Trees are Binary Tree which is used to store intervals or segments. That
is each node in a segment tree basically stores the segment of an array. Segment
Trees are generally used in problems where we need to solve queries on a range of
elements in arrays.

Let us consider the following problem to understand Segment Trees.

Problem: We have an array arr[0 . . . n-1]. We should be able to perform the below
operations on the array:

1. Find the sum of elements from index l to r where 0 <= l <= r <= n-1.
2. Change value of a specified element of the array to a new value x. We need
to do arr[i] = x where 0 <= i <= n-1.

General Solution: A simple solution is to run a loop from index l to r and calculate
the sum of elements in the given range. To update a value, simply do arr[i] = x. The
first operation takes O(N) time for every query, where N is the number of elements in
the range [l,r] and the second operation takes O(1) time.

Can we optimize the time complexity of the first operation in the above
solution?

Yes, we can optimize the first operation to be solved in O(1) time complexity by
storing presum. We can keep an auxiliary array say sum[] in which the i-th element
will store the sum of first i elements of the original array. So, whenever we need to
find the sum of a range of elements, we can simply calculate it by (sum[r]-sum[l-1]).
But in this solution the complexity to perform the second operation of updating an
element increases from O(1) to O(N).

What if the number of query and updates are equal? Can we perform both the
operations in O(log n) time once given the array?

We can use a segment tree to perform both of the operations in O(log N) time
complexity.

Representation of Segment trees:

1. Leaf Nodes are the elements of the input array.


2. Each internal node represents some merging of the leaf nodes. The merging
may be different for different problems. For this problem, merging is the sum
of leaves under a node.
An array representation of the tree is used to represent Segment Trees. For each
node at index i, the left child is at index 2*i + 1, right child at 2*i + 2 and the parent is
at (i - 1)/2.

Array representation of Segment Trees: Like Heap, segment tree is also


represented as an array. The difference here is, it is not a complete binary tree. It is
rather a full binary tree (every node has 0 or 2 children) and all levels are filled
except possibly the last level. Unlike Heap, the last level may have gaps between
nodes. Below are the values in the segment tree array for the above diagram.

Array representation of segment tree for input array {1, 3, 5, 7, 9, 11} is,

st[] = {36, 9, 27, 4, 5, 16, 11, 1, 3, DUMMY, DUMMY, 7, 9, DUMMY, DUMMY}

The dummy values are never accessed and have no use. This is some wastage of
space due to simple array representation. We may optimize this wastage using some
clever implementations, but the code for sum and update becomes more complex.

Construction of Segment Tree from the given array: We start with a segment
arr[0 . . . n-1] and every time we divide the current segment into two halves(if it has
not yet become a segment of length 1), and then call the same procedure on both
halves, and for each such segment, we store the sum in the corresponding node.
All levels of the constructed segment tree will be completely filled except the last
level. Also, the tree will be a Full Binary Tree because we always divide segments
into two halves at every level. Since the constructed tree is always a full binary tree
with n leaves, there will be n-1 internal nodes. So the total number of nodes will be
2*n – 1. Note that this does not include dummy nodes.

What is the total size of the array representing segment tree? If n is a power of
2, then there are no dummy nodes. So the size of the segment tree is 2n-1 (n leaf
nodes and n-1) internal nodes. If n is not a power of 2, the size of the tree will be 2*x
– 1 where x is the smallest of 2 greater than n. For example, when n = 10, then size
of array representing segment tree is 2*16-1 = 31.

Query for Sum of given range


Following is the algorithm to get the sum of elements.

int getSum(node, l, r)
{
if the range of the node is within l and r
return value in the node
else if the range of the node is completely outside l and r
return 0
else
return getSum(node's left child, l, r) +
getSum(node's right child, l, r)
}

Updating a value
Like tree construction and query operations, the update can also be done
recursively. We are given an index which needs to be updated. Let diff be the value
to be added. We start from the root of the segment tree and add diff to all nodes
which have given index in their range. If a node doesn’t have given index in its
range, we don’t make any changes to that node.

Implementation:
C++

// C++ program to show segment tree operations like construction, query


// and update
#include <bits/stdc++.h>
using namespace std;

// A utility function to get the middle index from corner indexes.


int getMid(int s, int e) { return s + (e -s)/2; }

/* A recursive function to get the sum of values in given range


of the array. The following are parameters for this function.
st --> Pointer to segment tree
si --> Index of current node in the segment tree. Initially
0 is passed as root is always at index 0
ss & se --> Starting and ending indexes of the segment represented
by current node, i.e., st[si]
qs & qe --> Starting and ending indexes of query range */
int getSumUtil(int *st, int ss, int se, int qs, int qe, int si)
{
// If segment of this node is a part of given range, then return
// the sum of the segment
if (qs <= ss && qe >= se)
return st[si];

// If segment of this node is outside the given range


if (se < qs || ss > qe)
return 0;

// If a part of this segment overlaps with the given range


int mid = getMid(ss, se);
return getSumUtil(st, ss, mid, qs, qe, 2*si+1) +
getSumUtil(st, mid+1, se, qs, qe, 2*si+2);
}

/* A recursive function to update the nodes which have the given


index in their range. The following are parameters
st, si, ss and se are same as getSumUtil()
i --> index of the element to be updated. This index is
in the input array.
diff --> Value to be added to all nodes which have i in range */
void updateValueUtil(int *st, int ss, int se, int i, int diff, int si)
{
// Base Case: If the input index lies outside the range of
// this segment
if (i < ss || i > se)
return;

// If the input index is in range of this node, then update


// the value of the node and its children
st[si] = st[si] + diff;
if (se != ss)
{
int mid = getMid(ss, se);
updateValueUtil(st, ss, mid, i, diff, 2*si + 1);
updateValueUtil(st, mid+1, se, i, diff, 2*si + 2);
}
}
// The function to update a value in input array and segment tree.
// It uses updateValueUtil() to update the value in segment tree
void updateValue(int arr[], int *st, int n, int i, int new_val)
{
// Check for erroneous input index
if (i < 0 || i > n-1)
{
cout<<"Invalid Input";
return;
}

// Get the difference between new value and old value


int diff = new_val - arr[i];

// Update the value in array


arr[i] = new_val;

// Update the values of nodes in segment tree


updateValueUtil(st, 0, n-1, i, diff, 0);
}

// Return sum of elements in range from index qs (quey start)


// to qe (query end). It mainly uses getSumUtil()
int getSum(int *st, int n, int qs, int qe)
{
// Check for erroneous input values
if (qs < 0 || qe > n-1 || qs > qe)
{
cout<<"Invalid Input";
return -1;
}

return getSumUtil(st, 0, n-1, qs, qe, 0);


}

// A recursive function that constructs Segment Tree for array[ss..se].


// si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int *st, int si)
{
// If there is one element in array, store it in current node of
// segment tree and return
if (ss == se)
{
st[si] = arr[ss];
return arr[ss];
}

// If there are more than one elements, then recur for left and
// right subtrees and store the sum of values in this node
int mid = getMid(ss, se);
st[si] = constructSTUtil(arr, ss, mid, st, si*2+1) +
constructSTUtil(arr, mid+1, se, st, si*2+2);
return st[si];
}

/* Function to construct segment tree from given array. This function


allocates memory for segment tree and calls constructSTUtil() to
fill the allocated memory */
int *constructST(int arr[], int n)
{
// Allocate memory for the segment tree

//Height of segment tree


int x = (int)(ceil(log2(n)));

//Maximum size of segment tree


int max_size = 2*(int)pow(2, x) - 1;

// Allocate memory
int *st = new int[max_size];

// Fill the allocated memory st


constructSTUtil(arr, 0, n-1, st, 0);

// Return the constructed segment tree


return st;
}

// Driver program to test above functions


int main()
{
int arr[] = {1, 3, 5, 7, 9, 11};
int n = sizeof(arr)/sizeof(arr[0]);

// Build segment tree from given array


int *st = constructST(arr, n);

// Print sum of values in array from index 1 to 3


cout<<"Sum of values in given range = "<<getSum(st, n, 1, 3)<<endl;

// Update: set arr[1] = 10 and update corresponding


// segment tree nodes
updateValue(arr, st, n, 1, 10);

// Find sum after the value is updated


cout<<"Updated sum of values in given range = "
<<getSum(st, n, 1, 3)<<endl;
return 0;
}
Run
Java

// Java Program to show segment tree operations like construction,


// query and update
class SegmentTree
{
int st[]; // The array that stores segment tree nodes

/* Constructor to construct segment tree from given array. This


constructor allocates memory for segment tree and calls
constructSTUtil() to fill the allocated memory */
SegmentTree(int arr[], int n)
{
// Allocate memory for segment tree
//Height of segment tree
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));

//Maximum size of segment tree


int max_size = 2 * (int) Math.pow(2, x) - 1;

st = new int[max_size]; // Memory allocation

constructSTUtil(arr, 0, n - 1, 0);
}

// A utility function to get the middle index from corner indexes.


int getMid(int s, int e) {
return s + (e - s) / 2;
}

/* A recursive function to get the sum of values in given range


of the array. The following are parameters for this function.

st --> Pointer to segment tree


si --> Index of current node in the segment tree. Initially
0 is passed as root is always at index 0
ss & se --> Starting and ending indexes of the segment represented
by current node, i.e., st[si]
qs & qe --> Starting and ending indexes of query range */
int getSumUtil(int ss, int se, int qs, int qe, int si)
{
// If segment of this node is a part of given range, then return
// the sum of the segment
if (qs <= ss && qe >= se)
return st[si];

// If segment of this node is outside the given range


if (se < qs || ss > qe)
return 0;

// If a part of this segment overlaps with the given range


int mid = getMid(ss, se);
return getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
}

/* A recursive function to update the nodes which have the given


index in their range. The following are parameters
st, si, ss and se are same as getSumUtil()
i --> index of the element to be updated. This index is in
input array.
diff --> Value to be added to all nodes which have i in range */
void updateValueUtil(int ss, int se, int i, int diff, int si)
{
// Base Case: If the input index lies outside the range of
// this segment
if (i < ss || i > se)
return;

// If the input index is in range of this node, then update the


// value of the node and its children
st[si] = st[si] + diff;
if (se != ss) {
int mid = getMid(ss, se);
updateValueUtil(ss, mid, i, diff, 2 * si + 1);
updateValueUtil(mid + 1, se, i, diff, 2 * si + 2);
}
}

// The function to update a value in input array and segment tree.


// It uses updateValueUtil() to update the value in segment tree
void updateValue(int arr[], int n, int i, int new_val)
{
// Check for erroneous input index
if (i < 0 || i > n - 1) {
System.out.println("Invalid Input");
return;
}

// Get the difference between new value and old value


int diff = new_val - arr[i];

// Update the value in array


arr[i] = new_val;

// Update the values of nodes in segment tree


updateValueUtil(0, n - 1, i, diff, 0);
}

// Return sum of elements in range from index qs (quey start) to


// qe (query end). It mainly uses getSumUtil()
int getSum(int n, int qs, int qe)
{
// Check for erroneous input values
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}
return getSumUtil(0, n - 1, qs, qe, 0);
}

// A recursive function that constructs Segment Tree for array[ss..se].


// si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int si)
{
// If there is one element in array, store it in current node of
// segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}

// If there are more than one elements, then recur for left and
// right subtrees and store the sum of values in this node
int mid = getMid(ss, se);
st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) +
constructSTUtil(arr, mid + 1, se, si * 2 + 2);
return st[si];
}
// Driver program to test above functions
public static void main(String args[])
{
int arr[] = {1, 3, 5, 7, 9, 11};
int n = arr.length;
SegmentTree tree = new SegmentTree(arr, n);

// Build segment tree from given array

// Print sum of values in array from index 1 to 3


System.out.println("Sum of values in given range = " +
tree.getSum(n, 1, 3));

// Update: set arr[1] = 10 and update corresponding segment


// tree nodes
tree.updateValue(arr, n, 1, 10);

// Find sum after the value is updated


System.out.println("Updated sum of values in given range = " +
tree.getSum(n, 1, 3));
}
}
Run

Output:
Sum of values in given range = 15
Updated sum of values in given range = 22

Time Complexity: The time Complexity for tree construction is O(n). There are total
2n-1 nodes, and the value of every node is calculated only once in tree construction.

Time complexity to query is O(Logn). To query a sum, we process at most four


nodes at every level and number of levels is O(Logn).

The time complexity of the update is also O(Logn). To update a leaf value, we
process one node at every level and number of levels is O(Logn).
Segment Tree | Range Minimum Query
The Range Minimum Query is another popular problem which can be solved using
Segment Trees. The problems state that, given an array and a list of queries
containing ranges, the task is to find the minimum element in the range for every
query.

Note: This problem does not require us to perform any update operation on the
array.

Representation of Segment trees:


1. Leaf Nodes are the elements of the input array.
2. Each internal node represents a minimum of all leaves under it.

An array representation of the tree is used to represent Segment Trees. For each
node at index i, the left child is at index 2*i + 1, the right child at 2*i + 2 and the
parent is at (i-1)/2.

We start with a segment arr[0 . . . n-1] and every time we divide the current segment
into two halves(if it has not yet become a segment of length 1), and then call the
same procedure on both halves, and for each such segment, we store the minimum
value in a segment tree node.

All levels of the constructed segment tree will be completely filled except the last
level. Also, the tree will be a Full Binary Tree because we always divide segments
into two halves at every level. Since the constructed tree is always a full binary tree
with n leaves, there will be n-1 internal nodes. So the total number of nodes will be
2*n – 1.

Height of the segment tree will be log2N. Since the tree is represented using array
and relation between parent and child indexes must be maintained, the size of
memory allocated for segment tree will be 2*2log N - 1.
2

Query for the minimum value in a given range:


Following is the algorithm to get the minimum element in Range.
// qs --> query start index, qe --> query end index
int RMQ(node, qs, qe)
{
if the range of node is within qs and qe
return value in the node
else if the range of node is completely outside qs and qe
return INFINITE
else
return min( RMQ(node's left child, qs, qe),
RMQ(node's right child, qs, qe) )
}

Implementation:

C++

// C++ program for range minimum


// query using segment tree
#include <bits/stdc++.h>
using namespace std;

// A utility function to get minimum of two numbers


int minVal(int x, int y) { return (x < y)? x: y; }

// A utility function to get the


// middle index from corner indexes.
int getMid(int s, int e) { return s + (e -s)/2; }

/* A recursive function to get the


minimum value in a given range
of array indexes. The following
are parameters for this function.

st --> Pointer to segment tree


index --> Index of current node in the
segment tree. Initially 0 is
passed as root is always at index 0
ss & se --> Starting and ending indexes
of the segment represented
by current node, i.e., st[index]
qs & qe --> Starting and ending indexes of query range */
int RMQUtil(int *st, int ss, int se, int qs, int qe, int index)
{
// If segment of this node is a part
// of given range, then return
// the min of the segment
if (qs <= ss && qe >= se)
return st[index];
// If segment of this node
// is outside the given range
if (se < qs || ss > qe)
return INT_MAX;

// If a part of this segment


// overlaps with the given range
int mid = getMid(ss, se);
return minVal(RMQUtil(st, ss, mid, qs, qe, 2*index+1),
RMQUtil(st, mid+1, se, qs, qe, 2*index+2));
}

// Return minimum of elements in range


// from index qs (quey start) to
// qe (query end). It mainly uses RMQUtil()
int RMQ(int *st, int n, int qs, int qe)
{
// Check for erroneous input values
if (qs < 0 || qe > n-1 || qs > qe)
{
cout<<"Invalid Input";
return -1;
}

return RMQUtil(st, 0, n-1, qs, qe, 0);


}

// A recursive function that constructs


// Segment Tree for array[ss..se].
// si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se,
int *st, int si)
{
// If there is one element in array,
// store it in current node of
// segment tree and return
if (ss == se)
{
st[si] = arr[ss];
return arr[ss];
}

// If there are more than one elements,


// then recur for left and right subtrees
// and store the minimum of two values in this node
int mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, st, si*2+1),
constructSTUtil(arr, mid+1, se, st, si*2+2));
return st[si];
}

/* Function to construct segment tree


from given array. This function allocates
memory for segment tree and calls constructSTUtil() to
fill the allocated memory */
int *constructST(int arr[], int n)
{
// Allocate memory for segment tree

//Height of segment tree


int x = (int)(ceil(log2(n)));

// Maximum size of segment tree


int max_size = 2*(int)pow(2, x) - 1;

int *st = new int[max_size];

// Fill the allocated memory st


constructSTUtil(arr, 0, n-1, st, 0);

// Return the constructed segment tree


return st;
}

// Driver Code
int main()
{
int arr[] = {1, 3, 2, 7, 9, 11};
int n = sizeof(arr)/sizeof(arr[0]);

// Build segment tree from given array


int *st = constructST(arr, n);

int qs = 1; // Starting index of query range


int qe = 5; // Ending index of query range

// Print minimum value in arr[qs..qe]


cout<<"Minimum of values in range ["<<qs<<", "<<qe<<"] "<<
"is = "<<RMQ(st, n, qs, qe)<<endl;

return 0;
}
Run
Java

// Program for range minimum query using segment tree


class SegmentTreeRMQ
{
int st[]; //array to store segment tree

// A utility function to get minimum of two numbers


int minVal(int x, int y) {
return (x < y) ? x : y;
}

// A utility function to get the middle index from corner


// indexes.
int getMid(int s, int e) {
return s + (e - s) / 2;
}

/* A recursive function to get the minimum value in a given


range of array indexes. The following are parameters for
this function.

st --> Pointer to segment tree


index --> Index of current node in the segment tree. Initially
0 is passed as root is always at index 0
ss & se --> Starting and ending indexes of the segment
represented by current node, i.e., st[index]
qs & qe --> Starting and ending indexes of query range */
int RMQUtil(int ss, int se, int qs, int qe, int index)
{
// If segment of this node is a part of given range, then
// return the min of the segment
if (qs <= ss && qe >= se)
return st[index];

// If segment of this node is outside the given range


if (se < qs || ss > qe)
return Integer.MAX_VALUE;

// If a part of this segment overlaps with the given range


int mid = getMid(ss, se);
return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1),
RMQUtil(mid + 1, se, qs, qe, 2 * index + 2));
}
// Return minimum of elements in range from index qs (quey
// start) to qe (query end). It mainly uses RMQUtil()
int RMQ(int n, int qs, int qe)
{
// Check for erroneous input values
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}

return RMQUtil(0, n - 1, qs, qe, 0);


}

// A recursive function that constructs Segment Tree for


// array[ss..se]. si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int si)
{
// If there is one element in array, store it in current
// node of segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}

// If there are more than one elements, then recur for left and
// right subtrees and store the minimum of two values in this node
int mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, si * 2 + 2));
return st[si];
}

/* Function to construct segment tree from given array. This function


allocates memory for segment tree and calls constructSTUtil() to
fill the allocated memory */
void constructST(int arr[], int n)
{
// Allocate memory for segment tree

//Height of segment tree


int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));

// Maximum size of segment tree


int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // allocate memory
// Fill the allocated memory st
constructSTUtil(arr, 0, n - 1, 0);
}

// Driver Code
public static void main(String args[])
{
int arr[] = {1, 3, 2, 7, 9, 11};
int n = arr.length;
SegmentTreeRMQ tree = new SegmentTreeRMQ();

// Build segment tree from given array


tree.constructST(arr, n);

int qs = 1; // Starting index of query range


int qe = 5; // Ending index of query range

// Print minimum value in arr[qs..qe]


System.out.println("Minimum of values in range [" + qs + ", "
+ qe + "] is = " + tree.RMQ(n, qs, qe));
}
}
Run

Output:
Minimum of values in range [1, 5] is = 2

Time Complexity: The time complexity for tree construction is O(n). There are total
2n-1 nodes, and the value of every node is calculated only once in tree construction.

Time complexity to query is O(Logn). To query a range minimum, we process at


most two nodes at every level and number of levels is O(Logn).

You might also like