Implement sorted_vec() which performs exactly the same as in_order_vec()

pull/3/head
sgoudham 3 years ago
parent cfd35f38e3
commit d64e073aaf
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -19,7 +19,7 @@ type HeapNode<T> = Option<Box<Node<T>>>;
impl<T: Ord> PartialEq for BinarySearchTree<T> { impl<T: Ord> PartialEq for BinarySearchTree<T> {
fn eq(&self, other: &Self) -> bool { 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<T: Ord + Clone> Clone for BinarySearchTree<T> {
impl<T: Ord + Debug> Display for BinarySearchTree<T> { impl<T: Ord + Debug> Display for BinarySearchTree<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 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<T: Ord> BinarySearchTree<T> {
removed_max 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<T> {
let mut elements = Vec::new();
Node::consume_in_order_vec(self.root, &mut elements);
elements
}
pub fn pre_order_vec(&self) -> Vec<&T> { pub fn pre_order_vec(&self) -> Vec<&T> {
let mut elements: Vec<&T> = Vec::new(); let mut elements: Vec<&T> = Vec::new();
Node::pre_order_vec(&self.root, &mut elements); Node::pre_order_vec(&self.root, &mut elements);
@ -657,7 +669,7 @@ mod bst_test {
} }
#[test] #[test]
fn pre_order_traversal() { fn pre_order_iter() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -688,7 +700,7 @@ mod bst_test {
} }
#[test] #[test]
fn in_order_traversal() { fn in_order_iter() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -719,7 +731,7 @@ mod bst_test {
} }
#[test] #[test]
fn post_order_traversal() { fn post_order_iter() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -751,7 +763,7 @@ mod bst_test {
} }
#[test] #[test]
fn into_pre_order_traversal() { fn into_pre_order_iter() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -770,7 +782,7 @@ mod bst_test {
} }
#[test] #[test]
fn into_in_order_traversal() { fn into_in_order_iter() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -789,7 +801,7 @@ mod bst_test {
} }
#[test] #[test]
fn into_post_order_traversal() { fn into_post_order_iter() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -807,6 +819,30 @@ mod bst_test {
assert_eq!(post_order_traversal.next(), None); 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] #[test]
fn get_pre_order_vec() { fn get_pre_order_vec() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();

Loading…
Cancel
Save