Skip to content

Commit ac76a04

Browse files
authored
Updated segment_tree.py
updated the wrong space complexity and fixed pre-commit
1 parent 8e16739 commit ac76a04

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

searches/segment_tree.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
- a[1] + a[2] + a[3] + a[4] + a[5] = 3+3+1+7+2 = 16
2828
- answer is 16
2929
30-
Time Complexity:- O(N + Q)
31-
-- O(N) pre-calculation time to calculate the prefix sum array
32-
-- and O(1) time per each query = O(1 * Q) = O(Q) time
33-
34-
Space Complexity:- O(N Log N + Q Log N)
30+
Time Complexity:- O(N Log N + Q Log N)
3531
-- O(N Log N) time for building the segment tree
3632
-- O(log n) time for each query
3733
-- Q queries are there so total time complexity is O(Q Log n)
3834
35+
Space Complexity:- O(N)
36+
3937
Algorithm:-
4038
We first build the segment tree. An example of what the tree would look like:-
4139
(query type is sum)
@@ -83,7 +81,6 @@
8381
Query both the children and add their results and return that
8482
"""
8583

86-
8784
class SegmentTree:
8885
def __init__(self, arr, merge_func, default):
8986
"""
@@ -98,7 +95,7 @@ def __init__(self, arr, merge_func, default):
9895
self.n = len(arr)
9996

10097
# while self.n is not a power of two
101-
while (self.n & (self.n - 1)) != 0:
98+
while (self.n & (self.n-1)) != 0:
10299
self.n += 1
103100
self.arr.append(default)
104101

@@ -110,9 +107,8 @@ def __init__(self, arr, merge_func, default):
110107
self.segment_tree[self.n + i] = arr[i]
111108

112109
for i in range(self.n - 1, 0, -1):
113-
self.segment_tree[i] = self.merge_func(
114-
self.segment_tree[2 * i], self.segment_tree[2 * i + 1]
115-
)
110+
self.segment_tree[i] = self.merge_func(self.segment_tree[2 * i],
111+
self.segment_tree[2 * i + 1])
116112

117113
def update(self, index, value):
118114
"""
@@ -122,16 +118,15 @@ def update(self, index, value):
122118

123119
while index >= 1:
124120
index //= 2 # Go to the parent of index
125-
self.segment_tree[index] = self.merge_func(
126-
self.segment_tree[2 * index], self.segment_tree[2 * index + 1]
127-
)
121+
self.segment_tree[index] = self.merge_func(self.segment_tree[2 * index],
122+
self.segment_tree[2 * index + 1])
128123

129124
def query(self, left, right, node_index=1, node_left=0, node_right=None):
130125
"""
131126
Finds the answer of self.merge_query(left, left+1, left+2, left+3, ..., right)
132127
"""
133128
if not node_right:
134-
# We cant add self.n as the default value in the function
129+
# We can't add self.n as the default value in the function
135130
# because self itself is a parameter so we do it this way
136131
node_right = self.n
137132

@@ -151,5 +146,5 @@ def query(self, left, right, node_index=1, node_left=0, node_right=None):
151146
# of the query values of both the children nodes
152147
return self.merge_func(
153148
self.query(left, right, node_index * 2, node_left, mid),
154-
self.query(left, right, node_index * 2 + 1, mid + 1, node_right),
149+
self.query(left, right, node_index * 2 + 1, mid + 1, node_right)
155150
)

0 commit comments

Comments
 (0)