From d64e073aaf083331cad330664c211c9226109ea0 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Fri, 18 Feb 2022 22:52:11 +0000 Subject: [PATCH] Implement sorted_vec() which performs exactly the same as in_order_vec() --- src/lib.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 24e9c29..9ad1689 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ type HeapNode = Option>>; impl PartialEq for BinarySearchTree { fn eq(&self, other: &Self) -> bool { - self.in_order_vec() == other.in_order_vec() + self.sorted_vec() == other.sorted_vec() } } @@ -73,7 +73,7 @@ impl Clone for BinarySearchTree { impl Display for BinarySearchTree { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self.in_order_vec()) + write!(f, "{:?}", self.sorted_vec()) } } @@ -358,6 +358,18 @@ impl BinarySearchTree { removed_max } + pub fn sorted_vec(&self) -> Vec<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::in_order_vec(&self.root, &mut elements); + elements + } + + pub fn into_sorted_vec(self) -> Vec { + let mut elements = Vec::new(); + Node::consume_in_order_vec(self.root, &mut elements); + elements + } + pub fn pre_order_vec(&self) -> Vec<&T> { let mut elements: Vec<&T> = Vec::new(); Node::pre_order_vec(&self.root, &mut elements); @@ -657,7 +669,7 @@ mod bst_test { } #[test] - fn pre_order_traversal() { + fn pre_order_iter() { let mut bst = BinarySearchTree::empty(); bst.insert(3); bst.insert(4); @@ -688,7 +700,7 @@ mod bst_test { } #[test] - fn in_order_traversal() { + fn in_order_iter() { let mut bst = BinarySearchTree::empty(); bst.insert(3); bst.insert(4); @@ -719,7 +731,7 @@ mod bst_test { } #[test] - fn post_order_traversal() { + fn post_order_iter() { let mut bst = BinarySearchTree::empty(); bst.insert(3); bst.insert(4); @@ -751,7 +763,7 @@ mod bst_test { } #[test] - fn into_pre_order_traversal() { + fn into_pre_order_iter() { let mut bst = BinarySearchTree::empty(); bst.insert(3); bst.insert(4); @@ -770,7 +782,7 @@ mod bst_test { } #[test] - fn into_in_order_traversal() { + fn into_in_order_iter() { let mut bst = BinarySearchTree::empty(); bst.insert(3); bst.insert(4); @@ -789,7 +801,7 @@ mod bst_test { } #[test] - fn into_post_order_traversal() { + fn into_post_order_iter() { let mut bst = BinarySearchTree::empty(); bst.insert(3); bst.insert(4); @@ -807,6 +819,30 @@ mod bst_test { assert_eq!(post_order_traversal.next(), None); } + #[test] + fn get_sorted_vec() { + let mut bst = BinarySearchTree::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 = BinarySearchTree::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 = BinarySearchTree::empty();