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
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, n: int): # Create Fenwick tree with size n
self.n = n
self.arr = [0] * (n + 1)
self.tree = [0] * (n + 1)

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

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


if __name__ == "__main__":
import doctest

doctest.testmod()