← Range Queries & Strings · guided lesson

Segment Trees, Fenwick & String Matching

1 / 18 · story

the story

Fenwick tree (binary indexed tree)

Every tool here exists because something was too slow or broke without it. Here is what.

1989 · 1994 · Boris Ryabko (1989, 1992); Peter M. Fenwick, University of Auckland (1994)

Adaptive data compression (arithmetic coding) keeps a table of how often each symbol has appeared, and for every symbol it codes it must both update the table and read a running total. Boris Ryabko described this structure in 1989, in a paper on fast adaptive ("on-line") codes, with a further version in 1992. Peter Fenwick described it in 1994 as the "binary indexed tree": one array, where each operation's cost depends on the 1-bits of the index. His name stuck — today almost everyone calls it a Fenwick tree.

the problem it solved

Keep cumulative frequencies correct while they change on every symbol: update and prefix-sum both in O(log n), in a single array.

why it matters: without it

Every "sum or count so far, while values keep changing" question would cost O(n) on one side — reads or writes — and slow down as the data grows.

sources: P. M. Fenwick, A New Data Structure for Cumulative Frequency Tables, Software: Practice and Experience 24(3), 1994 ↗ · P. M. Fenwick, A New Data Structure for Cumulative Probability Tables, Tech. Report 88, University of Auckland (1993) ↗ · B. Ya. Ryabko, A fast on-line code, Doklady Akademii Nauk SSSR 306(3), 1989 (Math-Net.Ru) ↗ · Fenwick tree — history ↗

also in this chapter: Segment tree · Lazy propagation (range updates on a segment tree) · KMP and the prefix function (lps) · Z-function (Z-algorithm) · Rabin–Karp rolling hash