You are on page 1of 15

SEGMENT TREES

CS-201 PROJECT (GROUP 42)


What are Segment Trees and Why do we need them?

● A Segment Tree is a data structure( full Binary tree) that allows answering range queries over an array
effectively, while still being flexible enough to allow modifying the array.

● This includes finding the sum of consecutive array elements A[L-R] or finding minimum/maximum
elements in such a range or applying any associative operation over a range in O(logN) time.

● Between answering such queries, Segment tree also allows modifying the array by replacing one
element, or even change the elements of a whole sub-segment( e.g assigning all elements in the range
[L-R] a particular value, or adding/subtracting values according to some defined property) also in
O(logN) time.
Building A Segment Tree:

●The root node represents the interval[0 , N-1] and its left child will represent the interval[0 , (N-1)/2] and
its right child will represent the interval[(N-1)/2+1,N-1]. Similarly if a particular tree node represents the
interval[L-R] then its two child will represent the interval[L,R/2] and [R/2+1,R].

●For construction of the Segment tree, we start at the bottom level (the leaf vertices) and assign them
their respective values. On the basis of these values, we can compute the values of the previous level i.e
the parent nodes, using the merge function. And on the basis of those, we can compute the values of all
the levels, and repeat the procedure until we reach the root vertex.

●Also a Segment Tree build on a 1-D array of size N has at most 4N nodes. Therefore time complexity of
building a Segment Tree will be O(N) (assuming that the merge function takes constant time).Figure on
next slide represents a Segment Tree which is build on the following array :
3 2 4 5 1 3 5 3
Point Updates:
● Now our task is to modify a specific element in the array, let's say we want to change a[i] to some new value. And
we have to rebuild the Segment Tree, such that it now corresponds to the new, modified array.

● Each level of a Segment Tree forms a partition of the array. Therefore the index of the element to be modified lies
only in 1 particular interval of any level.

● Since we need to change only 1 node at each level whose interval will contain this element, therefore, at maximum
we need to traverse upto the height of the tree. we only need to change the information stored in the ancestors of
the leaf node that we are changing.

● Therefore, as the Height of tree is O(logN), point updates will also take O(logN) time. Let’s see what will our tree
look like if we change a[2] to the value 8.(Nodes marked in yellow are the nodes which will be updated).
Query Function Without Range Updates:
There will be three cases:
Case 1: The interval represented by the tree node will lie completely outside our interval for which we are
querying.Therefore, we directly return from here as the nodes in the subtree of this node will not contribute in
our range.
Case 2: The interval represented by the tree node will lie completely inside our range for which we are
querying. Therefore,we directly return from here also, as all the nodes in the subtree of this node will
contribute to the answer.
Case 3: The interval represented by the tree node will lie partially inside and partially outside the range for
which we are querying. Therefore,we need to go to both the child nodes of this node.
We visit at most 4 nodes on any level of the tree.Therefore, maximum nodes visited in whole traversal
=4*Height of Tree=4 logN.
Therefore, Query for a range will take O(logN) time.
Range Updates:
● Now for range updates,all the tree nodes will not store the actual value they should store. Only the nodes we are going
to visit will store the actual value.

● For range updates also, we are going to traverse the tree in exactly similar manner, like we were doing for query
function. For the nodes which we will visit, we will change their values to the values which they should now represent
and push the value we are adding or assigning to the child nodes of the nodes we are visiting.

● Therefore, we will be visiting at most 4 logN nodes as the traversal is similar to traversal of query function.Hence
range updates will also take O(logN) time.

● Suppose we are adding 6 in the range [2-6]. So now the diagram on next slide, represents the structure of the tree after
this update.(the values which are indicated in blue in the figure on next slide will tell the value to be added/assigned to
the elements of the range represented by the tree nodes).
Query with Range Updates:
● Query traversal when we have done some range updates will be similar to the query traversal which
we were doing earlier when there were no range updates. Just, while we will be traversing the tree
nodes, we will see if there are any extra values to be added/assigned in the range represented by the
tree node(represented by blue in the figure on next slide) and if there are any we will do so and push
the values we are adding or assigning to the child nodes of the nodes we are visiting.

● Since the traversal is similar to the earlier query function(just we have to pushdown the extra value
to both the child nodes of the nodes we are visiting). Therefore this query function will also take
O(logN) time.

● The diagram on next slide illustrates the process of query in the the range[3-5].
THANK YOU

You might also like