Data Structures19 sections · 729 units
Open in Course

The Core Idea

Numbers as sum of powers of 2

Every positive integer ii you can write as a sum of distinct powers of 2 (its binary representation).

Fenwick Trees exploit this: instead of storing prefix sums directly, they store partial sums at strategic positions.

Position ii stores the sum of a range whose length is the lowest set bit of ii. Example: position 1212 (binary 11001100) stores sum of 44 elements (lowest bit is 100=4100 = 4).

Position 88 (binary 10001000) stores sum of 88 elements. Position 77 (binary 111111) stores sum of 11 element. To get prefix sum up to position ii:

1.1. Add tree[i]

2.2. Remove the lowest set bit from ii

3.3. Repeat until ii becomes 00 This visits at most logn\log n positions.