Data Structures19 sections · 729 units
Open in Course

Fenwick Tree Structure

What each position stores

A Fenwick Tree uses 1-based indexing. Position ii stores the sum of elements from ilowbit(i)+1i - \text{lowbit}(i) + 1 to ii.

Index (binary):  1(1)  2(10)  3(11)  4(100)  5(101)  6(110)  7(111)  8(1000)
Stores sum of:   [1]   [1,2]   [3]   [1-4]    [5]    [5,6]    [7]    [1-8]

Position 11 stores 11 element (lowbit = 1).

Position 22 stores 22 elements (lowbit = 2).

Position 44 stores 44 elements (lowbit = 4).

Position 88 stores 88 elements (lowbit = 8).

Each position covers a different "responsibility range." Together, they allow computing any prefix sum in O(logn)O(\log n) additions.