pub struct FenwickTree<I>(/* private fields */);Expand description
A Fenwick tree specialized for primitive integers and addition.
Allowing only primitive integers enables the sum and
set operations along with a O(log n)
update implementation.
See also: GenericFenwickTree.
§Example
use rust_dsa::FenwickTree;
// First, we create an empty tree.
let mut tree = FenwickTree::new();
// Then we push some values.
tree.push(1);
tree.push(4);
tree.push(3);
tree.push(-2);
// We can index into the tree.
assert_eq!(tree[1], 4);
assert_eq!(tree.get(2), Some(3));
assert_eq!(tree.get(4), None);
// And we can calculate sums.
assert_eq!(tree.sum_to(2), 5);
assert_eq!(tree.sum(1..3), 7);
assert_eq!(tree.total(), 6);
// We can also pop values.
assert_eq!(tree.pop(), Some(-2));
assert_eq!(tree.pop(), Some(3));
// We can create trees from iterators.
let mut digits: FenwickTree<u64> = (0..=9).collect();
// And update/set values.
digits.update(2, 4);
digits.set(6, 0);
assert_eq!(digits[2], 6);
assert_eq!(digits[6], 0);
// We can also create trees from arrays.
let more_digits = FenwickTree::from([0, 1, 6, 3, 4, 5, 0, 7, 8, 9]);
assert!(digits == more_digits);§Runtime complexity
| Operation | Runtime Complexity |
|---|---|
FenwickTree::from_iter | O(n) |
FenwickTree::push | O(log n) |
FenwickTree::sum_to | O(log n) |
FenwickTree::sum | O(log n) |
FenwickTree::update | O(log n) |
FenwickTree::set | O(log n) |
FenwickTree::get | O(1) |
FenwickTree::pop | O(1) |
Implementations§
Source§impl<I: PrimInt> FenwickTree<I>
impl<I: PrimInt> FenwickTree<I>
Sourcepub fn new() -> Selfwhere
I: WrappingAdd,
pub fn new() -> Selfwhere
I: WrappingAdd,
Creates a Fenwick tree.
Sourcepub fn push(&mut self, value: I)
pub fn push(&mut self, value: I)
Pushes a value onto the end of the tree.
§Example
use rust_dsa::FenwickTree;
let mut tree = FenwickTree::new();
tree.push(1);
tree.push(4);
tree.push(3);
tree.push(-1);
assert_eq!(tree.len(), 4);
assert_eq!(tree.get(1), Some(4));
assert_eq!(tree.total(), 7);Sourcepub fn get(&self, index: usize) -> Option<I>
pub fn get(&self, index: usize) -> Option<I>
Returns the value at position index if one exists.
§Example
use rust_dsa::FenwickTree;
let tree = FenwickTree::from([8, 4, 2]);
assert_eq!(tree.get(1), Some(4));
assert_eq!(tree.get(3), None);Sourcepub fn pop(&mut self) -> Option<I>
pub fn pop(&mut self) -> Option<I>
Removes and returns the last value in the tree, or returns None if the
tree is empty.
§Example
use rust_dsa::FenwickTree;
let mut tree = FenwickTree::from([8, 4, 2]);
assert_eq!(tree.pop(), Some(2));
assert_eq!(tree.pop(), Some(4));
assert_eq!(tree.pop(), Some(8));
assert_eq!(tree.pop(), None);Sourcepub fn last(&self) -> Option<I>
pub fn last(&self) -> Option<I>
Returns the last value in the tree, or None if the tree is empty.
§Example
use rust_dsa::FenwickTree;
let tree = FenwickTree::from([8, 4, 2]);
assert_eq!(tree.last(), Some(2));
let empty: FenwickTree<u8> = FenwickTree::new();
assert_eq!(empty.last(), None);Sourcepub fn sum_to(&self, end: usize) -> I
pub fn sum_to(&self, end: usize) -> I
Returns the sum of the values with indices in the range [0, end).
§Panics
Panics if end is larger than the number of values in the tree.
§Example
use rust_dsa::FenwickTree;
let tree = FenwickTree::from([1, 2, 3, 4, 5]);
assert_eq!(tree.sum_to(0), 0);
assert_eq!(tree.sum_to(3), 6);
assert_eq!(tree.sum_to(5), 15);Sourcepub fn sum<R: RangeBounds<usize>>(&self, range: R) -> Iwhere
I: WrappingSub,
pub fn sum<R: RangeBounds<usize>>(&self, range: R) -> Iwhere
I: WrappingSub,
Returns the sum of the values with indices in range.
§Panics
Panics if the starting point is greater than the end point or if the end point is greater than the number of values in the tree.
§Example
use rust_dsa::FenwickTree;
let tree: FenwickTree<_> = (2..12).collect();
assert_eq!(tree.sum(4..8), 30);
assert_eq!(tree.sum(..=3), 14);
assert_eq!(tree.sum(5..), 45);
assert_eq!(tree.sum(..), 65);Sourcepub fn total(&self) -> I
pub fn total(&self) -> I
Returns sum of all values in the tree.
§Example
use rust_dsa::FenwickTree;
let tree = FenwickTree::from([1, 2, 3, 4, 5]);
assert_eq!(tree.total(), 15);Sourcepub fn set(&mut self, index: usize, new_value: I)where
I: WrappingSub,
pub fn set(&mut self, index: usize, new_value: I)where
I: WrappingSub,
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of values in the tree.
§Example
use rust_dsa::FenwickTree;
let mut tree = FenwickTree::from([1, 2, 3, 4, 5]);
assert_eq!(tree.len(), 5);
tree.pop();
assert_eq!(tree.len(), 4);Trait Implementations§
Source§impl<I: Clone> Clone for FenwickTree<I>
impl<I: Clone> Clone for FenwickTree<I>
Source§fn clone(&self) -> FenwickTree<I>
fn clone(&self) -> FenwickTree<I>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more