From 9699802a8fa3f985672398514853a430416b2334 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sun, 20 Feb 2022 20:26:54 +0000 Subject: [PATCH] WIP iterative_consume_post_order_vec() + add some docs --- src/lib.rs | 105 +++++++++++++++++------------------------ tests/iterative_bst.rs | 96 +++++++++++++++++++++++++++++-------- 2 files changed, 119 insertions(+), 82 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5545650..8dbed24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,16 @@ +//! This crate contains Recursive & Iterative Binary Search Tree Implementations. All common +//! operations are included along with +//! +//! It is important to note that [RecursiveBST] is more likely to `blow the stack.` +//! For more information on why that is the case, please read [The Story of Tail Call Optimizations in Rust.](https://seanchen1991.github.io/posts/tco-story/) +//! +//! # Basic Usage +//! +//! ```rust +//! +//! // Create new IterativeBST +//! ``` + use std::cmp::Ordering; use std::fmt::{Debug, Display, Formatter}; use std::vec::IntoIter; @@ -474,42 +487,26 @@ impl Node { } } - fn iterative_post_order_vec(mut root: &HeapNode) -> Vec<&T> { + fn iterative_post_order_vec(root: &HeapNode) -> Vec<&T> { let mut elements = Vec::new(); - let mut stack = Vec::new(); + let mut stack_one = vec![root]; + let mut stack_two = vec![]; - while !stack.is_empty() || root.is_some() { - match root { - Some(node) => { - stack.push(root); - root = &node.left - } - None => { - let mut node = Node::right_node(&stack); - match node { - Some(_) => root = node, - None => { - node = stack.pop().unwrap(); - elements.push(&node.as_ref().unwrap().value); - - while !stack.is_empty() - && node.as_ref().unwrap().value - == Node::right_node(&stack).as_ref().unwrap().value - { - node = stack.pop().unwrap(); - elements.push(&node.as_ref().unwrap().value); - } - } - } - } + while let Some(node) = stack_one.pop().unwrap_or(&None) { + if node.left.is_some() { + stack_one.push(&node.left); + } + if node.right.is_some() { + stack_one.push(&node.right); } + stack_two.push(node); } - elements - } + while let Some(node) = stack_two.pop() { + elements.push(&node.value); + } - fn right_node<'a>(stack: &[&'a HeapNode]) -> &'a HeapNode { - &stack.last().unwrap().as_ref().unwrap().right + elements } fn recursive_post_order_vec<'a>(node: &'a HeapNode, elements: &mut Vec<&'a T>) { @@ -576,40 +573,22 @@ impl Node { fn iterative_consume_post_order_vec(root: HeapNode) -> Vec { let mut elements = Vec::new(); - let mut stack = vec![root]; + let mut stack_one = vec![root]; + let mut stack_two = vec![]; + + while let Some(mut node) = stack_one.pop().unwrap_or(None) { + if let Some(left_node) = node.left.take() { + stack_one.push(Some(left_node)); + } + if let Some(right_node) = node.right.take() { + stack_one.push(Some(right_node)); + } + stack_two.push(node); + } - // while !stack.is_empty() { - // match stack.pop().unwrap() { - // Some(mut node) => { - // let left_node = node.left.take(); - // stack.push(Some(node)); - // stack.push(left_node); - // } - // None => { - // let mut node = stack.pop().unwrap(); - // match node { - // Some(mut node) => { - // let option = node.right.take(); - // stack.push(Some(node)); - // stack.push(option) - // }, - // None => { - // let something = node.take().unwrap(); - // - // while !stack.is_empty() { - // let something_2 = stack.pop().unwrap().take().unwrap(); - // if something.value == something_2.value { - // break; - // } - // elements.push(something_2.value); - // } - // - // elements.push(something.value); - // } - // } - // } - // } - // } + while let Some(node) = stack_two.pop() { + elements.push(node.value); + } elements } diff --git a/tests/iterative_bst.rs b/tests/iterative_bst.rs index a085d0a..e813228 100644 --- a/tests/iterative_bst.rs +++ b/tests/iterative_bst.rs @@ -73,7 +73,7 @@ fn successfully_remove_leaf_node() { } #[test] -fn remove_single_right_node_with_children() { +fn successfully_remove_single_right_node_with_children() { let mut expected_bst = IterativeBST::new(); expected_bst.insert(5); expected_bst.insert(4); @@ -94,7 +94,7 @@ fn remove_single_right_node_with_children() { } #[test] -fn remove_single_left_node_with_children() { +fn successfully_remove_single_left_node_with_children() { let mut expected_bst = IterativeBST::new(); expected_bst.insert(5); expected_bst.insert(3); @@ -114,7 +114,7 @@ fn remove_single_left_node_with_children() { } #[test] -fn remove_node_with_two_children() { +fn successfully_remove_node_with_two_children() { let mut expected_bst = IterativeBST::new(); expected_bst.insert(10); expected_bst.insert(3); @@ -133,7 +133,7 @@ fn remove_node_with_two_children() { } #[test] -fn does_not_fail_when_removing_non_existing_element() { +fn successfully_does_not_fail_when_removing_non_existing_element() { let mut expected_bst = IterativeBST::new(); expected_bst.insert(10); expected_bst.insert(5); @@ -155,7 +155,7 @@ fn does_not_fail_when_removing_non_existing_element() { } #[test] -fn retrieve_element() { +fn successfully_retrieve_element() { let mut bst = IterativeBST::new(); bst.insert(5); bst.insert(10); @@ -168,7 +168,7 @@ fn retrieve_element() { } #[test] -fn retrieve_element_as_mut_and_modify_bst() { +fn successfully_retrieve_element_as_mut_and_modify_bst() { let mut expected_bst = IterativeBST::new(); expected_bst.insert(10); expected_bst.insert(2); @@ -184,7 +184,7 @@ fn retrieve_element_as_mut_and_modify_bst() { } #[test] -fn get_min_from_bst() { +fn successfully_get_min_from_bst() { let mut bst = IterativeBST::new(); assert_eq!(bst.min(), None); @@ -197,7 +197,7 @@ fn get_min_from_bst() { } #[test] -fn get_max_from_bst() { +fn successfully_get_max_from_bst() { let mut bst = IterativeBST::new(); assert_eq!(bst.max(), None); @@ -210,7 +210,7 @@ fn get_max_from_bst() { } #[test] -fn remove_min_from_bst() { +fn successfully_remove_min_from_bst() { let mut bst = IterativeBST::new(); assert_eq!(bst.remove_min(), None); @@ -229,7 +229,7 @@ fn remove_min_from_bst() { } #[test] -fn remove_max_from_bst() { +fn successfully_remove_max_from_bst() { let mut bst = IterativeBST::new(); assert_eq!(bst.remove_max(), None); @@ -319,7 +319,6 @@ fn post_order_iter() { 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)); @@ -341,6 +340,26 @@ fn post_order_iter() { assert_eq!(another_post_order_iter.next(), None); } +#[test] +fn into_pre_order_iter_with_no_elements() { + let bst: IterativeBST = IterativeBST::new(); + + let mut pre_order_traversal = bst.into_pre_order_iter(); + + assert_eq!(pre_order_traversal.next(), None); +} + +#[test] +fn into_pre_order_iter_with_one_element() { + let mut bst = IterativeBST::new(); + bst.insert(3); + + let mut pre_order_traversal = bst.into_pre_order_iter(); + + assert_eq!(pre_order_traversal.next(), Some(3)); + assert_eq!(pre_order_traversal.next(), None); +} + #[test] fn into_pre_order_iter() { let mut iter: IntoIter = IterativeBST::new().into_pre_order_iter(); @@ -363,6 +382,26 @@ fn into_pre_order_iter() { assert_eq!(pre_order_iter.next(), None); } +#[test] +fn into_in_order_iter_with_no_elements() { + let bst: IterativeBST = IterativeBST::new(); + + let mut in_order_traversal = bst.into_in_order_iter(); + + assert_eq!(in_order_traversal.next(), None); +} + +#[test] +fn into_in_order_iter_with_one_element() { + let mut bst = IterativeBST::new(); + bst.insert(3); + + let mut in_order_traversal = bst.into_in_order_iter(); + + assert_eq!(in_order_traversal.next(), Some(3)); + assert_eq!(in_order_traversal.next(), None); +} + #[test] fn into_in_order_iter() { let another_bst: IterativeBST = IterativeBST::new(); @@ -387,11 +426,27 @@ fn into_in_order_iter() { } #[test] -fn into_post_order_iter() { - let another_bst: IterativeBST = IterativeBST::new(); - let mut iter = another_bst.into_post_order_iter(); - assert_eq!(iter.next(), None); +fn into_post_order_iter_with_no_elements() { + let bst: IterativeBST = IterativeBST::new(); + + let mut post_order_traversal = bst.into_post_order_iter(); + assert_eq!(post_order_traversal.next(), None); +} + +#[test] +fn into_post_order_iter_with_one_element() { + let mut bst = IterativeBST::new(); + bst.insert(3); + + let mut post_order_traversal = bst.into_post_order_iter(); + + assert_eq!(post_order_traversal.next(), Some(3)); + assert_eq!(post_order_traversal.next(), None); +} + +#[test] +fn into_post_order_iter_with_many_elements() { let mut bst = IterativeBST::new(); bst.insert(3); bst.insert(4); @@ -410,7 +465,10 @@ fn into_post_order_iter() { } #[test] -fn get_sorted_vec() { +fn successfully_get_sorted_vec() { + let bst: IterativeBST = IterativeBST::new(); + assert!(bst.sorted_vec().is_empty()); + let mut bst = IterativeBST::new(); bst.insert(3); bst.insert(4); @@ -422,7 +480,7 @@ fn get_sorted_vec() { } #[test] -fn bst_into_sorted_vec() { +fn successfully_transfer_bst_into_sorted_vec() { let mut bst = IterativeBST::new(); bst.insert(3); bst.insert(4); @@ -434,7 +492,7 @@ fn bst_into_sorted_vec() { } #[test] -fn get_pre_order_vec() { +fn successfully_get_pre_order_vec() { let mut bst = IterativeBST::new(); assert!(bst.pre_order_vec().is_empty()); @@ -448,7 +506,7 @@ fn get_pre_order_vec() { } #[test] -fn get_in_order_vec() { +fn successfully_get_in_order_vec() { let mut bst = IterativeBST::new(); assert!(bst.in_order_vec().is_empty());