Struct BST

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

BinarySearchTree

This crate implements a Binary Search Tree data structure with insert, preorder traversal, search and validate algorithms.

Implementations§

Source§

impl<T> BST<T>

Source

pub fn new() -> Self

Create a new BinarySearchTree

§Example
use flex_algo::BST;
 
let mut bst = BST::new();
bst.insert(3);
bst.insert(2);
bst.insert(1);
 
Source§

impl<T> BST<T>
where T: PartialOrd + Copy,

Source

pub fn insert(&mut self, data: T) -> bool

Insert into a new BinarySearchTree

§Example
use flex_algo::BST;
 
let mut bst = BST::new();
bst.insert(3);
bst.insert(2);
bst.insert(1);
 
Source

pub fn is_valid(&self, min: T, max: T) -> bool

Validate a new BinarySearchTree

§Example
use flex_algo::BST;
 
let mut bst = BST::new();
bst.insert(3);
bst.insert(2);
bst.insert(1);
 
let is_valid = bst.is_valid(i32::MIN, i32::MAX);
assert_eq!(is_valid, true);
 
Source

pub fn search(&self, data: T) -> Option<T>

Search a new BinarySearchTree

§Example
use flex_algo::BST;
 
let mut bst = BST::new();
bst.insert(3);
bst.insert(2);
bst.insert(1);
 
let none = bst.search(5);
assert_eq!(none, None);
 
let found = bst.search(2);
assert_eq!(found, Some(2));
 
Source§

impl<T: Debug> BST<T>

Source

pub fn print_preorder(&self, depth: i32)

Traversal a new BinarySearchTree preorder

§Example
use flex_algo::BST;
 
let mut bst = BST::new();
bst.insert(3);
bst.insert(2);
bst.insert(1);
 
bst.print_preorder(0);
 

Trait Implementations§

Source§

impl<T: Debug> Debug for BST<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 BST<T>

§

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

§

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

§

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

§

impl<T> Unpin for BST<T>

§

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