"""
SegmentTrees.py
This module provides a comprehensive implementation of Segment Trees, a powerful data structure
used for efficient range queries and updates on arrays. The implementation covers basic to advanced
concepts, ensuring clarity, maintainability, and scalability. It adheres strictly to PEP-8 standards
and includes comprehensive error handling.
Author: OpenAI ChatGPT
Date: 2023-10
"""
from typing import List, Callable, Optional
class SegmentTree:
"""
A versatile Segment Tree implementation supporting various range queries and updates.
"""
def __init__(self, data: List[int], function: Callable[[int, int], int], default: int) -> None:
"""
Initializes the Segment Tree.
Args:
data (List[int]): The input array for which the segment tree is built.
function (Callable[[int, int], int]): The function to be used for range queries
(e.g., sum, min, max).
default (int): The default value for non-overlapping segments.
Raises:
ValueError: If the input data list is empty.
TypeError: If the function provided is not callable.
"""
if not data:
raise ValueError("Input data list cannot be empty.")
if not callable(function):
raise TypeError("Function must be callable.")
self.n = len(data)
self.function = function
self.default = default
self.size = 1
while self.size < self.n:
self.size <<= 1
self.tree = [self.default] * (2 * self.size)
self._build(data)
def _build(self, data: List[int]) -> None:
"""
Builds the segment tree from the input data.
Args:
data (List[int]): The input array.
"""
# Initialize leaves
for i in range(self.n):
self.tree[self.size + i] = data[i]
# Build the tree by calculating parents
for i in range(self.size - 1, 0, -1):
left_child = self.tree[2 * i]
right_child = self.tree[2 * i + 1]
self.tree[i] = self.function(left_child, right_child)
def update(self, index: int, value: int) -> None:
"""
Updates the value at a specific index and recalculates the affected segment.
Args:
index (int): The index to update (0-based).
value (int): The new value to be set.
Raises:
IndexError: If the index is out of bounds.
"""
if index < 0 or index >= self.n:
raise IndexError("Index out of bounds.")
# Update the leaf node
pos = self.size + index
self.tree[pos] = value
# Update ancestors
while pos > 1:
pos //= 2
left_child = self.tree[2 * pos]
right_child = self.tree[2 * pos + 1]
new_val = self.function(left_child, right_child)
if self.tree[pos] == new_val:
break # No further updates required
self.tree[pos] = new_val
def query(self, left: int, right: int) -> int:
"""
Performs a range query on the interval [left, right).
Args:
left (int): The starting index of the range (inclusive).
right (int): The ending index of the range (exclusive).
Returns:
int: The result of applying the function over the specified range.
Raises:
IndexError: If the query range is out of bounds.
ValueError: If left is greater than right.
"""
if left < 0 or right > self.n:
raise IndexError("Query range out of bounds.")
if left > right:
raise ValueError("Left index cannot be greater than right index.")
result = self.default
left += self.size
right += self.size
while left < right:
if left % 2:
result = self.function(result, self.tree[left])
left += 1
if right % 2:
right -= 1
result = self.function(result, self.tree[right])
left //= 2
right //= 2
return result
def __str__(self) -> str:
"""
Returns a string representation of the segment tree.
Returns:
str: The string representation.
"""
levels = []
level_size = 1
while level_size < self.size:
levels.append(self.tree[level_size:2 * level_size])
level_size <<= 1
levels.append(self.tree[self.size:2 * self.size])
return '\n'.join(['Level {}: {}'.format(i, level) for i, level in enumerate(levels)])
def example_usage() -> None:
"""
Demonstrates the usage of the SegmentTree class with various operations.
"""
# Example data
data = [2, 1, 5, 3, 4, 6]
# Initialize a segment tree for range sum queries
sum_tree = SegmentTree(data, function=lambda x, y: x + y, default=0)
print("Initial Segment Tree for Range Sum:")
print(sum_tree)
print()
# Perform a range sum query from index 1 to 5
sum_query = sum_tree.query(1, 5)
print(f"Sum of elements from index 1 to 5: {sum_query}")
print()
# Update the value at index 3 to 10
sum_tree.update(3, 10)
print("Segment Tree after updating index 3 to 10:")
print(sum_tree)
print()
# Perform the same range sum query again
sum_query = sum_tree.query(1, 5)
print(f"Sum of elements from index 1 to 5 after update: {sum_query}")
print()
# Initialize a segment tree for range minimum queries
min_tree = SegmentTree(data, function=min, default=float('inf'))
print("Initial Segment Tree for Range Minimum:")
print(min_tree)
print()
# Perform a range minimum query from index 0 to 4
min_query = min_tree.query(0, 4)
print(f"Minimum of elements from index 0 to 4: {min_query}")
print()
# Update the value at index 2 to 0
min_tree.update(2, 0)
print("Segment Tree after updating index 2 to 0:")
print(min_tree)
print()
# Perform the same range minimum query again
min_query = min_tree.query(0, 4)
print(f"Minimum of elements from index 0 to 4 after update: {min_query}")
print()
if __name__ == "__main__":
example_usage()
Explanation of the Implementation
1. Class Initialization
-
Parameters:
data: The input array to build the segment tree.
function: The binary function (e.g., sum, min, max) to perform over the range.
default: The default value used for non-overlapping segments during queries.
-
Process:
- Validate input data and function.
- Calculate the size of the underlying tree, ensuring it's a power of two for simplicity.
- Initialize the tree with default values and build it using the
_build method.
2. Building the Tree (_build method)
- Assign the input data to the leaves of the tree.
- Iterate from the bottom up, applying the function to child nodes to compute parent nodes.
3. Updating the Tree (update method)
- Update a specific index with a new value.
- Recalculate the affected nodes up the tree to maintain the correct aggregated values.
4. Querying the Tree (query method)
- Perform a range query over the interval
[left, right).
- Utilize the properties of the segment tree to efficiently compute the result by traversing upwards and combining relevant segments.
5. String Representation (__str__ method)
- Provides a readable representation of the segment tree, showing each level for better visualization and debugging.
6. Example Usage (example_usage function)
- Demonstrates how to initialize and use the
SegmentTree class for different types of queries (e.g., sum and minimum).
- Showcases updating elements and performing queries post-update to validate the correctness of the implementation.
Advanced Features and Considerations
-
Lazy Propagation: For scenarios involving frequent range updates, implementing lazy propagation can optimize performance by postponing updates to relevant nodes until necessary.
-
Dynamic Segment Trees: The current implementation assumes a fixed size. For dynamic arrays where elements can be inserted or deleted, modifications are required to handle resizing and rebalancing.
-
Generic Types: While the current implementation uses int, it can be generalized to support other data types by adjusting type hints and ensuring the function is compatible.
-
Error Handling: Comprehensive error handling ensures that invalid inputs or operations are gracefully managed, preventing unexpected crashes and aiding in debugging.
-
Performance Optimization: The use of iterative methods and bit manipulation ensures that the implementation is both time and space-efficient, suitable for handling large datasets.
Conclusion
This SegmentTree class offers a robust and flexible foundation for a wide range of applications requiring efficient range queries and updates. By adhering to Python's best practices and providing detailed documentation within the code, it ensures both high performance and maintainability, making it a valuable tool for both educational purposes and professional projects.