Segment-Tree Module in Python
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
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.
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.
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]
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]