pub struct RedBlackTree<T> { /* private fields */ }Expand description
A Red-black tree which is an approximately balanced binary search tree.
All RedBlackTree tree binary search tree operations: insert, remove, contains, min, max, predecessor, successor have a worst case time complexity of O(log n).
Implementations§
Source§impl<T> RedBlackTree<T>
impl<T> RedBlackTree<T>
Sourcepub fn insert(&mut self, t: T)where
T: Ord,
pub fn insert(&mut self, t: T)where
T: Ord,
Inserts the given value into the tree. Duplicates are permitted and will lead to duplicate values in the tree.
This operation is O(log n).
Sourcepub fn remove<Q>(&mut self, q: &Q) -> Option<T>
pub fn remove<Q>(&mut self, q: &Q) -> Option<T>
Removes the first pre-order node with the given value from the tree, returning the removed value, if there was one to remove.
This operation is O(log n).
Sourcepub fn min(&self) -> Option<&T>
pub fn min(&self) -> Option<&T>
Returns the minimum value, returning None if the tree is empty.
This operation is O(log n).
To obtain the node with the minimum value &Node<T> use max_node().
Sourcepub fn extract_min(&mut self) -> Option<T>where
T: Ord,
pub fn extract_min(&mut self) -> Option<T>where
T: Ord,
Removes and returns the minimum value, returning None if the tree is empty.
This operation is O(log n).
If the minimum value is not unique in the tree, the first pre-order node is removed.
To obtain the value without removing it use min().
Sourcepub fn max(&self) -> Option<&T>
pub fn max(&self) -> Option<&T>
Returns the maximum value, returning None if the tree is empty.
This operation is O(log n).
To obtain the node with the maximum value, use max_node().
Sourcepub fn extract_max(&mut self) -> Option<T>where
T: Ord,
pub fn extract_max(&mut self) -> Option<T>where
T: Ord,
Removes and returns the maximum value, returning None if the tree is empty.
This operation is O(log n).
If the maximum value is not unique in the tree, the first pre-order node is removed.
To obtain the value without removing it use max().
Sourcepub fn predecessor<'t>(&self, t: &'t T) -> Option<&T>where
T: Ord,
pub fn predecessor<'t>(&self, t: &'t T) -> Option<&T>where
T: Ord,
Returns the predecessor value, which is the previous in-order value, of the given value.
This operation is O(log n).
To obtain the predecessor node, use get_node(t).map_or_default(|n| n.predecessor()).
Sourcepub fn successor<'t>(&self, t: &'t T) -> Option<&T>where
T: Ord,
pub fn successor<'t>(&self, t: &'t T) -> Option<&T>where
T: Ord,
Returns the successor value, which is the next in-order value, of the given value.
This operation is O(log n).
To obtain the successor node, use get_node(t).map_or_default(|n| n.successor()).
Sourcepub fn contains(&self, t: &T) -> boolwhere
T: Ord,
pub fn contains(&self, t: &T) -> boolwhere
T: Ord,
Returns true if the tree contains the given value.
This operation is O(log n).
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all values from the tree.
This operation is O(n) and is equivalent to dropping the tree and creating a new one.
Source§impl<T> RedBlackTree<T>
impl<T> RedBlackTree<T>
Sourcepub fn get_node<'a, Q>(&'a self, q: &Q) -> Option<&'a Node<T>>
pub fn get_node<'a, Q>(&'a self, q: &Q) -> Option<&'a Node<T>>
Finds the node with the given value in the tree and returns it, if it exists.
This operation is O(n).
If you only need to know whether the value exists, use contains().
Sourcepub fn min_node(&self) -> Option<&Node<T>>
pub fn min_node(&self) -> Option<&Node<T>>
Returns the node with the minimum value.
This operation is O(log n).
To obtain the value directly, use min.
Sourcepub fn max_node(&self) -> Option<&Node<T>>
pub fn max_node(&self) -> Option<&Node<T>>
Returns the node with the maximum value.
This operation is O(log n).
To obtain the value directly, use max.
Trait Implementations§
Source§impl<T: Clone> Clone for RedBlackTree<T>
impl<T: Clone> Clone for RedBlackTree<T>
Source§impl<T: Debug> Debug for RedBlackTree<T>
impl<T: Debug> Debug for RedBlackTree<T>
Source§impl<T> Default for RedBlackTree<T>
impl<T> Default for RedBlackTree<T>
Source§impl<T: Display> Display for RedBlackTree<T>
impl<T: Display> Display for RedBlackTree<T>
Source§impl<T> Drop for RedBlackTree<T>
impl<T> Drop for RedBlackTree<T>
impl<T: Eq> Eq for RedBlackTree<T>
Source§impl<A: Ord> Extend<A> for RedBlackTree<A>
impl<A: Ord> Extend<A> for RedBlackTree<A>
Source§fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)