Struct RecursiveBST

Source
pub struct RecursiveBST<T: Ord> { /* private fields */ }
Expand description

Recursive Binary Search Tree implementation.

§Important

It is also important to note that RecursiveBST is more likely to blow the stack and is generally less performant compared to IterativeBST.

For more information on why that is the case, please have a look at The Story of Tail Call Optimizations in Rust.

Implementations§

Source§

impl<T: Ord> RecursiveBST<T>

Source

pub fn new() -> RecursiveBST<T>

Creates an empty RecursiveBST<T>

No nodes are allocated on the heap yet

§Examples
use bst_rs::{BinarySearchTree, RecursiveBST};

// Empty tree is created
let mut bst: RecursiveBST<i32> = RecursiveBST::new();
assert!(bst.is_empty())

Trait Implementations§

Source§

impl<T: Ord> BinarySearchTree<T> for RecursiveBST<T>

Source§

fn size(&self) -> usize

Returns the total number of nodes within the tree.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(5);
bst.insert(10);
bst.insert(3);

assert_eq!(bst.size(), 3);
Source§

fn is_empty(&self) -> bool

Returns true if the binary search tree contains no nodes.

§Examples
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst: RecursiveBST<i32> = RecursiveBST::new();
assert!(bst.is_empty());
Source§

fn is_not_empty(&self) -> bool

Returns true if the binary search tree contains one or more nodes.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
assert!(bst.is_empty());

bst.insert(2);

assert!(bst.is_not_empty());
Source§

fn insert(&mut self, value: T)

Inserts given value as a node.

Duplicate values are not allowed.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();

bst.insert(10);
bst.insert(10);   // Element is not inserted
bst.insert(5);
bst.insert(2);
bst.insert(15);
bst.insert(25);

assert_eq!(bst.size(), 5);
Source§

fn contains(&self, value: &T) -> bool

Returns true if the binary search tree contains an element with the given value.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(5);
bst.insert(2);
bst.insert(7);

assert!(bst.contains(&5));
assert!(!bst.contains(&10));
Source§

fn remove(&mut self, value: &T)

Removes the given value.

Tree will not be modified if trying to remove element that does not exist.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(5);
bst.insert(2);
bst.insert(7);
assert_eq!(bst.size(), 3);

bst.remove(&5);
bst.remove(&10); // Element is not removed
assert_eq!(bst.size(), 2);
Source§

fn retrieve(&self, value: &T) -> Option<&T>

Returns a reference to the element or None if element does not exist.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(5);
bst.insert(2);
bst.insert(7);

assert_eq!(bst.retrieve(&5), Some(&5));
assert_eq!(bst.retrieve(&10), None);
Source§

fn retrieve_as_mut(&mut self, value: &T) -> Option<&mut T>

Returns a mutable reference to the element (see RecursiveBST::retrieve()) or None if element does not exist.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(10);
bst.insert(5);

let optional_retrieved_value_as_mut = bst.retrieve_as_mut(&5);
assert_eq!(optional_retrieved_value_as_mut, Some(&mut 5));

let mut retrieved_value = optional_retrieved_value_as_mut.unwrap();
*retrieved_value = 2; // Change value inside tree to '2'

assert_eq!(bst.retrieve_as_mut(&5), None); // 5 does not exist anymore
assert_eq!(bst.retrieve_as_mut(&2), Some(&mut 2));
Source§

fn height(&self) -> Option<isize>

Returns the height or None if tree is empty.

The height is the number of edges between the root and it’s furthest leaf node.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

// Given a tree that looks like:
 //         4
 //       /  \
 //      2    6
 //     / \  / \
 //    1  3 5   7
let mut bst = RecursiveBST::new();
assert_eq!(bst.height(), None);

bst.insert(4);
bst.insert(6);
bst.insert(2);
bst.insert(7);
bst.insert(5);
bst.insert(3);
bst.insert(1);

// The height is 2.
assert_eq!(bst.height(), Some(2));
Source§

fn min(&self) -> Option<&T>

Returns a reference to the minimum element of the tree or None if tree is empty.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
assert_eq!(bst.min(), None);

bst.insert(5);
bst.insert(2);
bst.insert(10);

assert_eq!(bst.min(), Some(&2));
Source§

fn max(&self) -> Option<&T>

Returns a reference to the maximum element of the tree or None if tree is empty.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
assert_eq!(bst.max(), None);

bst.insert(5);
bst.insert(2);
bst.insert(10);

assert_eq!(bst.max(), Some(&10));
Source§

fn remove_min(&mut self) -> Option<T>

Removes and returns the minimum element from the tree or None if tree is empty.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
assert_eq!(bst.remove_min(), None);

bst.insert(2);
bst.insert(5);
bst.insert(10);

assert_eq!(bst.size(), 3);
assert_eq!(bst.remove_min(), Some(2));
assert_eq!(bst.size(), 2);
Source§

fn remove_max(&mut self) -> Option<T>

Removes and returns the maximum element from the tree or None if tree is empty.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
assert_eq!(bst.remove_max(), None);

bst.insert(2);
bst.insert(5);
bst.insert(10);

assert_eq!(bst.size(), 3);
assert_eq!(bst.remove_max(), Some(10));
assert_eq!(bst.size(), 2);
Source§

fn asc_order_vec(&self) -> Vec<&T>

Returns references to the elements of the tree in ascending order.

§Important

This is function is analogous to RecursiveBST::in_order_vec() as the underlying behaviour is exactly the same.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(4);
bst.insert(6);
bst.insert(2);
bst.insert(7);
bst.insert(5);
bst.insert(3);
bst.insert(1);

assert_eq!(bst.asc_order_vec(), vec![&1, &2, &3, &4, &5, &6, &7]);
Source§

fn pre_order_vec(&self) -> Vec<&T>

Returns references to the elements of the tree in the order of a pre-order traversal.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

// Given a tree that looks like:
 //         4
 //       /  \
 //      2    6
 //     / \  / \
 //    1  3 5   7
let mut bst = RecursiveBST::new();
bst.insert(4);
bst.insert(6);
bst.insert(2);
bst.insert(7);
bst.insert(5);
bst.insert(3);
bst.insert(1);

// The pre_order_vec is: [&4, &2, &1, &3, &6, &5, &7]
assert_eq!(bst.pre_order_vec(), vec![&4, &2, &1, &3, &6, &5, &7]);
Source§

fn in_order_vec(&self) -> Vec<&T>

Returns references to the elements of the tree in the order of an in-order traversal.

§Important

This is function is analogous to RecursiveBST::asc_order_vec() as the underlying behaviour is exactly the same.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

// Given a tree that looks like:
 //         4
 //       /  \
 //      2    6
 //     / \  / \
 //    1  3 5   7
let mut bst = RecursiveBST::new();
bst.insert(4);
bst.insert(6);
bst.insert(2);
bst.insert(7);
bst.insert(5);
bst.insert(3);
bst.insert(1);

// The in_order_vec is: [&1, &2, &3, &4, &5, &6, &7]
assert_eq!(bst.in_order_vec(), vec![&1, &2, &3, &4, &5, &6, &7]);
Source§

fn post_order_vec(&self) -> Vec<&T>

Returns references to the elements of the tree in the order of a post-order traversal.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

// Given a tree that looks like:
 //         4
 //       /  \
 //      2    6
 //     / \  / \
 //    1  3 5   7
let mut bst = RecursiveBST::new();
bst.insert(4);
bst.insert(6);
bst.insert(2);
bst.insert(7);
bst.insert(5);
bst.insert(3);
bst.insert(1);

// The post_order_vec is: [&1, &3, &2, &5, &7, &6, &4]
assert_eq!(bst.post_order_vec(), vec![&1, &3, &2, &5, &7, &6, &4]);
Source§

fn level_order_vec(&self) -> Vec<&T>

Returns references to the elements of the tree in the order of a level-order traversal.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

// Given a tree that looks like:
 //         4
 //       /  \
 //      2    6
 //     / \  / \
 //    1  3 5   7
let mut bst = RecursiveBST::new();
bst.insert(4);
bst.insert(6);
bst.insert(2);
bst.insert(7);
bst.insert(5);
bst.insert(3);
bst.insert(1);

// The level_order_vec is: [&4, &2, &6, &1, &3, &5, &7]
assert_eq!(bst.level_order_vec(), vec![&4, &2, &6, &1, &3, &5, &7]);
Source§

fn asc_order_iter(&self) -> IntoIter<&T>

Returns an iterator over RecursiveBST::asc_order_vec().

§Important

This is function is analogous to RecursiveBST::in_order_iter() as the underlying behaviour is exactly the same.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut asc_order_iter = bst.asc_order_iter();

assert_eq!(asc_order_iter.next(), Some(&1));
assert_eq!(asc_order_iter.next(), Some(&2));
assert_eq!(asc_order_iter.next(), Some(&3));
assert_eq!(asc_order_iter.next(), Some(&4));
assert_eq!(asc_order_iter.next(), Some(&5));
assert_eq!(asc_order_iter.next(), None);
Source§

fn pre_order_iter(&self) -> IntoIter<&T>

Returns an iterator over RecursiveBST::pre_order_vec().

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut pre_order_iter = bst.pre_order_iter();

assert_eq!(pre_order_iter.next(), Some(&3));
assert_eq!(pre_order_iter.next(), Some(&1));
assert_eq!(pre_order_iter.next(), Some(&2));
assert_eq!(pre_order_iter.next(), Some(&4));
assert_eq!(pre_order_iter.next(), Some(&5));
assert_eq!(pre_order_iter.next(), None);
Source§

fn in_order_iter(&self) -> IntoIter<&T>

Returns an iterator over RecursiveBST::in_order_vec().

§Important

This is function is analogous to RecursiveBST::asc_order_iter() as the underlying behaviour is exactly the same.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut in_order_iter = bst.in_order_iter();

assert_eq!(in_order_iter.next(), Some(&1));
assert_eq!(in_order_iter.next(), Some(&2));
assert_eq!(in_order_iter.next(), Some(&3));
assert_eq!(in_order_iter.next(), Some(&4));
assert_eq!(in_order_iter.next(), Some(&5));
assert_eq!(in_order_iter.next(), None);
Source§

fn post_order_iter(&self) -> IntoIter<&T>

Returns an iterator over RecursiveBST::post_order_vec().

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut post_order_iter = bst.post_order_iter();

assert_eq!(post_order_iter.next(), Some(&2));
assert_eq!(post_order_iter.next(), Some(&1));
assert_eq!(post_order_iter.next(), Some(&5));
assert_eq!(post_order_iter.next(), Some(&4));
assert_eq!(post_order_iter.next(), Some(&3));
assert_eq!(post_order_iter.next(), None);
Source§

fn level_order_iter(&self) -> IntoIter<&T>

Returns an iterator over RecursiveBST::level_order_vec().

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut level_order_iter = bst.level_order_iter();

assert_eq!(level_order_iter.next(), Some(&3));
assert_eq!(level_order_iter.next(), Some(&1));
assert_eq!(level_order_iter.next(), Some(&4));
assert_eq!(level_order_iter.next(), Some(&2));
assert_eq!(level_order_iter.next(), Some(&5));
assert_eq!(level_order_iter.next(), None);
Source§

fn into_asc_order_iter(self) -> IntoIter<T>

Returns RecursiveBST::asc_order_iter() AND consumes the tree.

§Important

This is function is analogous to RecursiveBST::into_in_order_iter() as the underlying behaviour is exactly the same.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut into_asc_order_iter = bst.into_asc_order_iter();

assert_eq!(into_asc_order_iter.next(), Some(1));
assert_eq!(into_asc_order_iter.next(), Some(2));
assert_eq!(into_asc_order_iter.next(), Some(3));
assert_eq!(into_asc_order_iter.next(), Some(4));
assert_eq!(into_asc_order_iter.next(), Some(5));
assert_eq!(into_asc_order_iter.next(), None);

// bst.insert(10); -> COMPILE ERROR
Source§

fn into_pre_order_iter(self) -> IntoIter<T>

Returns RecursiveBST::pre_order_iter() AND consumes the tree.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut into_pre_order_iter = bst.into_pre_order_iter();

assert_eq!(into_pre_order_iter.next(), Some(3));
assert_eq!(into_pre_order_iter.next(), Some(1));
assert_eq!(into_pre_order_iter.next(), Some(2));
assert_eq!(into_pre_order_iter.next(), Some(4));
assert_eq!(into_pre_order_iter.next(), Some(5));
assert_eq!(into_pre_order_iter.next(), None);

// bst.insert(10); -> COMPILE ERROR
Source§

fn into_in_order_iter(self) -> IntoIter<T>

Returns RecursiveBST::in_order_iter() AND consumes the tree.

§Important

This is function is analogous to RecursiveBST::asc_order_iter() as the underlying behaviour is exactly the same.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut into_in_order_iter = bst.into_in_order_iter();

assert_eq!(into_in_order_iter.next(), Some(1));
assert_eq!(into_in_order_iter.next(), Some(2));
assert_eq!(into_in_order_iter.next(), Some(3));
assert_eq!(into_in_order_iter.next(), Some(4));
assert_eq!(into_in_order_iter.next(), Some(5));
assert_eq!(into_in_order_iter.next(), None);

// bst.insert(10); -> COMPILE ERROR
Source§

fn into_post_order_iter(self) -> IntoIter<T>

Returns RecursiveBST::post_order_iter() AND consumes the tree.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut into_post_order_iter = bst.into_post_order_iter();

assert_eq!(into_post_order_iter.next(), Some(2));
assert_eq!(into_post_order_iter.next(), Some(1));
assert_eq!(into_post_order_iter.next(), Some(5));
assert_eq!(into_post_order_iter.next(), Some(4));
assert_eq!(into_post_order_iter.next(), Some(3));
assert_eq!(into_post_order_iter.next(), None);

// bst.insert(10); -> COMPILE ERROR
Source§

fn into_level_order_iter(self) -> IntoIter<T>

Returns RecursiveBST::level_order_iter() AND consumes the tree.

§Example
use bst_rs::{BinarySearchTree, RecursiveBST};

let mut bst = RecursiveBST::new();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);

let mut into_level_order_iter = bst.into_level_order_iter();

assert_eq!(into_level_order_iter.next(), Some(3));
assert_eq!(into_level_order_iter.next(), Some(1));
assert_eq!(into_level_order_iter.next(), Some(4));
assert_eq!(into_level_order_iter.next(), Some(2));
assert_eq!(into_level_order_iter.next(), Some(5));
assert_eq!(into_level_order_iter.next(), None);

// bst.insert(10); -> COMPILE ERROR
Source§

impl<T: Ord + Clone> Clone for RecursiveBST<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Ord> Debug for RecursiveBST<T>

Source§

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

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

impl<T: Ord> Default for RecursiveBST<T>

Source§

fn default() -> RecursiveBST<T>

Creates an empty RecursiveBST<T>

Source§

impl<T: Ord + Debug> Display for RecursiveBST<T>

Source§

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

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

impl<T: Ord> Extend<T> for RecursiveBST<T>

Source§

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

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<T: Ord + Clone> From<&[T]> for RecursiveBST<T>

Source§

fn from(slice: &[T]) -> Self

Converts to this type from the input type.
Source§

impl<T: Ord> From<Vec<T>> for RecursiveBST<T>

Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Ord> FromIterator<T> for RecursiveBST<T>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<T: Ord> PartialEq for RecursiveBST<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T> Freeze for RecursiveBST<T>

§

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

§

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

§

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

§

impl<T> Unpin for RecursiveBST<T>

§

impl<T> UnwindSafe for RecursiveBST<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> 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.