Skip to the workspace
Pebellslearn by building
Prefix Sums & Range Queries  ›  Lesson

Fenwick Trees

Fenwick Trees 32 minSummaries of summaries, packed into one flat list and walked by the lowest set bit

dsa / Fenwick Trees

Explore each scene at your own pace.

01 · The lowest set bit

The lowest set bit

Fenwick Trees

Definition
A Fenwick tree, or binary indexed tree, is a single array in which each cell holds the total of a range whose length is given by the lowest set bit of its index. Prefix sums and point updates both take O(log n) steps.
In plain words
It is a set of nested running totals, like the markings on a ruler. To total the first 7 minutes you add together a few big pieces, one covering 4, one covering 2, one covering 1, instead of seven separate numbers.

In short: a Fenwick tree keeps one flat list where cell i owns a range of i & -i minutes ending at i, found with the lowest-set-bit trick. A prefix query jumps down, clearing the lowest set bit each step; a point update jumps up, adding it — both in O(log n) hops. It needs an aggregate with an inverse (sum, count, XOR), because a range answer is still prefix minus prefix; min has no inverse, which is what the segment tree is for.

A Fenwick list of 8 cells; T[4] is labelled "owns 1-4", and prefix(7) reads T[7], T[6] and T[4]

Square-root blocks got Quarry to O(√n) for both edits and queries. On a month-long log that is a couple of hundred reads, which is fine for one dashboard and not fine for the live counter on Quarry's status page. There, every incoming request is an edit and every refresh is a query, thousands of each per second.

The block idea had one level of summaries. What if there were summaries of summaries, and summaries of those, each level twice as coarse as the one below? Then any prefix could be assembled from about log₂ n pieces. A Fenwick tree (also called a binary indexed tree) does exactly that, and it does it in a single flat list the same length as the log, with no pointers and no nodes. It needs one bit trick. Everything else follows from it.

The lowest set bit

For a positive integer i, i & -i keeps only its lowest 1 bit. Python's integers are two's-complement for bitwise operations, so -i is "flip every bit and add one", which leaves the lowest set bit as the only bit the two share.

python
What does this print?
print(12 & -12, 10 & -10, 7 & -7)
Scene 1 of 8
Project context and further resources
You're building a piece ofQuarry — the analytics engine
This piece — fenwick_log(): Point edits and range totals, both in O(log n), in one flat array.
Scenario Quarry's status page counts requests live: every request is an edit and every refresh is a range total. A Fenwick tree keeps both at O(log n) in a list no bigger than the log itself.
fenwick_log.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
fenwick_log(values, ops) → listAnswers to the sum ops, with add ops applied in between.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.