Struct BinaryTree

Source
pub struct BinaryTree<T>
where T: Clone + Ord + Eq + Debug,
{ pub elem: Option<Box<T>>, pub right: Option<Box<BinaryTree<T>>>, pub left: Option<Box<BinaryTree<T>>>, }
Expand description

§Generic Search Binary Tree implementation

Fields§

§elem: Option<Box<T>>

Pointer holding the data can be None

§right: Option<Box<BinaryTree<T>>>

Pointer to the right leaf

§left: Option<Box<BinaryTree<T>>>

Pointer to the left leaf

Implementations§

Source§

impl<T> BinaryTree<T>
where T: Debug + Clone + Ord,

Source

pub fn new(elem: T) -> Self

Creates a new BinaryTree<T>

§Example

Basic usage:

let mut a = BinaryTree::new(1);
Source

pub fn delete(&mut self, del_elem: T) -> Option<T>

Deletes and returns the given element from the tree in O(log n) If the element is not in the tree returns None Basic usage:

let mut a = BinaryTree::new(1);
let x = a.delete(1);
assert_eq!(Some(x), a);
Source

pub fn insert(&mut self, new_elem: T)

Inserts the given element into the tree Time complexity -> O(log n) Basic usage:

let mut a = BinaryTree::new(1);
a.insert(1);
Source

pub fn is_empty(&self) -> bool

Returns true if the list is empty Basic usage:

let mut a = BinaryTree::new(1);
let b = a.is_empty();
assert_eq!(b, false);
Source

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

Returns true if the elemen is in the tree with O(log n) complexity Basic usage:

let mut a = BinaryTree::new(1);
let b = a.contains(1);
assert_eq!(b, true);
Source

pub fn clear(&mut self)

Clears/Deallocates the tree entirely Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
a.clear();
assert_eq!(a.is_empty(), true);
Source

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

Returns the maximum value of the tree in O(log n) Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
let x = a.max().unwrap();
assert_eq!(x, 2);
Source

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

Returns the minimum value of the tree in O(log n) Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
let x = a.min().unwrap();
assert_eq!(x, 1);
Source

pub fn height(&self) -> usize

Returns the height value of the tree in O(log n) Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
let x = a.height().unwrap();
assert_eq!(x, 1);
Source

pub fn count(&self) -> usize

Returns the total number of elements in the tree in O(n) Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
let x = a.count().unwrap();
assert_eq!(x, 2);
Source

pub fn inorder(&self)

Prints to screen the elements in inorder Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
a.inorder();
Source

pub fn preorder(&self)

Prints to screen the elements in preorder Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
a.peorder();
Source

pub fn postorder(&self)

Prints to screen the elements in postorder Basic usage:

let mut a = BinaryTree::new(1);
a.insert(2);
a.postorder();
Source

pub fn vec_insert(&mut self, elems: Vec<T>)

Inserts all the elements in the vector in the tree Basic usage:

let mut a = BinaryTree::new(1);
a.vec_insert([1,2,3]);

Trait Implementations§

Source§

impl<'a, T> IntoIterator for &'a BinaryTree<T>
where T: 'a + Clone + Ord + Eq + Debug,

Source§

fn into_iter(self) -> Self::IntoIter

Creates a iterator that iterates in INORDER Basic usage:

let mut a = BinaryTree::new(1);
a.vec_insert([1,2,3]);
for i in a.into_iter() {
    println!("{}", *i);
}
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = InOrderIter<'a, T>

Which kind of iterator are we turning this into?

Auto Trait Implementations§

§

impl<T> Freeze for BinaryTree<T>

§

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

§

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

§

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

§

impl<T> Unpin for BinaryTree<T>

§

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