FEAT(macro): Add bst! macro that act like vec!

This macro uses the `IterativeBST` to create a new
tree with the given elements, either creating just
and empty tree with `IterativeBST::new()` or using
a combination of `IterativeBST::from_iter()` and
`vec![]` if given any elements
pull/4/head
Carsten Kragelund 2 years ago
parent f85b449e66
commit 63a3190084

@ -89,6 +89,16 @@ use crate::node::{HeapNode, Node};
mod node;
#[macro_export]
macro_rules! bst {
() => (
$crate::IterativeBST::new()
);
($($x:expr),+ $(,)?) => (
$crate::IterativeBST::from_iter(vec![$($x),+].into_iter())
);
}
/// A trait containing all the common operations of Binary Search Trees.
///
/// # Examples

@ -737,4 +737,4 @@ fn successfully_clone_into_another_bst() {
actual_bst.clone_from(&expected_bst);
assert_eq!(actual_bst, expected_bst);
}
}

@ -0,0 +1,17 @@
use bst_rs::bst;
#[test]
fn successfully_construct_bst_from_macro() {
let mut actual_bst = IterativeBST::new();
actual_bst.insert(3);
actual_bst.insert(2);
let expected_bst = bst![3,2];
assert_eq!(actual_bst, expected_bst);
}
#[test]
fn verify_permutations_produce_same_tree() {
let expected_bst = bst![2,3];
let expected_bst = bst![3,2];
assert_eq!(actual_bst, expected_bst);
}
Loading…
Cancel
Save