Struct BinaryTree

Source
pub struct BinaryTree<T>(/* private fields */);
Expand description

BinaryTree

This crate implements a BinaryTree data structure with depth, level order, left/right side view, build a complete tree with count nodes algorithms.

Implementations§

Source§

impl<T> BinaryTree<T>
where T: Copy + Debug + 'static,

Source

pub fn new() -> Self

Create a new BinaryTree

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![Some(1), Some(2), Some(3), None, None, Some(4), Some(5), Some(6)];
tree.insert(&v);
Source

pub fn insert(&mut self, elements: &Vec<Option<T>>)

Build a BinaryTree by a Vector

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![Some(1), Some(2), Some(3), None, None, Some(4), Some(5), Some(6)];
tree.insert(&v);
 
Source

pub fn insert_as_complete(&mut self, elements: &Vec<T>)

Build a complete BinaryTree by a Vector

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![1, 2, 3, 4, 5, 6, 7];
tree.insert_as_complete(&v);
 
Source

pub fn print_preorder(&self, depth: usize)

Traverse a BinaryTree in preorder

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![Some(1), Some(2), Some(3), None, None, Some(4), Some(5), Some(6)];
tree.insert(&v);
tree.print_preorder(0);
 
Source

pub fn depth(&self) -> i32

Get the depth of the BinaryTree

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![Some(1), Some(2), Some(3), None, None, Some(4), Some(5), Some(6)];
tree.insert(&v);
 
let depth = tree.depth();
println!("depth: {}", depth);
assert_eq!(depth, 4);
Source

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

Get the level order of the BinaryTree

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![Some(1), Some(2), Some(3), None, None, Some(4), Some(5), Some(6)];
tree.insert(&v);
 
let level_order = tree.level_order();
println!("level order: {:?}", level_order);
assert_eq!(level_order, vec![vec![1], vec![2, 3], vec![4, 5], vec![6]].to_vec());
Source

pub fn right_side_view(&self, depth: usize, res: &mut Vec<T>)

Get the right side view of the BinaryTree

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![Some(1), Some(2), Some(3), None, None, Some(4), Some(5), Some(6)];
tree.insert(&v);
 
let mut res: Vec<i32> = Vec::new();
tree.right_side_view(0, &mut res);
println!("right side view: {:?}", res);
assert_eq!(res, vec![1, 3, 5, 6]);
Source

pub fn left_side_view(&self, depth: usize, res: &mut Vec<T>)

Get the left side view of the BinaryTree

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![Some(1), Some(2), Some(3), None, None, Some(4), Some(5), Some(6)];
tree.insert(&v);
 
let mut res: Vec<i32> = Vec::new();
tree.left_side_view(0, &mut res);
println!("left side view: {:?}", res);
assert_eq!(res, vec![1, 2, 4, 6]);
Source

pub fn height(&self) -> i32

Get the height of the BinaryTree

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![1, 2, 3, 4, 5, 6, 7];
tree.insert_as_complete(&v);
 
let height = tree.height();
println!("height: {}", 2);
assert_eq!(height, 2);
Source

pub fn count_nodes(&self) -> i32

Get the nodes count of the complete BinaryTree

§Example
use flex_algo::BinaryTree;
 
let mut tree = BinaryTree::new();
let v = vec![1, 2, 3, 4, 5, 6, 7];
tree.insert_as_complete(&v);
 
let count = tree.count_nodes();
println!("count: {}", count);
assert_eq!(count, 7);

Trait Implementations§

Source§

impl<T: Debug> Debug for BinaryTree<T>

Source§

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

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

impl<T: Copy + Debug + 'static> Display for BinaryTree<T>

Source§

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

Formats the value using the given formatter. Read more

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