Skip to content

Add Max Fenwick Tree #6298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 12, 2022
50 changes: 50 additions & 0 deletions data_structures/binary_tree/maximum_fenwick_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class MaxFenwickTree:
"""
Maximum Fenwick Tree
---------
>>> ft = MaxFenwickTree(5)
>>> ft.query(0, 5)
0
>>> ft.update(2, 20)
>>> ft.query(0, 5)
20
>>> ft.update(5, 10)
>>> ft.query(2, 5)
10
>>> ft.update(2, 0)
>>> ft.query(0, 5)
10
"""

def __init__(self, size: int) -> None: # Create Fenwick tree with specified size
self.size = size
self.arr = [0] * (size + 1)
self.tree = [0] * (size + 1)

def update(
self, index: int, value: int
) -> None: # Set index (1-Based) to value in O(lg^2 N)
self.arr[index] = value
while index < self.size:
self.tree[index] = max(value, self.query(index, index - index & -index))
index += index & (-index)

def query(
self, left: int, right: int
) -> int: # Query maximum value from range (left, right] (1-Based) in O(lg N)
res = 0
while left < right:
x = right - right & -right
if left < x:
res = max(res, self.tree[right])
right = x
else:
res = max(res, self.arr[right])
right -= 1
return res


if __name__ == "__main__":
import doctest

doctest.testmod()