Struct bintree::BinTree

source ·
pub struct BinTree<T> { /* private fields */ }
Expand description

A binary tree.

Implementations§

source§

impl<T> BinTree<T>

source

pub fn new(inner: T) -> Self

Creates a new binary tree root node. Left and right nodes are automatically set to None.

Example
use bintree::BinTree;

let my_tree: BinTree<i32> = BinTree::new(8);
source

pub fn new_with_nodes(inner: T, left: T, right: T) -> Self

Creates a new binary tree root node, but also allows immediatly setting the left and right nodes.

Example
use bintree::BinTree;

let my_tree: BinTree<i32> = BinTree::new_with_nodes(1, 2, 3);
assert_eq!(my_tree.get_inner(), &1);
assert_eq!(my_tree.get_left().unwrap().get_inner(), &2);
assert_eq!(my_tree.get_right().unwrap().get_inner(), &3);
source

pub fn boxed(self) -> Box<Self>

Returns a Boxed version of the node itself.

Example
use bintree::BinTree;

let my_tree: BinTree<i32> = BinTree::new(25);
let boxed: Box<BinTree<i32>> = my_tree.boxed();
// `my_tree` is no longer valid
assert_eq!(boxed.get_inner(), &25);
source

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

Sets the inner value of the node.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new(8);

assert_eq!(my_tree.get_inner(), &8);

my_tree.set_inner(9);

assert_eq!(my_tree.get_inner(), &9);
source

pub fn set_left(&mut self, value: Option<T>)

Sets the left value of the node.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new(8);
my_tree.set_left(Some(9));

assert_eq!(my_tree.get_left().unwrap().get_inner(), &9);
source

pub fn clear_left(&mut self)

Clears the node’s left value. This is the same as calling set_left(None) but it’s more clear.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(1, 2, 3);
my_tree.clear_left();

assert_eq!(my_tree.get_inner(), &1);
assert!(my_tree.get_left().is_none());
assert_eq!(my_tree.get_right().unwrap().get_inner(), &3);
source

pub fn set_right(&mut self, value: Option<T>)

Sets the right value of the node.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new(8);
my_tree.set_right(Some(9));

assert_eq!(my_tree.get_right().unwrap().get_inner(), &9);
source

pub fn clear_right(&mut self)

Clears the node’s right value. This is the same as calling set_right(None) but it’s more clear.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(1, 2, 3);
my_tree.clear_right();

assert_eq!(my_tree.get_inner(), &1);
assert_eq!(my_tree.get_left().unwrap().get_inner(), &2);
assert!(my_tree.get_right().is_none());
source

pub fn get_inner(&self) -> &T

Returns a borrowed reference to the inner value of the node.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new(1);
assert_eq!(my_tree.get_inner(), &1);
source

pub fn get_inner_mut(&mut self) -> &mut T

Returns a borrowed mutable reference to the inner value of the node.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new(1);

assert_eq!(my_tree.get_inner(), &1);

let inner = my_tree.get_inner_mut();
*inner = 99;

assert_eq!(my_tree.get_inner(), &99);
source

pub fn take_inner(self) -> T

Returns the inner value of the node while also consuming the node itself.

Example
use bintree::BinTree;

let my_tree = BinTree::new(1);
assert_eq!(my_tree.take_inner(), 1);
source

pub fn get_left(&self) -> Option<&Self>

Returns the left value of the node.

Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(1, 2, 3);
assert_eq!(my_tree.get_left().unwrap().get_inner(), &2);
source

pub fn get_left_mut(&mut self) -> Option<&mut Self>

Returns a mutable reference to the left value of the node.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(1, 2, 3);

assert_eq!(my_tree.get_left().unwrap().get_inner(), &2);

let left = my_tree.get_left_mut().unwrap();
left.set_inner(99);

assert_eq!(my_tree.get_left().unwrap().get_inner(), &99);
source

pub fn take_left(self) -> Option<Box<Self>>

Returns the left value of the node while also consuming the node itself.

Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(1, 2, 3);
let left_node = my_tree.take_left().unwrap();
assert_eq!(left_node.get_inner(), &2);
source

pub fn pop_left(&mut self) -> Option<Box<Self>>

Pops the left value of the node, leaving None in it’s place.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(1, 2, 3);
let left_value = my_tree.pop_left();
assert!(my_tree.get_left().is_none());
source

pub fn get_right(&self) -> Option<&Self>

Returns the right value of the node.

Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(1, 2, 3);
assert_eq!(my_tree.get_right().unwrap().get_inner(), &3);
source

pub fn get_right_mut(&mut self) -> Option<&mut Self>

Returns a mutable reference to the right value of the node.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(1, 2, 3);

assert_eq!(my_tree.get_right().unwrap().get_inner(), &3);

let right = my_tree.get_right_mut().unwrap();
right.set_inner(99);

assert_eq!(my_tree.get_right().unwrap().get_inner(), &99);
source

pub fn take_right(self) -> Option<Box<Self>>

Returns the right value of the node while also consuming the node itself.

Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(1, 2, 3);
let right_node = my_tree.take_right().unwrap();
assert_eq!(right_node.get_inner(), &3);
source

pub fn pop_right(&mut self) -> Option<Box<Self>>

Pops the right value of the node, leaving None in it’s place.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(1, 2, 3);
let right_value = my_tree.pop_right();
assert!(my_tree.get_right().is_none());
source

pub fn collect_values(self) -> (T, Option<T>, Option<T>)

Returns a tuple containing:

  1. The inner value
  2. The left value
  3. The right value
Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(-77, 31, 128);
let numbers = my_tree.collect_values();

assert_eq!(numbers.0, -77);
assert_eq!(numbers.1, Some(31));
assert_eq!(numbers.2, Some(128));
source

pub fn len(&self) -> usize

Calculate the amount of values the binary tree is holding.

Note

This method also counts the head node.

Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(-77, 31, 128);
assert_eq!(my_tree.len(), 3);
source

pub fn collect_nodes(&self) -> Vec<&BinTree<T>>

Returns a Vec of references to all nodes in the binary tree. This allows “browsing” the tree as if it was an array.

Notes

This method is recursive. Traveling left is prioritized.

Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(-77, 31, 128);
let nodes = my_tree.collect_nodes();

assert_eq!(nodes[0].get_inner(), &31);
assert_eq!(nodes[1].get_inner(), &128);
assert!(nodes.get(2).is_none());
source

pub fn collect_all_values(&self) -> Vec<&T>

Returns a Vec of references to all values the binary tree is holding. The returned array will always start with the head node’s value.

Example
use bintree::BinTree;

let my_tree = BinTree::new_with_nodes(-77, 31, 128);
let values = my_tree.collect_all_values();

assert_eq!(values[0], &-77);
assert_eq!(values[1], &31);
assert_eq!(values[2], &128);
assert!(values.get(3).is_none());
source

pub fn into_fast_iter(&self) -> IntoIter<&T>

Creates a very simple Vec iterator by collecting all values using collect_all_values().

This is equivalent to collect_all_values().into_iter().

Notes

Creating this kind of iterator may be slow, as the tree needs to be iterated to collect all the values first. Since collect_all_values() returns references, not copies, the memory usage by the iterator should be roughly size_of::<&T>() * tree.len().

source

pub fn leftmost(&self) -> &Self

Returns the leftmost node in the tree.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(-77, 31, 128);
let left = my_tree.get_left_mut().unwrap();
left.set_left(Some(69));

/*
  Tree:
     (-77)
     /   \
   (31)  (128)
   /
 (69)
*/

assert_eq!(my_tree.leftmost().get_inner(), &69);
source

pub fn rightmost(&self) -> &Self

Returns the rightmost node in the tree.

Example
use bintree::BinTree;

let mut my_tree = BinTree::new_with_nodes(-77, 36, 128);
let left = my_tree.get_right_mut().unwrap();
left.set_right(Some(79));

/*
  Tree:
     (-77)
     /   \
   (36)  (128)
             \
             (79)
*/

assert_eq!(my_tree.rightmost().get_inner(), &79);
source§

impl<T: Clone> BinTree<T>

source

pub fn clone_inner(&self) -> T

Returns a cloned copy of the node’s inner value.

source

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

Returns a cloned copy of the node’s left value.

source

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

Returns a cloned copy of the node’s right value.

Trait Implementations§

source§

impl<T: Clone> Clone for BinTree<T>

source§

fn clone(&self) -> Self

Returns a copy 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> Debug for BinTree<T>

source§

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

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

impl<T> Deref for BinTree<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for BinTree<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T: Display> Display for BinTree<T>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.