A Fenwick tree or binary indexed tree/bit indexed tree is a data structure
that supports the following two operations efficiently over an array of numbers a[0..n]:
- Calculate a prefix sum:
a[0] + a[1] + ... + a[i] - Update one element:
a[i] += delta
With a naïve implementation, only one of the operations can be made to have constant time
complexity while the other one has to be linear. With Fenwick tree, both take only O(log(N)).
This crate is no_std and has no (non-dev) dependencies.
Examples
Use the array module for operations on a 1D Fenwick tree:
use ;
let fw = &mut ; // backing array of Fenwick tree (NOT original array!)
assert_eq!;
assert_eq!;
update; // original array: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert_eq!;
assert_eq!;
update; // original array: [3, 0, 0, 0, 0, 9, 0, 0, 0, 0]
assert_eq!;
assert_eq!;
assert_eq!;
update; // original array: [3, 0, 0, 0, -5, 9, 0, 0, 0, 0]
assert_eq!;
assert_eq!;
update; // original array: [1, 0, 0, 0, -5, 9, 0, 0, 0, 0]
assert_eq!;
assert_eq!;
Use the index module to implement multidimensional Fenwick trees:
use ;
const MAX: usize = 1000;