Skip to content

Commit 321d38a

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 8ece70c commit 321d38a

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

data_structures/binary_tree/segment_tree_node.py

+28-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Node():
1+
class Node:
22
def __init__(self, start, end):
33
# Initializes a segment tree node with start and end indices
44
self.start = start
@@ -8,17 +8,17 @@ def __init__(self, start, end):
88
self.right = None
99

1010

11-
class SegmentTree():
12-
def __init__(self, nums, mode='max'):
11+
class SegmentTree:
12+
def __init__(self, nums, mode="max"):
1313
"""
1414
Initializes the Segment Tree.
1515
:param nums: List of integers to build the tree from.
1616
:param mode: Operation mode of the tree ('max' or 'sum').
1717
"""
1818
self.siz = len(nums)
1919
self.mode = mode
20-
if mode not in {'max', 'sum'}:
21-
self.mode = 'max' # Default to max if invalid mode is given
20+
if mode not in {"max", "sum"}:
21+
self.mode = "max" # Default to max if invalid mode is given
2222

2323
# Build the tree from the input list
2424
self.root = self.build(0, self.siz - 1, nums)
@@ -39,14 +39,14 @@ def build(self, start, end, nums):
3939
n = Node(start, end)
4040
n.value = nums[start]
4141
return n
42-
42+
4343
mid = (start + end) // 2
4444
root = Node(start, end)
4545
root.left = self.build(start, mid, nums)
4646
root.right = self.build(mid + 1, end, nums)
4747

4848
# Set the value according to the mode
49-
if self.mode == 'max':
49+
if self.mode == "max":
5050
root.value = max(root.left.value, root.right.value)
5151
else:
5252
root.value = root.left.value + root.right.value
@@ -58,24 +58,24 @@ def max_in_range(self, start_index, end_index):
5858
Queries the maximum value in a given range.
5959
Only works in 'max' mode.
6060
"""
61-
if self.mode == 'sum':
62-
raise Exception('Current Segment Tree doesn\'t support finding max')
63-
61+
if self.mode == "sum":
62+
raise Exception("Current Segment Tree doesn't support finding max")
63+
6464
if start_index > end_index or start_index < 0 or end_index >= self.siz:
65-
raise Exception('Invalid index')
66-
65+
raise Exception("Invalid index")
66+
6767
return self.query(self.root, start_index, end_index, 0, self.siz - 1)
6868

6969
def sum_in_range(self, start_index, end_index):
7070
"""
7171
Queries the sum of values in a given range.
7272
Only works in 'sum' mode.
7373
"""
74-
if self.mode == 'max':
75-
raise Exception('Current Segment Tree doesn\'t support summing')
76-
74+
if self.mode == "max":
75+
raise Exception("Current Segment Tree doesn't support summing")
76+
7777
if start_index > end_index or start_index < 0 or end_index >= self.siz:
78-
raise Exception('Invalid index')
78+
raise Exception("Invalid index")
7979

8080
return self.query(self.root, start_index, end_index, 0, self.siz - 1)
8181

@@ -92,7 +92,7 @@ def query(self, node, start_index, end_index, start, end):
9292
# Complete overlap
9393
if start_index <= start and end <= end_index:
9494
return node.value
95-
95+
9696
mid = (start + end) // 2
9797

9898
if end_index <= mid:
@@ -103,16 +103,15 @@ def query(self, node, start_index, end_index, start, end):
103103
return self.query(node.right, start_index, end_index, mid + 1, end)
104104
else:
105105
# Range spans both children
106-
if self.mode == 'max':
106+
if self.mode == "max":
107107
return max(
108108
self.query(node.left, start_index, end_index, start, mid),
109-
self.query(node.right, start_index, end_index, mid + 1, end)
109+
self.query(node.right, start_index, end_index, mid + 1, end),
110110
)
111111
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-
)
112+
return self.query(
113+
node.left, start_index, end_index, start, mid
114+
) + self.query(node.right, start_index, end_index, mid + 1, end)
116115

117116
def update(self, index, new_value):
118117
"""
@@ -121,8 +120,8 @@ def update(self, index, new_value):
121120
:param new_value: New value to set.
122121
"""
123122
if index < 0 or index >= self.siz:
124-
raise Exception('Invalid index')
125-
123+
raise Exception("Invalid index")
124+
126125
self.modify(self.root, index, new_value, 0, self.siz - 1)
127126

128127
def modify(self, node, index, new_value, start, end):
@@ -137,16 +136,16 @@ def modify(self, node, index, new_value, start, end):
137136
if start == end:
138137
node.value = new_value
139138
return
140-
139+
141140
mid = (start + end) // 2
142141

143142
if index <= mid:
144143
self.modify(node.left, index, new_value, start, mid)
145144
else:
146145
self.modify(node.right, index, new_value, mid + 1, end)
147-
146+
148147
# Recompute current node's value after update
149-
if self.mode == 'max':
148+
if self.mode == "max":
150149
node.value = max(node.left.value, node.right.value)
151150
else:
152151
node.value = node.left.value + node.right.value
@@ -201,4 +200,4 @@ def modify(self, node, index, new_value, start, end):
201200
202201
st_invalid = SegmentTree(nums, mode='unknown') # Should default to 'max'
203202
print(st_invalid.mode) # Expected: 'max'
204-
"""
203+
"""

0 commit comments

Comments
 (0)