Struct CountTree

Source
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>

Source

pub fn new() -> CountTree<T>

Returns an empty CountTree

Source

pub fn is_empty(&self) -> bool

Returns true if the tree contains no elements.

Source

pub fn len(&self) -> usize

Returns the number elements in the tree. Time complexity: O(1)

Source

pub fn clear(&mut self)

Clears the tree, dropping all elements iteratively.

Source

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))

Source

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))

Source

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()

Source

pub fn push_front(&mut self, value: T)

Prepends an element at the beginning.

Source

pub fn push_back(&mut self, value: T)

Appends an element at the end.

Source

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.

Source

pub fn pop_front(&mut self) -> Option<T>

Removes and returns the first element, or None if empty.

Source

pub fn pop_back(&mut self) -> Option<T>

Removes and returns the last element, or None if empty.

Trait Implementations§

Source§

impl<T> BinaryTree for CountTree<T>

Source§

type Node = CountNode<T>

Source§

fn root(&self) -> Option<&Self::Node>

Source§

impl<T> Debug for CountTree<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for CountTree<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> FromIterator<T> for CountTree<T>

Source§

fn from_iter<I>(iterable: I) -> Self
where I: IntoIterator<Item = T>,

Time complexity: Θ(n + log2(n))

Source§

impl<'a, T> IntoIterator for &'a CountTree<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for CountTree<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for CountTree<T>

§

impl<T> RefUnwindSafe for CountTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for CountTree<T>
where T: Send,

§

impl<T> Sync for CountTree<T>
where T: Sync,

§

impl<T> Unpin for CountTree<T>

§

impl<T> UnwindSafe for CountTree<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.