Overview
A Fenwick tree stores partial sums or other invertible prefix aggregates in a compact array. Updates and prefix queries jump through indices by adding or removing the least significant set bit.
Maintains prefix aggregates in an array using index ranges encoded by low bits.
Compact metadata for implementation and discovery.
A Fenwick tree stores partial sums or other invertible prefix aggregates in a compact array. Updates and prefix queries jump through indices by adding or removing the least significant set bit.
Quick facts
Runtime and memory behavior at a glance.
Best
O(n)
Average
O(log n)
Worst
O(log n)
Space
O(n)
index_next = index + (index & -index)Moves upward through Binary Indexed Tree ranges during an update.
Show indexes covering ranges determined by their lowest set bit.
Fenwick tree APIs should state index base and whether query endpoints are inclusive.
What each reference supports for this entry.
Original reference for binary indexed trees and cumulative frequency updates.
Open sourceAlgorithms textbook support for Fenwick Tree's invariant, correctness argument, and asymptotic analysis.
Open sourceGood next jumps from the relationship graph, ranked for useful discovery.
Fenwick trees are smaller and simpler for prefix-style aggregates.
Both answer aggregate-style queries from precomputed indexed summaries, but with different update tradeoffs.