how to implement segment tree in python?
- User
how to implement segment tree in python?
- Assistant
how to implement segment tree in python?
A segment tree is a data structure that allows for efficient querying and updating of ranges in an array. It is particularly useful for tasks such as range sum queries, range minimum/maximum queries, and more.
Here’s a basic implementation of a Segment Tree in Python. This implementation will support both the construction of the segment tree and various operations, including range sum queries and point updates.
Here’s a simple implementation of a Segment Tree for range sum queries:
class SegmentTree:
def __init__(self, data):
self.n = len(data)
self.tree = [0] * (4 * self.n) # Allocate memory for segment tree
self.build(data, 0, 0, self.n - 1)
def build(self, data, node, start, end):
if start == end: # Leaf node will have a single element
self.tree[node] = data[start]
else:
mid = (start + end) // 2
left_child = 2 * node + 1
right_child = 2 * node + 2
self.build(data, left_child, start, mid)
self.build(data, right_child, mid + 1, end)
self.tree[node] = self.tree[left_child] + self.tree[right_child] # Change this for min/max
def update(self, idx, value, node, start, end):
if start == end:
self.tree[node] = value # Update leaf node
else:
mid = (start + end) // 2
left_child = 2 * node + 1
right_child = 2 * node + 2
if start <= idx <= mid:
self.update(idx, value, left_child, start, mid)
else:
self.update(idx, value, right_child, mid + 1, end)
self.tree[node] = self.tree[left_child] + self.tree[right_child] # Change this for min/max
def query(self, L, R, node, start, end):
if R < start or end < L: # range represented by a node is completely outside the given range
return 0 # Change this return value for min/max
if L <= start <= end: # range represented by a node is completely inside the given range
return self.tree[node]
# range represented by a node is partially inside and partially outside the given range
mid = (start + end) // 2
left_child = 2 * node + 1
right_child = 2 * node + 2
left_sum = self.query(L, R, left_child, start, mid)
right_sum = self.query(L, R, right_child, mid + 1, end)
return left_sum + right_sum # Change this for min/max
def update_value(self, idx, value):
self.update(idx, value, 0, 0, self.n - 1)
def range_query(self, L, R):
return self.query(L, R, 0, 0, self.n - 1)
# Example Usage
if __name__ == "__main__":
data = [1, 3, 5, 7, 9, 11]
seg_tree = SegmentTree(data)
print("Sum of values in given range (1, 3):", seg_tree.range_query(1, 3))
# Update: Set value at index 1 to 10
seg_tree.update_value(1, 10)
print("Updated sum of values in given range (1, 3):", seg_tree.range_query(1, 3))
__init__): The class takes an array and initializes the segment tree.build method, it recursively divides the array to construct the segment tree.update method adjusts the segment tree when a value changes.query method retrieves the sum of elements in a specified range.You can modify the operations performed in this implementation to handle different types of queries, like finding the minimum or maximum in a range, by changing the aggregation function used in the build, update, and query methods.
Not yet included in a published release.