Rename empty() to new()

pull/3/head
sgoudham 3 years ago
parent bac301ad3a
commit 81aafcad77
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -3,8 +3,7 @@ use std::fmt::{Debug, Display, Formatter};
use std::vec::IntoIter; use std::vec::IntoIter;
pub trait BinarySearchTree<T: Ord> { pub trait BinarySearchTree<T: Ord> {
fn empty() -> Self; fn new() -> Self;
fn new(value: T) -> Self;
fn size(&self) -> usize; fn size(&self) -> usize;
fn is_empty(&self) -> bool; fn is_empty(&self) -> bool;
fn insert(&mut self, value: T); fn insert(&mut self, value: T);
@ -66,7 +65,7 @@ impl<T: Ord> Extend<T> for RecursiveBST<T> {
impl<T: Ord> FromIterator<T> for RecursiveBST<T> { impl<T: Ord> FromIterator<T> for RecursiveBST<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.extend(iter); bst.extend(iter);
bst bst
} }
@ -74,7 +73,7 @@ impl<T: Ord> FromIterator<T> for RecursiveBST<T> {
impl<T: Ord> From<Vec<T>> for RecursiveBST<T> { impl<T: Ord> From<Vec<T>> for RecursiveBST<T> {
fn from(vec: Vec<T>) -> Self { fn from(vec: Vec<T>) -> Self {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
for value in vec.into_iter() { for value in vec.into_iter() {
bst.insert(value); bst.insert(value);
} }
@ -84,7 +83,7 @@ impl<T: Ord> From<Vec<T>> for RecursiveBST<T> {
impl<T: Ord + Clone> From<&[T]> for RecursiveBST<T> { impl<T: Ord + Clone> From<&[T]> for RecursiveBST<T> {
fn from(slice: &[T]) -> Self { fn from(slice: &[T]) -> Self {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
for value in slice { for value in slice {
bst.insert((*value).clone()); bst.insert((*value).clone());
} }
@ -94,7 +93,7 @@ impl<T: Ord + Clone> From<&[T]> for RecursiveBST<T> {
impl<T: Ord + Clone> Clone for RecursiveBST<T> { impl<T: Ord + Clone> Clone for RecursiveBST<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
for value in self.in_order_iter() { for value in self.in_order_iter() {
bst.insert((*value).clone()); bst.insert((*value).clone());
@ -126,7 +125,7 @@ impl<T: Ord> Extend<T> for IterativeBST<T> {
impl<T: Ord> FromIterator<T> for IterativeBST<T> { impl<T: Ord> FromIterator<T> for IterativeBST<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.extend(iter); bst.extend(iter);
bst bst
} }
@ -134,7 +133,7 @@ impl<T: Ord> FromIterator<T> for IterativeBST<T> {
impl<T: Ord> From<Vec<T>> for IterativeBST<T> { impl<T: Ord> From<Vec<T>> for IterativeBST<T> {
fn from(vec: Vec<T>) -> Self { fn from(vec: Vec<T>) -> Self {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
for value in vec.into_iter() { for value in vec.into_iter() {
bst.insert(value); bst.insert(value);
} }
@ -144,7 +143,7 @@ impl<T: Ord> From<Vec<T>> for IterativeBST<T> {
impl<T: Ord + Clone> From<&[T]> for IterativeBST<T> { impl<T: Ord + Clone> From<&[T]> for IterativeBST<T> {
fn from(slice: &[T]) -> Self { fn from(slice: &[T]) -> Self {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
for value in slice { for value in slice {
bst.insert((*value).clone()); bst.insert((*value).clone());
} }
@ -154,7 +153,7 @@ impl<T: Ord + Clone> From<&[T]> for IterativeBST<T> {
impl<T: Ord + Clone> Clone for IterativeBST<T> { impl<T: Ord + Clone> Clone for IterativeBST<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
for value in self.in_order_iter() { for value in self.in_order_iter() {
bst.insert((*value).clone()); bst.insert((*value).clone());
@ -548,21 +547,22 @@ impl<T: Ord> Node<T> {
} }
fn iterative_consume_in_order_vec(mut root: HeapNode<T>) -> Vec<T> { fn iterative_consume_in_order_vec(mut root: HeapNode<T>) -> Vec<T> {
let mut elements = Vec::new(); // let mut elements = Vec::new();
let mut stack = Vec::new(); // let mut stack = Vec::new();
//
while !stack.is_empty() || root.is_some() { // while !stack.is_empty() || root.is_some() {
if root.is_some() { // if root.is_some() {
stack.push(root.as_ref().unwrap()); // stack.push(root.as_ref().unwrap());
root = root.unwrap().left; // root = root.unwrap().left;
} else { // } else {
let node = stack.pop(); // let node = stack.pop();
elements.push(node.as_ref().unwrap().value); // elements.push(node.as_ref().unwrap().value);
root = node.as_ref().unwrap().right; // root = node.as_ref().unwrap().right;
} // }
} // }
//
elements // elements
unimplemented!()
} }
fn recursive_consume_in_order_vec(node: HeapNode<T>, elements: &mut Vec<T>) { fn recursive_consume_in_order_vec(node: HeapNode<T>, elements: &mut Vec<T>) {
@ -583,20 +583,13 @@ impl<T: Ord> Node<T> {
} }
impl<T: Ord> BinarySearchTree<T> for IterativeBST<T> { impl<T: Ord> BinarySearchTree<T> for IterativeBST<T> {
fn empty() -> IterativeBST<T> { fn new() -> IterativeBST<T> {
IterativeBST { IterativeBST {
root: None, root: None,
size: 0, size: 0,
} }
} }
fn new(value: T) -> IterativeBST<T> {
IterativeBST {
root: Some(Box::from(Node::new(value))),
size: 1,
}
}
fn size(&self) -> usize { fn size(&self) -> usize {
self.size self.size
} }
@ -705,20 +698,13 @@ impl<T: Ord> BinarySearchTree<T> for IterativeBST<T> {
} }
impl<T: Ord> BinarySearchTree<T> for RecursiveBST<T> { impl<T: Ord> BinarySearchTree<T> for RecursiveBST<T> {
fn empty() -> RecursiveBST<T> { fn new() -> RecursiveBST<T> {
RecursiveBST { RecursiveBST {
root: None, root: None,
size: 0, size: 0,
} }
} }
fn new(value: T) -> RecursiveBST<T> {
RecursiveBST {
root: Some(Box::from(Node::new(value))),
size: 1,
}
}
fn size(&self) -> usize { fn size(&self) -> usize {
self.size self.size
} }

@ -4,14 +4,13 @@ use bst_rs::{BinarySearchTree, IterativeBST};
#[test] #[test]
fn successfully_insert_elements_into_bst() { fn successfully_insert_elements_into_bst() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(-1);
expected_bst.insert(0); expected_bst.insert(0);
expected_bst.insert(1); expected_bst.insert(1);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(-20); expected_bst.insert(-20);
let mut actual_bst = IterativeBST::new(-1); let mut actual_bst = IterativeBST::new();
actual_bst.insert(0); actual_bst.insert(0);
actual_bst.insert(1); actual_bst.insert(1);
actual_bst.insert(1); actual_bst.insert(1);
@ -19,12 +18,12 @@ fn successfully_insert_elements_into_bst() {
actual_bst.insert(-20); actual_bst.insert(-20);
assert_eq!(actual_bst, expected_bst); assert_eq!(actual_bst, expected_bst);
assert_eq!(actual_bst.size(), 5); assert_eq!(actual_bst.size(), 4);
} }
#[test] #[test]
fn check_if_bst_is_empty() { fn check_if_bst_is_empty() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert!(bst.is_empty()); assert!(bst.is_empty());
bst.insert(1); bst.insert(1);
@ -33,7 +32,7 @@ fn check_if_bst_is_empty() {
#[test] #[test]
fn check_if_bst_contains_elements() { fn check_if_bst_contains_elements() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert!(!bst.contains(&10)); assert!(!bst.contains(&10));
bst.insert(1); bst.insert(1);
@ -46,7 +45,7 @@ fn check_if_bst_contains_elements() {
#[test] #[test]
fn successfully_remove_root_node_from_bst() { fn successfully_remove_root_node_from_bst() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(0); bst.insert(0);
bst.remove(&0); bst.remove(&0);
@ -57,11 +56,11 @@ fn successfully_remove_root_node_from_bst() {
#[test] #[test]
fn successfully_remove_leaf_node() { fn successfully_remove_leaf_node() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(4); expected_bst.insert(4);
expected_bst.insert(6); expected_bst.insert(6);
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(4); actual_bst.insert(4);
actual_bst.insert(6); actual_bst.insert(6);
@ -75,12 +74,12 @@ fn successfully_remove_leaf_node() {
#[test] #[test]
fn remove_single_right_node_with_children() { fn remove_single_right_node_with_children() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(4); expected_bst.insert(4);
expected_bst.insert(7); expected_bst.insert(7);
expected_bst.insert(8); expected_bst.insert(8);
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(4); actual_bst.insert(4);
actual_bst.insert(6); actual_bst.insert(6);
@ -96,12 +95,12 @@ fn remove_single_right_node_with_children() {
#[test] #[test]
fn remove_single_left_node_with_children() { fn remove_single_left_node_with_children() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(6); expected_bst.insert(6);
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(4); actual_bst.insert(4);
actual_bst.insert(6); actual_bst.insert(6);
@ -116,12 +115,12 @@ fn remove_single_left_node_with_children() {
#[test] #[test]
fn remove_node_with_two_children() { fn remove_node_with_two_children() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(8); expected_bst.insert(8);
expected_bst.insert(15); expected_bst.insert(15);
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(10); actual_bst.insert(10);
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(8); actual_bst.insert(8);
@ -135,14 +134,14 @@ fn remove_node_with_two_children() {
#[test] #[test]
fn does_not_fail_when_removing_non_existing_element() { fn does_not_fail_when_removing_non_existing_element() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(8); expected_bst.insert(8);
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(15); expected_bst.insert(15);
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(10); actual_bst.insert(10);
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(8); actual_bst.insert(8);
@ -157,7 +156,7 @@ fn does_not_fail_when_removing_non_existing_element() {
#[test] #[test]
fn retrieve_element() { fn retrieve_element() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(5); bst.insert(5);
bst.insert(10); bst.insert(10);
@ -170,11 +169,11 @@ fn retrieve_element() {
#[test] #[test]
fn retrieve_element_as_mut_and_modify_bst() { fn retrieve_element_as_mut_and_modify_bst() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(2); expected_bst.insert(2);
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(10); actual_bst.insert(10);
actual_bst.insert(5); actual_bst.insert(5);
@ -186,7 +185,7 @@ fn retrieve_element_as_mut_and_modify_bst() {
#[test] #[test]
fn get_min_from_bst() { fn get_min_from_bst() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert_eq!(bst.min(), None); assert_eq!(bst.min(), None);
bst.insert(5); bst.insert(5);
@ -199,7 +198,7 @@ fn get_min_from_bst() {
#[test] #[test]
fn get_max_from_bst() { fn get_max_from_bst() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert_eq!(bst.max(), None); assert_eq!(bst.max(), None);
bst.insert(5); bst.insert(5);
@ -212,7 +211,7 @@ fn get_max_from_bst() {
#[test] #[test]
fn remove_min_from_bst() { fn remove_min_from_bst() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert_eq!(bst.remove_min(), None); assert_eq!(bst.remove_min(), None);
bst.insert(5); bst.insert(5);
@ -231,7 +230,7 @@ fn remove_min_from_bst() {
#[test] #[test]
fn remove_max_from_bst() { fn remove_max_from_bst() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert_eq!(bst.remove_max(), None); assert_eq!(bst.remove_max(), None);
bst.insert(5); bst.insert(5);
@ -250,7 +249,7 @@ fn remove_max_from_bst() {
#[test] #[test]
fn pre_order_iter() { fn pre_order_iter() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -281,7 +280,7 @@ fn pre_order_iter() {
#[test] #[test]
fn in_order_iter() { fn in_order_iter() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -312,7 +311,7 @@ fn in_order_iter() {
#[test] #[test]
fn post_order_iter() { fn post_order_iter() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -344,10 +343,10 @@ fn post_order_iter() {
#[test] #[test]
fn into_pre_order_iter() { fn into_pre_order_iter() {
let mut iter: IntoIter<i32> = IterativeBST::empty().into_pre_order_iter(); let mut iter: IntoIter<i32> = IterativeBST::new().into_pre_order_iter();
assert_eq!(iter.next(), None); assert_eq!(iter.next(), None);
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -366,7 +365,7 @@ fn into_pre_order_iter() {
#[test] #[test]
fn into_in_order_iter() { fn into_in_order_iter() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -385,7 +384,7 @@ fn into_in_order_iter() {
#[test] #[test]
fn into_post_order_iter() { fn into_post_order_iter() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -404,7 +403,7 @@ fn into_post_order_iter() {
#[test] #[test]
fn get_sorted_vec() { fn get_sorted_vec() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -416,7 +415,7 @@ fn get_sorted_vec() {
#[test] #[test]
fn bst_into_sorted_vec() { fn bst_into_sorted_vec() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -428,7 +427,7 @@ fn bst_into_sorted_vec() {
#[test] #[test]
fn get_pre_order_vec() { fn get_pre_order_vec() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert!(bst.pre_order_vec().is_empty()); assert!(bst.pre_order_vec().is_empty());
bst.insert(3); bst.insert(3);
@ -442,7 +441,7 @@ fn get_pre_order_vec() {
#[test] #[test]
fn get_in_order_vec() { fn get_in_order_vec() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert!(bst.in_order_vec().is_empty()); assert!(bst.in_order_vec().is_empty());
bst.insert(3); bst.insert(3);
@ -456,7 +455,7 @@ fn get_in_order_vec() {
#[test] #[test]
fn get_post_order_vec() { fn get_post_order_vec() {
let mut bst = IterativeBST::empty(); let mut bst = IterativeBST::new();
assert!(bst.post_order_vec().is_empty()); assert!(bst.post_order_vec().is_empty());
bst.insert(3); bst.insert(3);
@ -469,7 +468,7 @@ fn get_post_order_vec() {
#[test] #[test]
fn create_bst_from_vec() { fn create_bst_from_vec() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(20); expected_bst.insert(20);
expected_bst.insert(5); expected_bst.insert(5);
@ -482,7 +481,7 @@ fn create_bst_from_vec() {
#[test] #[test]
fn create_bst_from_slice() { fn create_bst_from_slice() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(20); expected_bst.insert(20);
expected_bst.insert(5); expected_bst.insert(5);
@ -495,7 +494,7 @@ fn create_bst_from_slice() {
#[test] #[test]
fn create_bst_from_into_vec() { fn create_bst_from_into_vec() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(20); expected_bst.insert(20);
expected_bst.insert(5); expected_bst.insert(5);
@ -509,14 +508,14 @@ fn create_bst_from_into_vec() {
#[test] #[test]
fn extend_bst_from_iter() { fn extend_bst_from_iter() {
let vec = vec![8, 1, 10]; let vec = vec![8, 1, 10];
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(8); expected_bst.insert(8);
expected_bst.insert(1); expected_bst.insert(1);
expected_bst.insert(10); expected_bst.insert(10);
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(3); actual_bst.insert(3);
actual_bst.insert(2); actual_bst.insert(2);
actual_bst.insert(5); actual_bst.insert(5);
@ -529,7 +528,7 @@ fn extend_bst_from_iter() {
#[test] #[test]
fn create_bst_from_iter() { fn create_bst_from_iter() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);
@ -544,7 +543,7 @@ fn create_bst_from_iter() {
#[test] #[test]
fn clone_bst() { fn clone_bst() {
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);
@ -559,10 +558,10 @@ fn clone_bst() {
#[test] #[test]
fn clone_into_another_bst() { fn clone_into_another_bst() {
let mut actual_bst = IterativeBST::empty(); let mut actual_bst = IterativeBST::new();
actual_bst.insert(3); actual_bst.insert(3);
actual_bst.insert(2); actual_bst.insert(2);
let mut expected_bst = IterativeBST::empty(); let mut expected_bst = IterativeBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);

@ -2,8 +2,9 @@ use bst_rs::{BinarySearchTree, RecursiveBST};
#[test] #[test]
fn can_insert_element() { fn can_insert_element() {
let mut bst = RecursiveBST::new(-1); let mut bst = RecursiveBST::new();
bst.insert(-1);
bst.insert(0); bst.insert(0);
bst.insert(1); bst.insert(1);
bst.insert(1); bst.insert(1);
@ -14,7 +15,7 @@ fn can_insert_element() {
#[test] #[test]
fn check_if_bst_is_empty() { fn check_if_bst_is_empty() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
assert!(bst.is_empty()); assert!(bst.is_empty());
bst.insert(1); bst.insert(1);
@ -23,7 +24,7 @@ fn check_if_bst_is_empty() {
#[test] #[test]
fn check_element_exists() { fn check_element_exists() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(1); bst.insert(1);
bst.insert(5); bst.insert(5);
@ -35,7 +36,7 @@ fn check_element_exists() {
#[test] #[test]
fn remove_root_element() { fn remove_root_element() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(0); bst.insert(0);
assert!(!bst.is_empty()); assert!(!bst.is_empty());
@ -49,11 +50,11 @@ fn remove_root_element() {
#[test] #[test]
fn remove_leaf_node() { fn remove_leaf_node() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(4); expected_bst.insert(4);
expected_bst.insert(6); expected_bst.insert(6);
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(4); actual_bst.insert(4);
actual_bst.insert(6); actual_bst.insert(6);
@ -67,12 +68,12 @@ fn remove_leaf_node() {
#[test] #[test]
fn remove_single_right_node_with_children() { fn remove_single_right_node_with_children() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(4); expected_bst.insert(4);
expected_bst.insert(7); expected_bst.insert(7);
expected_bst.insert(8); expected_bst.insert(8);
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(4); actual_bst.insert(4);
actual_bst.insert(6); actual_bst.insert(6);
@ -87,12 +88,12 @@ fn remove_single_right_node_with_children() {
#[test] #[test]
fn remove_single_left_node_with_children() { fn remove_single_left_node_with_children() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(6); expected_bst.insert(6);
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(4); actual_bst.insert(4);
actual_bst.insert(6); actual_bst.insert(6);
@ -107,12 +108,12 @@ fn remove_single_left_node_with_children() {
#[test] #[test]
fn remove_node_with_two_children() { fn remove_node_with_two_children() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(8); expected_bst.insert(8);
expected_bst.insert(15); expected_bst.insert(15);
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(10); actual_bst.insert(10);
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(8); actual_bst.insert(8);
@ -126,14 +127,14 @@ fn remove_node_with_two_children() {
#[test] #[test]
fn does_not_fail_when_removing_non_existing_element() { fn does_not_fail_when_removing_non_existing_element() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(8); expected_bst.insert(8);
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(15); expected_bst.insert(15);
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(10); actual_bst.insert(10);
actual_bst.insert(5); actual_bst.insert(5);
actual_bst.insert(8); actual_bst.insert(8);
@ -148,7 +149,7 @@ fn does_not_fail_when_removing_non_existing_element() {
#[test] #[test]
fn retrieve_element() { fn retrieve_element() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(5); bst.insert(5);
bst.insert(10); bst.insert(10);
@ -161,11 +162,11 @@ fn retrieve_element() {
#[test] #[test]
fn retrieve_element_as_mut_and_modify_bst() { fn retrieve_element_as_mut_and_modify_bst() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(2); expected_bst.insert(2);
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(10); actual_bst.insert(10);
actual_bst.insert(5); actual_bst.insert(5);
@ -177,7 +178,7 @@ fn retrieve_element_as_mut_and_modify_bst() {
#[test] #[test]
fn get_min_from_bst() { fn get_min_from_bst() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
assert_eq!(bst.min(), None); assert_eq!(bst.min(), None);
bst.insert(5); bst.insert(5);
@ -190,7 +191,7 @@ fn get_min_from_bst() {
#[test] #[test]
fn get_max_from_bst() { fn get_max_from_bst() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
assert_eq!(bst.max(), None); assert_eq!(bst.max(), None);
bst.insert(5); bst.insert(5);
@ -203,7 +204,7 @@ fn get_max_from_bst() {
#[test] #[test]
fn remove_min_from_bst() { fn remove_min_from_bst() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
assert_eq!(bst.remove_min(), None); assert_eq!(bst.remove_min(), None);
bst.insert(5); bst.insert(5);
@ -222,7 +223,7 @@ fn remove_min_from_bst() {
#[test] #[test]
fn remove_max_from_bst() { fn remove_max_from_bst() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
assert_eq!(bst.remove_max(), None); assert_eq!(bst.remove_max(), None);
bst.insert(5); bst.insert(5);
@ -241,7 +242,7 @@ fn remove_max_from_bst() {
#[test] #[test]
fn pre_order_iter() { fn pre_order_iter() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -272,7 +273,7 @@ fn pre_order_iter() {
#[test] #[test]
fn in_order_iter() { fn in_order_iter() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -303,7 +304,7 @@ fn in_order_iter() {
#[test] #[test]
fn post_order_iter() { fn post_order_iter() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -335,7 +336,7 @@ fn post_order_iter() {
#[test] #[test]
fn into_pre_order_iter() { fn into_pre_order_iter() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -354,7 +355,7 @@ fn into_pre_order_iter() {
#[test] #[test]
fn into_in_order_iter() { fn into_in_order_iter() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -373,7 +374,7 @@ fn into_in_order_iter() {
#[test] #[test]
fn into_post_order_iter() { fn into_post_order_iter() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -392,7 +393,7 @@ fn into_post_order_iter() {
#[test] #[test]
fn get_sorted_vec() { fn get_sorted_vec() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -404,7 +405,7 @@ fn get_sorted_vec() {
#[test] #[test]
fn bst_into_sorted_vec() { fn bst_into_sorted_vec() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -416,7 +417,7 @@ fn bst_into_sorted_vec() {
#[test] #[test]
fn get_pre_order_vec() { fn get_pre_order_vec() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -428,7 +429,7 @@ fn get_pre_order_vec() {
#[test] #[test]
fn get_in_order_vec() { fn get_in_order_vec() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -440,7 +441,7 @@ fn get_in_order_vec() {
#[test] #[test]
fn get_post_order_vec() { fn get_post_order_vec() {
let mut bst = RecursiveBST::empty(); let mut bst = RecursiveBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
bst.insert(5); bst.insert(5);
@ -452,7 +453,7 @@ fn get_post_order_vec() {
#[test] #[test]
fn create_bst_from_vec() { fn create_bst_from_vec() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(20); expected_bst.insert(20);
expected_bst.insert(5); expected_bst.insert(5);
@ -465,7 +466,7 @@ fn create_bst_from_vec() {
#[test] #[test]
fn create_bst_from_slice() { fn create_bst_from_slice() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(20); expected_bst.insert(20);
expected_bst.insert(5); expected_bst.insert(5);
@ -478,7 +479,7 @@ fn create_bst_from_slice() {
#[test] #[test]
fn create_bst_from_into_vec() { fn create_bst_from_into_vec() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(10); expected_bst.insert(10);
expected_bst.insert(20); expected_bst.insert(20);
expected_bst.insert(5); expected_bst.insert(5);
@ -492,14 +493,14 @@ fn create_bst_from_into_vec() {
#[test] #[test]
fn extend_bst_from_iter() { fn extend_bst_from_iter() {
let vec = vec![8, 1, 10]; let vec = vec![8, 1, 10];
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);
expected_bst.insert(8); expected_bst.insert(8);
expected_bst.insert(1); expected_bst.insert(1);
expected_bst.insert(10); expected_bst.insert(10);
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(3); actual_bst.insert(3);
actual_bst.insert(2); actual_bst.insert(2);
actual_bst.insert(5); actual_bst.insert(5);
@ -512,7 +513,7 @@ fn extend_bst_from_iter() {
#[test] #[test]
fn create_bst_from_iter() { fn create_bst_from_iter() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);
@ -527,7 +528,7 @@ fn create_bst_from_iter() {
#[test] #[test]
fn clone_bst() { fn clone_bst() {
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);
@ -542,10 +543,10 @@ fn clone_bst() {
#[test] #[test]
fn clone_into_another_bst() { fn clone_into_another_bst() {
let mut actual_bst = RecursiveBST::empty(); let mut actual_bst = RecursiveBST::new();
actual_bst.insert(3); actual_bst.insert(3);
actual_bst.insert(2); actual_bst.insert(2);
let mut expected_bst = RecursiveBST::empty(); let mut expected_bst = RecursiveBST::new();
expected_bst.insert(3); expected_bst.insert(3);
expected_bst.insert(2); expected_bst.insert(2);
expected_bst.insert(5); expected_bst.insert(5);

Loading…
Cancel
Save