Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Segment-Tree Module in Python

  • Last Updated : 21 Aug, 2021

Prerequisite: Segment Tree Implementation 
A Segment Tree is a data structure that allows programmers to solve range queries over the given array effectively and to modifying the array values. Basically, Segment Tree is a very flexible and efficient data structure and a large number of problems can be solved with the help of segment trees.

Python Segment tree Module is also used to solve range query problems. It performs various operations in given range like sum , max , min, and update value in a range. This modules helps to avoid the implementation of segmentation tree as we can directly use segment tree function for performing all operations. 
It generally reduces the stress of implementing the Segment tree .

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Installing Library:



pip install segment-tree

Functions of Segment Tree:

  • Query: It is the major function of segment tree which perform operations like finding maximum number in a range, finding minimum number in a range, and finding the sum of given range. It takes 3 arguments as input which are start_index(i.e. from where the range will start), End_index(i.e. upto which index range end) and operation to be performed.
    Syntax: 
obj.query(Start_index, End_index, operation_name)
  • Update: The second major function of segment tree is update which will update the value of a particular index within the range.It will replace the existing value present at that index with the new value.
    Syntax:
obj.update(index, value)

Example 1:

Python3




from segment_tree import SegmentTree
 
# an array with some elements
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
 
# here we are fitting our array
# into segment tree where t is
# taken as object of segment tree
# t will be used for performing
# operations on that segmentTree
 
t = SegmentTree(arr)
 
# here we are finding value
# of maximum number in a range
a = t.query(2, 9, "max")
print("The maximum value of this range is : ", a)
 
 
# here we are finding the value
# of minimum number in a range
a = t.query(2, 9, "min")
print("The minimum value of this range is : ", a)
 
 
# here we are finding the value
# of sum of a range
a = t.query(2, 7, "sum")
print("The sum of this range is : ", a)
 
 
# here we are updating the value
# of a particular index
t.update(2, 25)
 
# it will replace the value of
# index '2' with 25
print("The updated array is : ", arr)

Output: 

The maximum value of this range is :  10
The minimum value of this range is :  3
The sum of this range is :  33
The updated array is :  [1, 2, 25, 4, 5, 6, 7, 8, 9, 10, 11]

Example 2:

Python3




from segment_tree import SegmentTree
 
 
# an array with some elements
arr = [14, 28, 55, 105, 78, 4, 24, 99, 48, 200]
 
# here we are fitting our array
# into segment tree where t is
# taken as object of segment tree
# t will be used for performing
# operations on that segmentTree
 
t = SegmentTree(arr)
 
# here we are finding value of
# maximum number in a range
a = t.query(0, 9, "max")
print("The maximum value of this range is : ", a)
 
# here we are finding value of
# minimum number in a range
a = t.query(0, 9, "min")
print("The minimum value of this range is : ", a)
 
# here we are finding value
# of sum of a range
a = t.query(0, 9, "sum")
print("The sum of this range is : ", a)
 
# here we are updating the value
# of a particular index
t.update(5, 0)
print("The updated array is : ", arr)
 
 
# here we are finding value of
# sum of a range
a = t.query(1, 5, "sum")
print("The sum of this range is : ", a)
 
# here we are updating the value
# of a particular index
t.update(4, 10)
print("The updated array is : ", arr)

Output:

The maximum value of this range is :  200
The minimum value of this range is :  4
The sum of this range is :  655
The updated array is :  [14, 28, 55, 105, 78, 0, 24, 99, 48, 200]
The sum of this range is :  266
The updated array is :  [14, 28, 55, 105, 10, 0, 24, 99, 48, 200]



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!