0
0
Data Structures Theoryknowledge~10 mins

Fenwick trees (Binary Indexed Trees) in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fenwick trees (Binary Indexed Trees)
Start with array
Build Fenwick Tree
Query prefix sum
Update element
Repeat queries/updates
End
Fenwick tree starts by building from an array, then supports fast prefix sum queries and updates by moving through tree nodes using bit operations.
Execution Sample
Data Structures Theory
arr = [1,2,3,4,5]
fenw = [0]*6

def update(i, val):
    while i < len(fenw):
        fenw[i] += val
        i += i & (-i)

def query(i):
    s = 0
    while i > 0:
        s += fenw[i]
        i -= i & (-i)
    return s

# Build fenw
for i in range(1,6):
  update(i, arr[i-1])

# Query sum(1..3)
print(query(3))
Build Fenwick tree from array and query sum of first 3 elements.
Analysis Table
StepiActionFenw array stateOutput
11update(1,1)[0,1,1,0,1,0]
22update(2,2)[0,1,3,0,3,0]
33update(3,3)[0,1,3,3,6,0]
44update(4,4)[0,1,3,3,10,0]
55update(5,5)[0,1,3,3,10,5]
63query sum(3)[0,1,3,3,10,5]6
74query sum(4)[0,1,3,3,10,5]10
85query sum(5)[0,1,3,3,10,5]15
9-update(3,2) (add 2 to index 3)[0,1,3,5,12,5]
105query sum(5)[0,1,3,5,12,5]17
11-End[0,1,3,5,12,5]
💡 Finished building Fenwick tree and performing queries and updates.
State Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 9Final
fenw[0,0,0,0,0,0][0,1,1,0,1,0][0,1,3,0,3,0][0,1,3,3,6,0][0,1,3,3,10,0][0,1,3,3,10,5][0,1,3,3,10,5][0,1,3,5,12,5][0,1,3,5,12,5]
output617
Key Insights - 3 Insights
Why does the Fenwick tree array have one extra element compared to the input array?
Fenwick tree uses 1-based indexing internally for easier bit manipulation, so fenw array size is input size + 1 (see rows 1-5 in execution_table).
How does the update function affect multiple positions in the Fenwick tree?
Update adds the value to fenw[i] and then moves to next index by adding the last set bit of i, updating all relevant nodes (see steps 1-5 and 9).
Why does the query sum(3) return 6 in step 6?
Query sums fenw[3] + fenw[2] = 3 + 3 = 6, combining partial sums stored in Fenwick tree nodes (step 6 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6. What is the prefix sum output for sum(3)?
A9
B5
C6
D3
💡 Hint
Check the Output column at step 6 in execution_table.
At which step does the Fenwick tree get updated to include an additional +2 at index 3?
AStep 5
BStep 9
CStep 3
DStep 7
💡 Hint
Look for update actions with value 2 in the Action column.
If the initial array had 6 elements instead of 5, how would the fenw array size change?
ASize would be 7
BSize would be 6
CSize would be 5
DSize would be 8
💡 Hint
Fenwick tree uses 1-based indexing, so fenw size = input size + 1.
Concept Snapshot
Fenwick trees store cumulative frequencies for fast prefix sums.
Use 1-based indexing array fenw.
Update adds value at index and moves up using i += i & (-i).
Query sums prefix by moving down using i -= i & (-i).
Both operations run in O(log n) time.
Full Transcript
Fenwick trees, also called Binary Indexed Trees, help quickly find sums of elements from the start of an array up to any position. We build a fenw array one element larger than the input to use 1-based indexing. To update, we add a value at an index and then move to the next relevant node by adding the last set bit of the index. To query a prefix sum, we add fenw values moving down by subtracting the last set bit until we reach zero. This process is efficient and runs in logarithmic time. The execution table shows building fenw from an array, querying sums, and updating values step-by-step.