From 82270b7c636b42533099f7ac4069590c752a71ee Mon Sep 17 00:00:00 2001 From: Putul Singh <127419636+putul03@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:19:23 +0530 Subject: [PATCH] Update persistent_segment_tree.py --- data_structures/persistent_segment_tree.py | 73 +++++++++++++--------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/data_structures/persistent_segment_tree.py b/data_structures/persistent_segment_tree.py index 24a44d90a..bc6560348 100644 --- a/data_structures/persistent_segment_tree.py +++ b/data_structures/persistent_segment_tree.py @@ -7,18 +7,23 @@ class Node: class PersistentSegmentTree: def __init__(self, arr: list[int]) -> None: - """ - Initialize the Persistent Segment Tree with the given array. - - >>> pst = PersistentSegmentTree([1, 2, 3]) - >>> pst.query(0, 0, 2) - 6 - """ self.n = len(arr) self.roots: list[Node] = [] self.roots.append(self._build(arr, 0, self.n - 1)) def _build(self, arr: list[int], start: int, end: int) -> Node: + """ + Builds a segment tree from the provided array. + + >>> pst = PersistentSegmentTree([1, 2, 3, 4]) + >>> root = pst._build([1, 2, 3, 4], 0, 3) + >>> root.value # Sum of the whole array + 10 + >>> root.left.value # Sum of the left half + 3 + >>> root.right.value # Sum of the right half + 7 + """ if start == end: return Node(arr[start]) mid = (start + end) // 2 @@ -29,19 +34,26 @@ class PersistentSegmentTree: return node def update(self, version: int, index: int, value: int) -> int: - """ - Update the value at the given index and return the new version. - - >>> pst = PersistentSegmentTree([1, 2, 3]) - >>> version_1 = pst.update(0, 1, 5) - >>> pst.query(version_1, 0, 2) - 9 - """ new_root = self._update(self.roots[version], 0, self.n - 1, index, value) self.roots.append(new_root) return len(self.roots) - 1 def _update(self, node: Node, start: int, end: int, index: int, value: int) -> Node: + """ + Updates the node for the specified index and value and returns the new node. + + >>> pst = PersistentSegmentTree([1, 2, 3, 4]) + >>> old_root = pst.roots[0] + >>> new_root = pst._update(old_root, 0, 3, 1, 5) # Update index 1 to 5 + >>> new_root.value # New sum after update + 13 + >>> old_root.value # Old root remains unchanged + 10 + >>> new_root.left.value # Updated left child + 6 + >>> new_root.right.value # Right child remains the same + 7 + """ if start == end: return Node(value) @@ -60,34 +72,33 @@ class PersistentSegmentTree: return new_node def query(self, version: int, left: int, right: int) -> int: - """ - Query the sum in the given range for the specified version. - - >>> pst = PersistentSegmentTree([1, 2, 3]) - >>> pst.query(0, 0, 2) - 6 - >>> version_1 = pst.update(0, 1, 5) - >>> pst.query(version_1, 0, 1) - 6 - >>> pst.query(version_1, 0, 2) - 9 - """ return self._query(self.roots[version], 0, self.n - 1, left, right) def _query(self, node: Node, start: int, end: int, left: int, right: int) -> int: + """ + Queries the sum of values in the range [left, right] for the given node. + + >>> pst = PersistentSegmentTree([1, 2, 3, 4]) + >>> root = pst.roots[0] + >>> pst._query(root, 0, 3, 1, 2) # Sum of elements at index 1 and 2 + 5 + >>> pst._query(root, 0, 3, 0, 3) # Sum of all elements + 10 + >>> pst._query(root, 0, 3, 2, 3) # Sum of elements at index 2 and 3 + 7 + """ if left > end or right < start: return 0 if left <= start and right >= end: return node.value mid = (start + end) // 2 - return self._query(node.left, start, mid, left, right) + self._query( - node.right, mid + 1, end, left, right - ) + return (self._query(node.left, start, mid, left, right) + + self._query(node.right, mid + 1, end, left, right)) +# Running the doctests if __name__ == "__main__": import doctest - print("Running doctests...") result = doctest.testmod() print(f"Ran {result.attempted} tests, {result.failed} failed.")