pub struct CountTree<T>(/* private fields */);
Expand description
Counting tree.
A balanced binary tree which keeps track of total number of child nodes in each node, so that elements can be inserted and deleted using its in-order index. The algorithm used internally is a variation of AVL Tree. Time complexities mentioned below are that of worst case scenario (and are of the same order as that of an AVL tree).
§Examples
let mut ct: CountTree<i32> = CountTree::new();
ct.push_front(20);
ct.push_front(10);
assert_eq!(ct.pop_back().unwrap(), 20);
You can also use collect
to create one from an iterator. This has a time
complexity of O(n), which is much more efficient than inserting iteratively.
let mut ct: CountTree<i32> = (0..100).collect();
assert_eq!(ct.remove(32), 32);
Implementations§
Source§impl<T> CountTree<T>
impl<T> CountTree<T>
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns the element at the given index, or None
if index is out of
bounds. Time complexity: O(log(n))
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index, or None
if out of bounds. Time complexity: O(log(n))
Sourcepub fn insert(&mut self, index: usize, value: T)
pub fn insert(&mut self, index: usize, value: T)
Inserts an element at the given index. Time complexity: O(log(n))
§Panics
Panics if index is greater than self.len()
Sourcepub fn push_front(&mut self, value: T)
pub fn push_front(&mut self, value: T)
Prepends an element at the beginning.
Sourcepub fn remove(&mut self, index: usize) -> T
pub fn remove(&mut self, index: usize) -> T
Removes the element at the given index. Time complexity: O(log(n))
§Panics
Panics if index is out of bounds.
Trait Implementations§
Source§impl<T> BinaryTree for CountTree<T>
impl<T> BinaryTree for CountTree<T>
Source§impl<T> FromIterator<T> for CountTree<T>
impl<T> FromIterator<T> for CountTree<T>
Source§fn from_iter<I>(iterable: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iterable: I) -> Selfwhere
I: IntoIterator<Item = T>,
Time complexity: Θ(n + log2(n))