Skip to content

Commit 4c6df26

Browse files
committed
add new file data_structures/binary_tree/segment_tree_node.py fixed
1 parent 8ece70c commit 4c6df26

File tree

1 file changed

+12
-71
lines changed

1 file changed

+12
-71
lines changed

data_structures/binary_tree/segment_tree_node.py

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ def __init__(self, start, end):
77
self.left = None
88
self.right = None
99

10-
1110
class SegmentTree():
1211
def __init__(self, nums, mode='max'):
1312
"""
1413
Initializes the Segment Tree.
1514
:param nums: List of integers to build the tree from.
1615
:param mode: Operation mode of the tree ('max' or 'sum').
1716
"""
18-
self.siz = len(nums)
17+
self.size = len(nums)
1918
self.mode = mode
2019
if mode not in {'max', 'sum'}:
2120
self.mode = 'max' # Default to max if invalid mode is given
2221

2322
# Build the tree from the input list
24-
self.root = self.build(0, self.siz - 1, nums)
23+
self.root = self.build(0, self.size - 1, nums)
2524

2625
def build(self, start, end, nums):
2726
"""
@@ -61,10 +60,10 @@ def max_in_range(self, start_index, end_index):
6160
if self.mode == 'sum':
6261
raise Exception('Current Segment Tree doesn\'t support finding max')
6362

64-
if start_index > end_index or start_index < 0 or end_index >= self.siz:
63+
if start_index > end_index or start_index < 0 or end_index >= self.size:
6564
raise Exception('Invalid index')
6665

67-
return self.query(self.root, start_index, end_index, 0, self.siz - 1)
66+
return self.query(self.root, start_index, end_index, 0, self.size - 1)
6867

6968
def sum_in_range(self, start_index, end_index):
7069
"""
@@ -74,10 +73,10 @@ def sum_in_range(self, start_index, end_index):
7473
if self.mode == 'max':
7574
raise Exception('Current Segment Tree doesn\'t support summing')
7675

77-
if start_index > end_index or start_index < 0 or end_index >= self.siz:
76+
if start_index > end_index or start_index < 0 or end_index >= self.size:
7877
raise Exception('Invalid index')
7978

80-
return self.query(self.root, start_index, end_index, 0, self.siz - 1)
79+
return self.query(self.root, start_index, end_index, 0, self.size - 1)
8180

8281
def query(self, node, start_index, end_index, start, end):
8382
"""
@@ -104,26 +103,20 @@ def query(self, node, start_index, end_index, start, end):
104103
else:
105104
# Range spans both children
106105
if self.mode == 'max':
107-
return max(
108-
self.query(node.left, start_index, end_index, start, mid),
109-
self.query(node.right, start_index, end_index, mid + 1, end)
110-
)
106+
return max(self.query(node.left, start_index, end_index, start, mid), self.query(node.right, start_index, end_index, mid + 1, end))
111107
else:
112-
return (
113-
self.query(node.left, start_index, end_index, start, mid) +
114-
self.query(node.right, start_index, end_index, mid + 1, end)
115-
)
108+
return (self.query(node.left, start_index, end_index, start, mid) + self.query(node.right, start_index, end_index, mid + 1, end))
116109

117110
def update(self, index, new_value):
118111
"""
119112
Updates a value at a specific index in the segment tree.
120113
:param index: Index to update.
121114
:param new_value: New value to set.
122115
"""
123-
if index < 0 or index >= self.siz:
116+
if index < 0 or index >= self.size:
124117
raise Exception('Invalid index')
125118

126-
self.modify(self.root, index, new_value, 0, self.siz - 1)
119+
self.modify(self.root, index, new_value, 0, self.size - 1)
127120

128121
def modify(self, node, index, new_value, start, end):
129122
"""
@@ -137,7 +130,7 @@ def modify(self, node, index, new_value, start, end):
137130
if start == end:
138131
node.value = new_value
139132
return
140-
133+
141134
mid = (start + end) // 2
142135

143136
if index <= mid:
@@ -149,56 +142,4 @@ def modify(self, node, index, new_value, start, end):
149142
if self.mode == 'max':
150143
node.value = max(node.left.value, node.right.value)
151144
else:
152-
node.value = node.left.value + node.right.value
153-
154-
155-
"""
156-
nums = [1, 3, 5, 7, 9, 11]
157-
158-
st_max = SegmentTree(nums, mode='max')
159-
print(st_max.max_in_range(1, 3)) # Expected: 7 (max of [3,5,7])
160-
print(st_max.max_in_range(0, 5)) # Expected: 11
161-
st_max.update(3, 10) # nums[3] = 10
162-
print(st_max.max_in_range(1, 4)) # Expected: 10
163-
164-
try:
165-
st_max.sum_in_range(0, 2) # Should raise exception
166-
except Exception as e:
167-
print(e) # Expected: Current Segment Tree doesn't support summing
168-
169-
try:
170-
st_max.max_in_range(3, 2) # Should raise exception
171-
except Exception as e:
172-
print(e) # Expected: Invalid index
173-
174-
try:
175-
st_max.max_in_range(1, 200) # Should raise exception
176-
except Exception as e:
177-
print(e) # Expected: Invalid index
178-
179-
180-
st_sum = SegmentTree(nums, mode='sum')
181-
print(st_sum.sum_in_range(1, 3)) # Expected: 15 (3+5+7)
182-
print(st_sum.sum_in_range(0, 5)) # Expected: 36 (sum of all elements)
183-
print(st_sum.sum_in_range(1, 3)) # Expected: 15 (3+5+7)
184-
print(st_sum.sum_in_range(0, 5)) # Expected: 36 (sum of all elements)
185-
186-
try:
187-
st_sum.max_in_range(0, 2) # Should raise exception
188-
except Exception as e:
189-
print(e) # Expected: Current Segment Tree doesn't support finding max
190-
191-
try:
192-
st_sum.sum_in_range(3, 2) # Should raise exception
193-
except Exception as e:
194-
print(e) # Expected: Invalid index
195-
196-
try:
197-
st_sum.sum_in_range(1, 200) # Should raise exception
198-
except Exception as e:
199-
print(e) # Expected: Invalid index
200-
201-
202-
st_invalid = SegmentTree(nums, mode='unknown') # Should default to 'max'
203-
print(st_invalid.mode) # Expected: 'max'
204-
"""
145+
node.value = node.left.value + node.right.value

0 commit comments

Comments
 (0)