Skip to content

Commit 5b892bb

Browse files
committed
add new file data_structures/binary_tree/segment_tree_node.py add ret type
1 parent 571214a commit 5b892bb

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

data_structures/binary_tree/segment_tree_node.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Node():
2-
def __init__(self, start, end):
2+
def __init__(self, start: int, end: int) -> None:
33
# Initializes a segment tree node with start and end indices
44
self.start = start
55
self.end = end
@@ -8,7 +8,7 @@ def __init__(self, start, end):
88
self.right = None
99

1010
class SegmentTree():
11-
def __init__(self, nums, mode='max'):
11+
def __init__(self, nums: list[int], mode: str='max') -> None:
1212
"""
1313
Initializes the Segment Tree.
1414
:param nums: List of integers to build the tree from.
@@ -22,7 +22,7 @@ def __init__(self, nums, mode='max'):
2222
# Build the tree from the input list
2323
self.root = self.build(0, self.size - 1, nums)
2424

25-
def build(self, start, end, nums):
25+
def build(self, start: int, end: int, nums: list[int]) -> Node:
2626
"""
2727
Recursively builds the segment tree.
2828
:param start: Start index of the segment.
@@ -52,7 +52,7 @@ def build(self, start, end, nums):
5252

5353
return root
5454

55-
def max_in_range(self, start_index, end_index):
55+
def max_in_range(self, start_index: int, end_index: int) -> int:
5656
"""
5757
Queries the maximum value in a given range.
5858
Only works in 'max' mode.
@@ -65,7 +65,7 @@ def max_in_range(self, start_index, end_index):
6565

6666
return self.query(self.root, start_index, end_index, 0, self.size - 1)
6767

68-
def sum_in_range(self, start_index, end_index):
68+
def sum_in_range(self, start_index: int, end_index: int) -> int:
6969
"""
7070
Queries the sum of values in a given range.
7171
Only works in 'sum' mode.
@@ -78,7 +78,7 @@ def sum_in_range(self, start_index, end_index):
7878

7979
return self.query(self.root, start_index, end_index, 0, self.size - 1)
8080

81-
def query(self, node, start_index, end_index, start, end):
81+
def query(self, node: Node, start_index: int, end_index: int, start: int, end: int) -> int:
8282
"""
8383
Recursively queries a value (max or sum) in a given range.
8484
:param node: Current node in the tree.
@@ -107,7 +107,7 @@ def query(self, node, start_index, end_index, start, end):
107107
else:
108108
return (self.query(node.left, start_index, end_index, start, mid) + self.query(node.right, start_index, end_index, mid + 1, end))
109109

110-
def update(self, index, new_value):
110+
def update(self, index: int, new_value: int) -> int:
111111
"""
112112
Updates a value at a specific index in the segment tree.
113113
:param index: Index to update.
@@ -118,7 +118,7 @@ def update(self, index, new_value):
118118

119119
self.modify(self.root, index, new_value, 0, self.size - 1)
120120

121-
def modify(self, node, index, new_value, start, end):
121+
def modify(self, node: Node, index: int, new_value: int, start: int, end: int) -> int:
122122
"""
123123
Recursively updates the tree to reflect a change at a specific index.
124124
:param node: Current node being processed.

0 commit comments

Comments
 (0)