Skip to main content

RedBlackTree

Struct RedBlackTree 

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

Source

pub fn new() -> Self

Returns an empty RedBlackTree.

Source

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

Source

pub fn remove<Q>(&mut self, q: &Q) -> Option<T>
where T: Borrow<Q>, Q: Ord + Eq + ?Sized,

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

pub fn contains(&self, t: &T) -> bool
where T: Ord,

Returns true if the tree contains the given value.

This operation is O(log n).

Source

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

pub fn root(&self) -> Option<&T>

Returns the root value of the tree.

This operation is O(1). To obtain the root node, use root_node().

Source

pub fn len(&self) -> usize

Returns the number of elements in the tree.

Source

pub fn is_empty(&self) -> bool

Returns true if the tree contains no elements.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an in-order iterator over shared references of the values in the tree.

Iterator creation is O(1).

Source§

impl<T> RedBlackTree<T>

Source

pub fn get_node<'a, Q>(&'a self, q: &Q) -> Option<&'a Node<T>>
where T: Borrow<Q>, Q: Ord + ?Sized,

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

Source

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.

Source

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.

Source

pub fn root_node(&self) -> Option<&Node<T>>

Returns the root value of the tree.

This operation is O(1).

Source

pub fn iter_node(&self) -> IterNode<'_, T>

Returns an in-order iterator over shared references of the nodes in the tree.

Iterator creation is O(1).

Trait Implementations§

Source§

impl<T: Clone> Clone for RedBlackTree<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for RedBlackTree<T>

Source§

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

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

impl<T> Default for RedBlackTree<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Display> Display for RedBlackTree<T>

Source§

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

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

impl<T> Drop for RedBlackTree<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Eq> Eq for RedBlackTree<T>

Source§

impl<A: Ord> Extend<A> for RedBlackTree<A>

Source§

fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<A: Ord> FromIterator<A> for RedBlackTree<A>

Source§

fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T> IntoIterator for &'a RedBlackTree<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 RedBlackTree<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
Source§

impl<T: PartialEq> PartialEq for RedBlackTree<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: Send> Send for RedBlackTree<T>

Source§

impl<T: Sync> Sync for RedBlackTree<T>

Auto Trait Implementations§

§

impl<T> Freeze for RedBlackTree<T>

§

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

§

impl<T> Unpin for RedBlackTree<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for RedBlackTree<T>

§

impl<T> UnwindSafe for RedBlackTree<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.