Segment-Tree Module in Python
Last Updated :
25 Apr, 2025
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
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]
Similar Reads
Persistent Segment Tree in Python
Persistent data structures are a powerful tool in computer science, enabling us to maintain and access multiple versions of a data structure over time. One such structure is the Persistent Segment Tree. Segment trees are versatile data structures that allow efficient querying and updating of array i
9 min read
Lazy Propagation in Segment Tree
Segment tree is introduced in previous post with an example of range sum problem. We have used the same "Sum of given Range" problem to explain Lazy propagation  How does update work in Simple Segment Tree? In the previous post, update function was called to update only a single value in array. Ple
15+ min read
Tree Sort in Python
Tree sort is a sorting algorithm that builds a Binary Search Tree (BST) from the elements of the array to be sorted and then performs an in-order traversal of the BST to get the elements in sorted order. In this article, we will learn about the basics of Tree Sort along with its implementation in Py
3 min read
SymPy | Prufer.to_tree() in Python
Prufer.to_tree() : to_tree() is a sympy Python library function that returns the tree representation of the Prufer sequence. Syntax : sympy.combinatorics.Prufer.prufer.to_tree() Return : representation of the Prufer sequence Code #1 : to_tree() Example Python3 1=1 # Python code explaining # SymPy.Pr
1 min read
Compression of 2D Segment Tree in Python
Compressing a 2D segment tree in Python involves reducing memory usage by storing only necessary information. This can be achieved by employing various techniques such as segment tree compression and lazy propagation. Let's create a tutorial on compressing a 2D segment tree in Python. A 2D segment t
5 min read
Implement Simple 2D Segment Tree in Python
A 2D Segment Tree is a data structure that allows efficient querying and updating of two-dimensional arrays, such as matrices. It's an extension of the 1D segment tree, allowing range queries and updates in 2D. Here's a step-by-step implementation of a simple 2D Segment Tree in Python. We'll focus o
5 min read
Segment tree meaning in DSA
A segment tree is a data structure used to effectively query and update ranges of array members. It's typically implemented as a binary tree, with each node representing a segment or range of array elements. Segment tree Characteristics of Segment Tree:A segment tree is a binary tree with a leaf nod
2 min read
Draw tree using Turtle module in Python
Prerequisite: Turtle module, Drawing Triangle, Drawing Rectangle There are many modules in python which depicts graphical illustrations, one of them is turtle, it is an in-built module in Python, which lets the user control a pen(turtle) to draw on screen(drawing board). It is mostly used to illustr
2 min read
Recursion on Trees in Python
In Python, recursion is implemented by defining a function that makes a call to itself within its definition. This process continues until a base case is reached, which is a condition where the function returns a value without making any further recursive calls. Without a base case, the recursion wo
8 min read
Segment tree | Efficient implementation
Let us consider the following problem to understand Segment Trees without recursion.We have an array arr[0 . . . n-1]. We should be able to, Find the sum of elements from index l to r where 0 <= l <= r <= n-1Change the value of a specified element of the array to a new value x. We need to d
12 min read