WIP Iterative implementation of BST

pull/3/head
sgoudham 3 years ago
parent 6572ba88d1
commit 97e81f8620
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -110,6 +110,66 @@ impl<T: Ord + Debug> Display for RecursiveBST<T> {
}
}
impl<T: Ord> PartialEq for IterativeBST<T> {
fn eq(&self, other: &Self) -> bool {
self.sorted_vec() == other.sorted_vec()
}
}
impl<T: Ord> Extend<T> for IterativeBST<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for value in iter.into_iter() {
self.insert(value)
}
}
}
impl<T: Ord> FromIterator<T> for IterativeBST<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut bst = IterativeBST::empty();
bst.extend(iter);
bst
}
}
impl<T: Ord> From<Vec<T>> for IterativeBST<T> {
fn from(vec: Vec<T>) -> Self {
let mut bst = IterativeBST::empty();
for value in vec.into_iter() {
bst.insert(value);
}
bst
}
}
impl<T: Ord + Clone> From<&[T]> for IterativeBST<T> {
fn from(slice: &[T]) -> Self {
let mut bst = IterativeBST::empty();
for value in slice {
bst.insert((*value).clone());
}
bst
}
}
impl<T: Ord + Clone> Clone for IterativeBST<T> {
fn clone(&self) -> Self {
let mut bst = IterativeBST::empty();
for value in self.in_order_iter() {
bst.insert((*value).clone());
}
bst
}
}
impl<T: Ord + Debug> Display for IterativeBST<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.sorted_vec())
}
}
impl<T: Ord> Node<T> {
fn new(value: T) -> Node<T> {
Node {
@ -119,6 +179,21 @@ impl<T: Ord> Node<T> {
}
}
fn iterative_insert(root: &mut HeapNode<T>, value: T) -> Result<(), ()> {
let mut current = root;
while let Some(ref mut node) = current {
match value.cmp(&node.value) {
Ordering::Equal => return Err(()),
Ordering::Less => current = &mut node.left,
Ordering::Greater => current = &mut node.right,
}
}
*current = Some(Box::new(Node::new(value)));
Ok(())
}
fn recursive_insert(&mut self, value: T) -> Result<(), ()> {
match value.cmp(&self.value) {
Ordering::Equal => Err(()),
@ -238,6 +313,23 @@ impl<T: Ord> Node<T> {
}
}
fn iterative_pre_order_vec(node: &HeapNode<T>) -> Vec<&T> {
let mut elements = Vec::new();
let mut stack = vec![node.as_ref()];
while let Some(current) = stack.pop().unwrap_or(None) {
elements.push(&current.value);
if current.right.is_some() {
stack.push(current.right.as_ref());
}
if current.left.is_some() {
stack.push(current.left.as_ref());
}
}
elements
}
fn recursive_pre_order_vec<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) {
if let Some(ref node) = node {
elements.push(&node.value);
@ -246,6 +338,25 @@ impl<T: Ord> Node<T> {
}
}
fn iterative_in_order_vec(node: &HeapNode<T>) -> Vec<&T> {
let mut root = node;
let mut elements = Vec::new();
let mut stack = Vec::new();
while !stack.is_empty() || root.is_some() {
if root.is_some() {
stack.push(root);
root = &root.as_ref().unwrap().left;
} else {
let node = stack.pop().unwrap();
elements.push(&node.as_ref().unwrap().value);
root = &node.as_ref().unwrap().right;
}
}
elements
}
fn recursive_in_order_vec<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) {
if let Some(ref node) = node {
Node::recursive_in_order_vec(&node.left, elements);
@ -254,6 +365,44 @@ impl<T: Ord> Node<T> {
}
}
fn iterative_post_order_vec(node: &HeapNode<T>) -> Vec<&T> {
let mut root = node;
let mut elements = Vec::new();
let mut stack = Vec::new();
loop {
while let Some(current) = root {
if current.right.is_some() {
stack.push(current.right.as_ref().unwrap());
}
stack.push(root.as_ref().unwrap());
root = &current.left;
}
if stack.is_empty() {
break;
}
if let Some(current) = stack.pop() {
if !stack.is_empty()
&& current.right.is_some()
&& stack.last().unwrap().value == current.right.as_ref().unwrap().value
{
stack.pop();
stack.push(current);
root = &current.right;
} else {
elements.push(&current.value);
root = &None;
}
} else {
break;
}
}
elements
}
fn recursive_post_order_vec<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) {
if let Some(ref node) = node {
Node::recursive_post_order_vec(&node.left, elements);
@ -287,6 +436,155 @@ impl<T: Ord> Node<T> {
}
}
impl<T: Ord> BinarySearchTree<T> for IterativeBST<T> {
fn empty() -> IterativeBST<T> {
IterativeBST {
root: None,
size: 0,
}
}
fn new(value: T) -> IterativeBST<T> {
IterativeBST {
root: Some(Box::from(Node::new(value))),
size: 1,
}
}
fn size(&self) -> usize {
self.size
}
fn is_empty(&self) -> bool {
self.size == 0
}
fn insert(&mut self, value: T) {
if Node::iterative_insert(&mut self.root, value).is_ok() {
self.size += 1;
}
}
fn contains(&self, value: &T) -> bool {
match self.root {
None => false,
Some(ref node) => node.recursive_contains(value),
}
}
fn remove(&mut self, value: &T) {
if Node::recursive_remove(&mut self.root, value).is_ok() {
self.size -= 1;
}
}
fn retrieve(&self, value: T) -> Option<&T> {
match self.root {
None => None,
Some(ref node) => node.recursive_retrieve(value),
}
}
fn retrieve_as_mut(&mut self, value: T) -> Option<&mut T> {
match self.root {
None => None,
Some(ref mut node) => node.recursive_retrieve_as_mut(value),
}
}
fn min(&self) -> Option<&T> {
match self.root {
None => None,
Some(ref node) => node.recursive_min(),
}
}
fn max(&self) -> Option<&T> {
match self.root {
None => None,
Some(ref node) => node.recursive_max(),
}
}
fn remove_min(&mut self) -> Option<T> {
let removed_min = match self.root {
None => None,
Some(_) => Node::recursive_remove_min(&mut self.root),
};
if removed_min.is_some() {
self.size -= 1;
}
removed_min
}
fn remove_max(&mut self) -> Option<T> {
let removed_max = match self.root {
None => None,
Some(_) => Node::recursive_remove_max(&mut self.root),
};
if removed_max.is_some() {
self.size -= 1;
}
removed_max
}
fn sorted_vec(&self) -> Vec<&T> {
Node::iterative_in_order_vec(&self.root)
}
fn into_sorted_vec(self) -> Vec<T> {
let mut elements = Vec::new();
Node::recursive_consume_in_order_vec(self.root, &mut elements);
elements
}
fn pre_order_vec(&self) -> Vec<&T> {
Node::iterative_pre_order_vec(&self.root)
}
fn in_order_vec(&self) -> Vec<&T> {
Node::iterative_in_order_vec(&self.root)
}
fn post_order_vec(&self) -> Vec<&T> {
Node::iterative_post_order_vec(&self.root)
}
fn pre_order_iter(&self) -> IntoIter<&T> {
Node::iterative_pre_order_vec(&self.root).into_iter()
}
fn in_order_iter(&self) -> IntoIter<&T> {
Node::iterative_in_order_vec(&self.root).into_iter()
}
fn post_order_iter(&self) -> IntoIter<&T> {
Node::iterative_post_order_vec(&self.root).into_iter()
}
fn into_pre_order_iter(self) -> IntoIter<T> {
let mut elements = Vec::new();
Node::recursive_consume_pre_order_vec(self.root, &mut elements);
elements.into_iter()
}
fn into_in_order_iter(self) -> IntoIter<T> {
let mut elements = Vec::new();
Node::recursive_consume_in_order_vec(self.root, &mut elements);
elements.into_iter()
}
fn into_post_order_iter(self) -> IntoIter<T> {
let mut elements = Vec::new();
Node::recursive_consume_post_order_vec(self.root, &mut elements);
elements.into_iter()
}
}
impl<T: Ord> BinarySearchTree<T> for RecursiveBST<T> {
fn empty() -> RecursiveBST<T> {
RecursiveBST {

@ -0,0 +1,572 @@
use bst_rs::{BinarySearchTree, IterativeBST};
#[test]
fn can_insert_element() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(-1);
expected_bst.insert(0);
expected_bst.insert(1);
expected_bst.insert(2);
expected_bst.insert(-20);
let mut actual_bst = IterativeBST::new(-1);
actual_bst.insert(0);
actual_bst.insert(1);
actual_bst.insert(1);
actual_bst.insert(2);
actual_bst.insert(-20);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn check_if_bst_is_empty() {
let mut bst = IterativeBST::empty();
assert!(bst.is_empty());
bst.insert(1);
assert!(!bst.is_empty());
}
#[test]
fn check_element_exists() {
let mut bst = IterativeBST::empty();
bst.insert(1);
bst.insert(5);
assert!(!bst.contains(&10));
assert!(bst.contains(&1));
assert!(bst.contains(&5));
}
#[test]
fn remove_root_element() {
let mut bst = IterativeBST::empty();
bst.insert(0);
assert!(!bst.is_empty());
assert_eq!(bst.size(), 1);
bst.remove(&0);
assert!(bst.is_empty());
assert_eq!(bst.size(), 0)
}
#[test]
fn remove_leaf_node() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(5);
expected_bst.insert(4);
expected_bst.insert(6);
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(5);
actual_bst.insert(4);
actual_bst.insert(6);
actual_bst.insert(7);
actual_bst.remove(&7);
assert_eq!(actual_bst.size(), 3);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn remove_single_right_node_with_children() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(5);
expected_bst.insert(4);
expected_bst.insert(7);
expected_bst.insert(8);
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(5);
actual_bst.insert(4);
actual_bst.insert(6);
actual_bst.insert(7);
actual_bst.insert(8);
actual_bst.remove(&6);
assert_eq!(actual_bst.size(), 4);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn remove_single_left_node_with_children() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(5);
expected_bst.insert(3);
expected_bst.insert(2);
expected_bst.insert(6);
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(5);
actual_bst.insert(4);
actual_bst.insert(6);
actual_bst.insert(3);
actual_bst.insert(2);
actual_bst.remove(&4);
assert_eq!(actual_bst.size(), 4);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn remove_node_with_two_children() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(10);
expected_bst.insert(3);
expected_bst.insert(8);
expected_bst.insert(15);
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(10);
actual_bst.insert(5);
actual_bst.insert(8);
actual_bst.insert(3);
actual_bst.insert(15);
actual_bst.remove(&5);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn does_not_fail_when_removing_non_existing_element() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(10);
expected_bst.insert(5);
expected_bst.insert(8);
expected_bst.insert(3);
expected_bst.insert(15);
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(10);
actual_bst.insert(5);
actual_bst.insert(8);
actual_bst.insert(3);
actual_bst.insert(15);
actual_bst.remove(&20);
assert_eq!(actual_bst.size(), 5);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn retrieve_element() {
let mut bst = IterativeBST::empty();
bst.insert(5);
bst.insert(10);
let retrieved_value = bst.retrieve(5);
let invalid_value = bst.retrieve(15);
assert_eq!(retrieved_value, Some(&5));
assert_eq!(invalid_value, None);
}
#[test]
fn retrieve_element_as_mut_and_modify_bst() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(10);
expected_bst.insert(2);
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(10);
actual_bst.insert(5);
let _retrieved_value_as_mut: &mut i32 = actual_bst.retrieve_as_mut(5).unwrap();
*_retrieved_value_as_mut = 2;
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn get_min_from_bst() {
let mut bst = IterativeBST::empty();
assert_eq!(bst.min(), None);
bst.insert(5);
bst.insert(3);
bst.insert(1);
bst.insert(15);
assert_eq!(bst.min(), Some(&1));
}
#[test]
fn get_max_from_bst() {
let mut bst = IterativeBST::empty();
assert_eq!(bst.max(), None);
bst.insert(5);
bst.insert(12);
bst.insert(1);
bst.insert(15);
assert_eq!(bst.max(), Some(&15));
}
#[test]
fn remove_min_from_bst() {
let mut bst = IterativeBST::empty();
assert_eq!(bst.remove_min(), None);
bst.insert(5);
assert_eq!(bst.remove_min(), Some(5));
assert_eq!(bst.size(), 0);
bst.insert(3);
bst.insert(1);
bst.insert(2);
bst.insert(15);
assert_eq!(bst.remove_min(), Some(1));
assert!(bst.contains(&2));
assert_eq!(bst.size(), 3);
}
#[test]
fn remove_max_from_bst() {
let mut bst = IterativeBST::empty();
assert_eq!(bst.remove_max(), None);
bst.insert(5);
assert_eq!(bst.remove_max(), Some(5));
assert_eq!(bst.size(), 0);
bst.insert(3);
bst.insert(1);
bst.insert(15);
bst.insert(10);
assert_eq!(bst.remove_max(), Some(15));
assert!(bst.contains(&10));
assert_eq!(bst.size(), 3);
}
#[test]
fn pre_order_iter() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
let mut pre_order_iter = bst.pre_order_iter();
assert_eq!(pre_order_iter.next(), Some(&3));
assert_eq!(pre_order_iter.next(), Some(&1));
assert_eq!(pre_order_iter.next(), Some(&2));
assert_eq!(pre_order_iter.next(), Some(&4));
assert_eq!(pre_order_iter.next(), Some(&5));
assert_eq!(pre_order_iter.next(), None);
bst.insert(10);
let mut another_pre_order_iter = bst.pre_order_iter();
assert_eq!(another_pre_order_iter.next(), Some(&3));
assert_eq!(another_pre_order_iter.next(), Some(&1));
assert_eq!(another_pre_order_iter.next(), Some(&2));
assert_eq!(another_pre_order_iter.next(), Some(&4));
assert_eq!(another_pre_order_iter.next(), Some(&5));
assert_eq!(another_pre_order_iter.next(), Some(&10));
assert_eq!(another_pre_order_iter.next(), None);
}
#[test]
fn in_order_iter() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
let mut in_order_iter = bst.in_order_iter();
assert_eq!(in_order_iter.next(), Some(&1));
assert_eq!(in_order_iter.next(), Some(&2));
assert_eq!(in_order_iter.next(), Some(&3));
assert_eq!(in_order_iter.next(), Some(&4));
assert_eq!(in_order_iter.next(), Some(&5));
assert_eq!(in_order_iter.next(), None);
bst.insert(6);
let mut another_in_order_iter = bst.in_order_iter();
assert_eq!(another_in_order_iter.next(), Some(&1));
assert_eq!(another_in_order_iter.next(), Some(&2));
assert_eq!(another_in_order_iter.next(), Some(&3));
assert_eq!(another_in_order_iter.next(), Some(&4));
assert_eq!(another_in_order_iter.next(), Some(&5));
assert_eq!(another_in_order_iter.next(), Some(&6));
assert_eq!(another_in_order_iter.next(), None);
}
#[test]
fn post_order_iter() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
let mut post_order_iter = bst.post_order_iter();
println!("{:?}", bst);
assert_eq!(post_order_iter.next(), Some(&2));
assert_eq!(post_order_iter.next(), Some(&1));
assert_eq!(post_order_iter.next(), Some(&5));
assert_eq!(post_order_iter.next(), Some(&4));
assert_eq!(post_order_iter.next(), Some(&3));
assert_eq!(post_order_iter.next(), None);
bst.insert(10);
let mut another_post_order_iter = bst.post_order_iter();
assert_eq!(another_post_order_iter.next(), Some(&2));
assert_eq!(another_post_order_iter.next(), Some(&1));
assert_eq!(another_post_order_iter.next(), Some(&10));
assert_eq!(another_post_order_iter.next(), Some(&5));
assert_eq!(another_post_order_iter.next(), Some(&4));
assert_eq!(another_post_order_iter.next(), Some(&3));
assert_eq!(another_post_order_iter.next(), None);
}
#[test]
fn into_pre_order_iter() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
let mut pre_order_iter = bst.into_pre_order_iter();
assert_eq!(pre_order_iter.next(), Some(3));
assert_eq!(pre_order_iter.next(), Some(1));
assert_eq!(pre_order_iter.next(), Some(2));
assert_eq!(pre_order_iter.next(), Some(4));
assert_eq!(pre_order_iter.next(), Some(5));
assert_eq!(pre_order_iter.next(), None);
}
#[test]
fn into_in_order_iter() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
let mut in_order_iter = bst.into_in_order_iter();
assert_eq!(in_order_iter.next(), Some(1));
assert_eq!(in_order_iter.next(), Some(2));
assert_eq!(in_order_iter.next(), Some(3));
assert_eq!(in_order_iter.next(), Some(4));
assert_eq!(in_order_iter.next(), Some(5));
assert_eq!(in_order_iter.next(), None);
}
#[test]
fn into_post_order_iter() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
let mut post_order_traversal = bst.into_post_order_iter();
assert_eq!(post_order_traversal.next(), Some(2));
assert_eq!(post_order_traversal.next(), Some(1));
assert_eq!(post_order_traversal.next(), Some(5));
assert_eq!(post_order_traversal.next(), Some(4));
assert_eq!(post_order_traversal.next(), Some(3));
assert_eq!(post_order_traversal.next(), None);
}
#[test]
fn get_sorted_vec() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
assert_eq!(bst.sorted_vec(), vec![&1, &2, &3, &4, &5]);
}
#[test]
fn bst_into_sorted_vec() {
let mut bst = IterativeBST::empty();
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
assert_eq!(bst.into_sorted_vec(), vec![1, 2, 3, 4, 5]);
}
#[test]
fn get_pre_order_vec() {
let mut bst = IterativeBST::empty();
assert!(bst.pre_order_vec().is_empty());
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
assert_eq!(bst.pre_order_vec(), vec![&3, &1, &2, &4, &5]);
}
#[test]
fn get_in_order_vec() {
let mut bst = IterativeBST::empty();
assert!(bst.in_order_vec().is_empty());
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
assert_eq!(bst.in_order_vec(), vec![&1, &2, &3, &4, &5]);
}
#[test]
fn get_post_order_vec() {
let mut bst = IterativeBST::empty();
assert!(bst.post_order_vec().is_empty());
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(1);
bst.insert(2);
assert_eq!(bst.post_order_vec(), vec![&2, &1, &5, &4, &3]);
}
#[test]
fn create_bst_from_vec() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(10);
expected_bst.insert(20);
expected_bst.insert(5);
expected_bst.insert(30);
let actual_bst = IterativeBST::from(vec![10, 20, 5, 30]);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn create_bst_from_slice() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(10);
expected_bst.insert(20);
expected_bst.insert(5);
expected_bst.insert(30);
let actual_bst = IterativeBST::from(vec![10, 20, 5, 30].as_slice());
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn create_bst_from_into_vec() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(10);
expected_bst.insert(20);
expected_bst.insert(5);
expected_bst.insert(30);
let actual_bst: IterativeBST<i32> = vec![10, 20, 5, 30].into();
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn extend_bst_from_iter() {
let vec = vec![8, 1, 10];
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(3);
expected_bst.insert(2);
expected_bst.insert(5);
expected_bst.insert(8);
expected_bst.insert(1);
expected_bst.insert(10);
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(3);
actual_bst.insert(2);
actual_bst.insert(5);
actual_bst.extend(vec.into_iter());
assert_eq!(actual_bst.size(), 6);
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn create_bst_from_iter() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(3);
expected_bst.insert(2);
expected_bst.insert(5);
expected_bst.insert(8);
expected_bst.insert(1);
expected_bst.insert(10);
let actual_bst = IterativeBST::from_iter(vec![3, 2, 5, 8, 1, 10].into_iter());
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn clone_bst() {
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(3);
expected_bst.insert(2);
expected_bst.insert(5);
expected_bst.insert(8);
expected_bst.insert(1);
expected_bst.insert(10);
let cloned_bst = expected_bst.clone();
assert_eq!(cloned_bst, expected_bst);
}
#[test]
fn clone_into_another_bst() {
let mut actual_bst = IterativeBST::empty();
actual_bst.insert(3);
actual_bst.insert(2);
let mut expected_bst = IterativeBST::empty();
expected_bst.insert(3);
expected_bst.insert(2);
expected_bst.insert(5);
expected_bst.insert(8);
expected_bst.insert(1);
expected_bst.insert(10);
assert_ne!(actual_bst, expected_bst);
actual_bst.clone_from(&expected_bst);
assert_eq!(actual_bst, expected_bst);
}
Loading…
Cancel
Save