Segment-Tree Module in Python - GeeksforGeeks

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 .
Installing Library:
pip install 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 elementsarr = [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 rangea = 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 rangea = t.query(2, 9, "min")print("The minimum value of this range is : ", a)  # here we are finding the value# of sum of a rangea = t.query(2, 7, "sum")print("The sum of this range is : ", a)  # here we are updating the value# of a particular indext.update(2, 25) # it will replace the value of# index '2' with 25print("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 elementsarr = [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 rangea = t.query(0, 9, "max")print("The maximum value of this range is : ", a) # here we are finding value of# minimum number in a rangea = t.query(0, 9, "min")print("The minimum value of this range is : ", a) # here we are finding value# of sum of a rangea = t.query(0, 9, "sum")print("The sum of this range is : ", a) # here we are updating the value# of a particular indext.update(5, 0)print("The updated array is : ", arr)  # here we are finding value of# sum of a rangea = t.query(1, 5, "sum")print("The sum of this range is : ", a) # here we are updating the value# of a particular indext.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]
surindertarika1234
simmytarika5
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Split string into list of characters