Structure of the Tree

The segment tree works on the principle of divide and conquer

  • 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).

Introduction to Segment Trees – Data Structure and Algorithm Tutorials

Similar Reads

What is Segment Tree?

A Segment Tree is a data structure that stores information about a range of elements in its nodes. It also allows users to modify the array and perform range queries in smaller complexity. 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....

Types of Operations:

The operations that the segment tree can perform must be binary and associative. Overall the values must belong to the set of the semigroup. The neutral element must be obvious according to the type of operation and semigroup we are looking for. For example, if we want to find the sum over the range of values in an array where the elements belong to  then the neutral element, in this case, will be 0. Some of the examples of operations are:...

Structure of the Tree

The segment tree works on the principle of divide and conquer....

Constructing the segment tree:

There are two important points to be noted while constructing the segment tree:...

Range Query

Let us understand this with the help of the following problem...

Point Updates

Given an index,  idx, update the value of the array at index idx with value V...

Updating an interval (Lazy propagation):

...

Applications:

...

Advantages:

...

Disadvantages:

...

Contact Us