A Fenwick tree or binary indexed tree (BIT) is a data structure that can efficiently update elements and calculate
prefix sum In computer science, the prefix sum, cumulative sum, inclusive scan, or simply scan of a sequence of numbers is a second sequence of numbers , the sums of prefixes (running totals) of the input sequence:
:
:
:
:...
For instance, the prefix sums of ...
s in a table of numbers.
This structure was proposed by Boris Ryabko in 1989
with a further modification published in 1992.
It has subsequently become known under the name Fenwick tree after
Peter Fenwick, who described this structure in his 1994 article.
When compared with a flat array of numbers, the Fenwick tree achieves a much better balance between two operations: element update and prefix sum calculation. A flat array of
numbers can either store the elements or the prefix sums. In the first case, computing prefix sums requires linear time; in the second case, updating the array elements requires linear time (in both cases, the other operation can be performed in constant time). Fenwick trees allow both operations to be performed in
time. This is achieved by representing the numbers as a
tree
In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, including only woody plants with secondary growth, plants that are ...
with
nodes where the value of each node in the tree is the prefix sum of the array from the index of the parent (inclusive) up to the index of the node (exclusive). The tree itself is implicit and can be stored as an array of
numbers, with the implicit root node omitted from the array. The tree structure allows the operations of element retrieval, element update, prefix sum, and range sum to be performed using only
node accesses.
Motivation
Given a table of elements, it is sometimes desirable to calculate the
running total {{Unreferenced, date=June 2019, bot=noref (GreenC bot)
A running total or rolling total is the summation of a sequence of numbers which is updated each time a new number is added to the sequence, by adding the value of the new number to the previous ...
of values up to each index according to some
associative
In mathematics, the associative property is a property of some binary operations, which means that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a valid rule of replacement ...
binary operation
In mathematics, a binary operation or dyadic operation is a rule for combining two elements (called operands) to produce another element. More formally, a binary operation is an operation of arity two.
More specifically, an internal binary op ...
(addition on integers being by far the most common). Fenwick trees provide a method to query the running total at any index, in addition to allowing changes to the underlying value table and having all further queries reflect those changes.
Fenwick trees are particularly designed to implement the
arithmetic coding
Arithmetic coding (AC) is a form of entropy encoding used in lossless data compression. Normally, a string of characters is represented using a fixed number of bits per character, as in the ASCII code. When a string is converted to arithmetic ...
algorithm, which maintains counts of each symbol produced and needs to convert those to the
cumulative probability of a symbol less than a given symbol. Development of operations it supports were primarily motivated by use in that case.
Using a Fenwick tree it requires only
operations to compute any desired cumulative sum, or more generally the sum of any range of values (not necessarily starting at zero). It is also possible to construct extensions of this data structure to quickly compute cumulative sums on
-dimensional arrays in
time.
Description
A Fenwick tree is most easily understood by considering a
one-based array