Data structures / Fenwick trees

Fenwick trees

An array of cleverly chosen partial sums that gives the total of the first k numbers, and absorbs a change to any of them, in a handful of hops. Worked through on eight numbers.

Intermediate7 steps~7 min
Array cell — one of the numbers being summed
Tree cell — the sum of one block of the array
Lowest 1 bit — sets the block length and the hop size
Block in use — read by the current prefix sum
// THE SHORT VERSION

In 30 seconds

A Fenwick tree, or binary indexed tree, answers “what is the sum of the first k numbers?” while those numbers keep changing.

It is just a second array of the same size. Each cell stores the sum of a block of the original, and the block’s length is given by the lowest 1 bit of the cell’s index written in binary.

To read a prefix sum, add a few blocks while stripping that bit. To change a number, fix a few blocks while adding it. Both take about log n steps: at most 20 for a million numbers.

Fenwick trees, step by step

STEPS01 / 07

The sum of any range is two prefix sums: sum(l..r) = sum(r) − sum(l − 1). Indexes start at 1 because 0 has no lowest 1 bit. Array-based segment trees often reserve up to 4n slots.

STEP 01 / 07 The problem: running totals
THE MATH
sum(7) = a1 + ... + a7 = 37 plain array: sum is O(n) stored totals: update is O(n)
  1. Step 1: The problem: running totals. Take eight numbers that keep changing, and a question asked again and again: what is the sum of the first k? A plain array answers by adding up to n cells. Storing every running total answers instantly, but then changing one number means fixing every total after it. Either way one operation costs about n steps.
  2. Step 2: Each cell covers a block. A Fenwick tree, or binary indexed tree, is a second array of the same length. Each cell tree[i] stores the sum of a block of the original array that ends at index i. Odd indexes cover just one cell, tree[2] and tree[6] cover two, tree[4] covers four and tree[8] covers all eight.
  3. Step 3: The lowest-set-bit trick. The block lengths come straight from the index written in binary. Find the lowest 1 bit of i and read it as a number: that is how many cells tree[i] covers. For 6, binary 0110, the lowest 1 bit is worth 2, so tree[6] covers cells 5 and 6. In code this is the one-liner i & -i.
  4. Step 4: Prefix sum: hop down. To sum the first 7 numbers, start at i = 7 and add tree[7]. Subtract the lowest 1 bit to reach 6 and add tree[6], then again to reach 4 and add tree[4]. The next hop lands on 0, so stop. Three blocks tile cells 1 to 7 exactly, and three additions replace seven.
  5. Step 5: Update: hop up. Now add 3 to a[5]. Every tree cell whose block contains index 5 must grow by 3, and there are only three of them. Start at i = 5, then keep adding the lowest 1 bit: 6, then 8. The next hop would be 16, past the end of the array, so stop. Three writes instead of fixing every running total.
  6. Step 6: Both in O(log n). Every hop clears or carries the lowest 1 bit, so the number of hops is limited by how many binary digits the index has, which is about log n. For eight numbers that is at most 4 tree cells. For a million numbers it is at most 20, where a plain scan might add up a million cells.
  7. Step 7: Compact beside a segment tree. A segment tree answers the same questions in the same O(log n) time, but stores a sum for every range it splits into: 2n − 1 nodes, 15 for our eight numbers. The Fenwick tree needs only n cells and two short loops. The segment tree earns its extra space by also handling minimum, maximum and updates to whole ranges.
VALUES SO FAR
a[1..8]5 2 9 3 6 4 8 1input
tree[4]····block
lowbit 6····rule
sum(7)····query
a[5]+=3····update
cost····both
memory····compact
// AFTER

Learn next

Nothing builds on this one yet. See every data structures concept.

Spotted a mistake on this page?