Skip to content

Commit 6ff786b

Browse files
authored
Refactor Node class to use dataclass
Refactor Node class to use dataclass for cleaner syntax. Update PersistentSegmentTree methods to maintain functionality with the new Node structure.
1 parent bcd9f5d commit 6ff786b

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

data_structures/persistent_segment_tree.py renamed to data_structures/binary_tree/persistent_segment_tree.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
22

3+
from dataclasses import dataclass
34

5+
6+
@dataclass
47
class Node:
5-
def __init__(self, value: int = 0) -> None:
6-
self.value: int = value
7-
self.left: Node | None = None
8-
self.right: Node | None = None
8+
value: int = value
9+
left: Node | None = None
10+
right: Node | None = None
911

1012

1113
class PersistentSegmentTree:
@@ -56,7 +58,7 @@ def update(self, version: int, index: int, value: int) -> int:
5658

5759
def _update(self, node: Node, start: int, end: int, index: int, value: int) -> Node:
5860
"""
59-
Updates the node for the specified index and value and returns the new node.
61+
Update the node for the specified index and value and return the new node.
6062
6163
>>> pst = PersistentSegmentTree([1, 2, 3, 4])
6264
>>> old_root = pst.roots[0]
@@ -131,7 +133,7 @@ def _query(self, node: Node, start: int, end: int, left: int, right: int) -> int
131133

132134
# Running the doctests
133135
if __name__ == "__main__":
134-
import doctest
136+
from doctest
135137

136138
print("Running doctests...")
137139
result = doctest.testmod()

0 commit comments

Comments
 (0)