pub struct BinaryTree<T>{
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>
impl<T> BinaryTree<T>
Sourcepub fn delete(&mut self, del_elem: T) -> Option<T>
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);
Sourcepub fn insert(&mut self, new_elem: T)
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);
Sourcepub fn is_empty(&self) -> bool
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);
Sourcepub fn contains(&self, search_elem: T) -> bool
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);
Sourcepub fn clear(&mut self)
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);
Sourcepub fn max(&self) -> Option<&T>
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);
Sourcepub fn min(&self) -> Option<&T>
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);
Sourcepub fn height(&self) -> usize
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);
Sourcepub fn count(&self) -> usize
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);
Sourcepub fn inorder(&self)
pub fn inorder(&self)
Prints to screen the elements in inorder Basic usage:
let mut a = BinaryTree::new(1);
a.insert(2);
a.inorder();
Sourcepub fn preorder(&self)
pub fn preorder(&self)
Prints to screen the elements in preorder Basic usage:
let mut a = BinaryTree::new(1);
a.insert(2);
a.peorder();
Sourcepub fn postorder(&self)
pub fn postorder(&self)
Prints to screen the elements in postorder Basic usage:
let mut a = BinaryTree::new(1);
a.insert(2);
a.postorder();
Sourcepub fn vec_insert(&mut self, elems: Vec<T>)
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]);