Skip to content

Commit 9f0f3d3

Browse files
authored
Merge pull request #4127 from AKSHITHA-CHILUKA/patch-13
Create fenwickTree.md
2 parents 0a07114 + d43da13 commit 9f0f3d3

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

dsa/Advance/fenwickTree.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Fenwick Tree (Binary Indexed Tree)
2+
3+
A Fenwick Tree, also known as a Binary Indexed Tree (BIT), is a data structure that provides efficient methods for calculation and manipulation of the prefix sums of a table of values.
4+
5+
## Key Features
6+
7+
- **Space Complexity:** O(n)
8+
- **Time Complexity for Updates:** O(log n)
9+
- **Time Complexity for Queries:** O(log n)
10+
11+
## Algorithm Description
12+
13+
A Fenwick Tree is used to efficiently compute prefix sums and update elements. It is particularly useful in situations where there are frequent updates and prefix sum queries.
14+
15+
### Operations
16+
17+
1. **Initialization:** Create a tree of size n initialized to 0.
18+
2. **Update:** Add a value to an element and propagate the change through the tree.
19+
3. **Prefix Sum Query:** Calculate the sum of elements from the start of the array to a given index.
20+
21+
## Python Implementation
22+
23+
Here is a Python implementation of a Fenwick Tree:
24+
25+
```python
26+
class FenwickTree:
27+
def __init__(self, size):
28+
self.size = size
29+
self.tree = [0] * (size + 1)
30+
31+
def update(self, index, value):
32+
"""
33+
Adds `value` to the element at `index` in the Fenwick Tree.
34+
"""
35+
index += 1 # Fenwick Tree is 1-indexed
36+
while index <= self.size:
37+
self.tree[index] += value
38+
index += index & -index
39+
40+
def prefix_sum(self, index):
41+
"""
42+
Returns the sum of elements from the start to `index` in the Fenwick Tree.
43+
"""
44+
index += 1 # Fenwick Tree is 1-indexed
45+
result = 0
46+
while index > 0:
47+
result += self.tree[index]
48+
index -= index & -index
49+
return result
50+
51+
def range_sum(self, left, right):
52+
"""
53+
Returns the sum of elements from `left` to `right` in the Fenwick Tree.
54+
"""
55+
return self.prefix_sum(right) - self.prefix_sum(left - 1)
56+
```
57+
# Example usage
58+
if __name__ == "__main__":
59+
arr = [1, 7, 3, 0, 7, 8, 3, 2, 6, 2]
60+
fenwick_tree = FenwickTree(len(arr))
61+
62+
# Build the Fenwick Tree
63+
for i, val in enumerate(arr):
64+
fenwick_tree.update(i, val)
65+
66+
print("Prefix sum up to index 5:", fenwick_tree.prefix_sum(5))
67+
print("Range sum from index 2 to 6:", fenwick_tree.range_sum(2, 6))
68+
fenwick_tree.update(3, 5) # Update index 3 with +5
69+
print("Prefix sum up to index 5 after update:", fenwick_tree.prefix_sum(5))
70+
print("Range sum from index 2 to 6 after update:", fenwick_tree.range_sum(2, 6))
71+
72+
73+
# Explanation
74+
The FenwickTree class initializes the tree with a given size and provides methods for updating elements and querying prefix sums.
75+
The update method adds a value to an element at a given index and updates the tree accordingly.
76+
The prefix_sum method calculates the sum of elements from the start to a given index.
77+
The range_sum method calculates the sum of elements within a specified range by using the prefix sums.
78+
79+
## Conclusion
80+
Fenwick Trees are powerful and efficient data structures for handling prefix sums and updates in logarithmic time. They are particularly useful in scenarios involving frequent updates and queries, such as in competitive programming and real-time data analysis.
81+
82+

0 commit comments

Comments
 (0)