language-icon Old Web
English
Sign In

Fenwick tree

A Fenwick tree or binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers. (Frequency)Query(BIT1, index) - Query(BIT1, index-1)Alternative 2: This query can be answered in O(1) time by trading off for O(n) space.Update(2, 3) = Query(2) = 5, Query(3) = 3(Cumulative Frequency)Query(BIT1, index)Update(2, 3) = Query(2) = 6, Query(3) = 9(Frequency)Perform operation 1 on each index in the range Range = for(index from L to R) Others can have their own definition.This query can be answered in O(k) time by trading off for O(n) space.Update(2, 3) = Query(2, 4) = (Cumulative Frequency)Range = Update(2, 3) = Query(2, 4) = Query(4) -Query(1) = 12(Frequency)Range = Update(BIT1, R+1, -value)Update(2, 4, 3) = Query(2) = 5, Query(3) = 6(Cumulative Frequency)Range = Update(BIT1, R+1, -value)Update(2, 4, 3) = Query(2) = 6, Query(3) = 12(Frequency)Perform operation 1 on each index in the range Update(BIT1, R+1, -value)Update(2, 4, 3) = Query(2, 4) = (Cumulative Frequency)Range = Update(BIT1, R+1, -value)Update(2, 4, 3) = Query(2, 4) = Query(4) -Query(1) = 18 A Fenwick tree or binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers. This structure was proposed by Boris Ryabko in 1989 with a further modification published in 1992. It has subsequently became known under the name Fenwick tree after Peter Fenwick who has described this structure in his 1994 paper. 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. In a flat array of n {displaystyle n} numbers, you 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 O ( log ⁡ n ) {displaystyle O(log n)} time. This is achieved by representing the numbers as a tree, where the value of each node is the sum of the numbers in that subtree. The tree structure allows operations to be performed using only O ( log ⁡ n ) {displaystyle O(log n)} node accesses. Given a table of elements, it is sometimes desirable to calculate the running total of values up to each index according to some associative binary operation (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 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 O ( log ⁡ n ) {displaystyle O(log n)} operations to compute any desired cumulative sum, or more generally the sum of any range of values (not necessarily starting at zero). Fenwick trees can be extended to update and query subarrays of multidimensional arrays. These operations can be performed with complexity O ( 4 d log d ⁡ n ) {displaystyle O(4^{d}log ^{d}n)} , where d {displaystyle d} is number of dimensions and n {displaystyle n} is the number of elements along each dimension. Although Fenwick trees are trees in concept, in practice they are implemented as an implicit data structure using a flat array analogous to implementations of a binary heap. Given an index in the array representing a vertex, the index of a vertex's parent or child is calculated through bitwise operations on the binary representation of its index. Each element of the array contains the pre-calculated sum of a range of values, and by combining that sum with additional ranges encountered during an upward traversal to the root, the prefix sum is calculated. When a table value is modified, all range sums which contain the modified value are in turn modified during a similar traversal of the tree. The range sums are defined in such a way that both queries and modifications to the table are executed in asymptotically equivalent time ( O ( log ⁡ n ) {displaystyle O(log n)} in the worst case). The initial process of building the Fenwick tree over a table of values runs in O ( n ) {displaystyle O(n)} time. Other efficient operations include locating the index of a value if all values are positive, or all indices with a given value if all values are non-negative. Also supported is the scaling of all values by a constant factor in O ( n ) {displaystyle O(n)} time.

[ "Algorithm", "Discrete mathematics", "Data structure", "Programming language", "Space (mathematics)" ]
Parent Topic
Child Topic
    No Parent Topic