From 63a319008440e59a961f1278edf592255cf862db Mon Sep 17 00:00:00 2001 From: Carsten Kragelund Date: Sat, 1 Oct 2022 02:14:03 +0200 Subject: [PATCH 1/2] 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 --- src/lib.rs | 10 ++++++++++ tests/iterative_bst.rs | 2 +- tests/macro.rs | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/macro.rs diff --git a/src/lib.rs b/src/lib.rs index 6d609d3..df5e7db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 diff --git a/tests/iterative_bst.rs b/tests/iterative_bst.rs index e4fcc52..73ee128 100644 --- a/tests/iterative_bst.rs +++ b/tests/iterative_bst.rs @@ -737,4 +737,4 @@ fn successfully_clone_into_another_bst() { actual_bst.clone_from(&expected_bst); assert_eq!(actual_bst, expected_bst); -} \ No newline at end of file +} diff --git a/tests/macro.rs b/tests/macro.rs new file mode 100644 index 0000000..0d48297 --- /dev/null +++ b/tests/macro.rs @@ -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); +} From fdf7d1c8cf27119b699acd570f43c89e7f1d07cc Mon Sep 17 00:00:00 2001 From: Carsten Kragelund Date: Sat, 1 Oct 2022 02:35:39 +0200 Subject: [PATCH 2/2] FIX(docs, tests): Fix empty doctests, and add docs * Adds documentation to the `bst!` macro * Changes the tree visualizations from `rust` blocks to just `text`, so that `cargo doc` does not complain * Runs `cargo fmt` --- src/lib.rs | 86 ++++++++++++++++++++++++++++++++------------------ tests/macro.rs | 8 ++--- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index df5e7db..0ed5394 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,12 +83,36 @@ //! assert_ne!(recursive_bst, RecursiveBST::new()); //! ``` +use crate::node::{HeapNode, Node}; use std::fmt::{Debug, Display, Formatter}; use std::vec::IntoIter; -use crate::node::{HeapNode, Node}; mod node; +/// Creates a [`IterativeBST`] containing the arguments. +/// +/// # Important +/// +/// If given no arguments this will be equivalent to calling +/// `IterativeBST::new()` +/// +/// # Example +/// - Create a [`IterativeBST`] containing a given list of elements: +/// +/// ```rust +/// use bst_rs::{BinarySearchTree, IterativeBST, bst}; +/// +/// let t1 = bst![1, 2, 3]; +/// // Which is functionally equivalent to +/// let t2 = IterativeBST::from_iter(vec![1,2,3]); +/// // and produces the following tree +/// // 2 +/// // / \ +/// // 1 3 +/// assert_eq!(t1, t2); +/// ``` +/// +/// [`IterativeBST`]: crate::IterativeBST #[macro_export] macro_rules! bst { () => ( @@ -219,12 +243,12 @@ pub trait BinarySearchTree { /// /// Given a tree that looks like: /// - /// ```rust - /// // 4 - /// // / \ - /// // 2 6 - /// // / \ / \ - /// // 1 3 5 7 + /// ```text + /// 4 + /// / \ + /// 2 6 + /// / \ / \ + /// 1 3 5 7 /// ``` /// /// The height is: **2** @@ -255,12 +279,12 @@ pub trait BinarySearchTree { /// # Example /// /// Given a tree that looks like: - /// ```rust - /// // 4 - /// // / \ - /// // 2 6 - /// // / \ / \ - /// // 1 3 5 7 + /// ```text + /// 4 + /// / \ + /// 2 6 + /// / \ / \ + /// 1 3 5 7 /// ``` /// The pre_order_vec is: **[&4, &2, &1, &3, &6, &5, &7].** fn pre_order_vec(&self) -> Vec<&T>; @@ -275,12 +299,12 @@ pub trait BinarySearchTree { /// # Example /// /// Given a tree that looks like: - /// ```rust - /// // 4 - /// // / \ - /// // 2 6 - /// // / \ / \ - /// // 1 3 5 7 + /// ```text + /// 4 + /// / \ + /// 2 6 + /// / \ / \ + /// 1 3 5 7 /// ``` /// The in_order_vec is: **[&1, &2, &3, &4, &5, &6, &7].** fn in_order_vec(&self) -> Vec<&T>; @@ -290,12 +314,12 @@ pub trait BinarySearchTree { /// # Example /// /// Given a tree that looks like: - /// ```rust - /// // 4 - /// // / \ - /// // 2 6 - /// // / \ / \ - /// // 1 3 5 7 + /// ```text + /// 4 + /// / \ + /// 2 6 + /// / \ / \ + /// 1 3 5 7 /// ``` /// The post_order_vec is: **[&1, &3, &2, &5, &7, &6, &4].** fn post_order_vec(&self) -> Vec<&T>; @@ -305,12 +329,12 @@ pub trait BinarySearchTree { /// # Example /// /// Given a tree that looks like: - /// ```rust - /// // 4 - /// // / \ - /// // 2 6 - /// // / \ / \ - /// // 1 3 5 7 + /// ```text + /// 4 + /// / \ + /// 2 6 + /// / \ / \ + /// 1 3 5 7 /// ``` /// The post_order_vec is: **[&4, &2, &6, &1, &3, &5, &7].** fn level_order_vec(&self) -> Vec<&T>; diff --git a/tests/macro.rs b/tests/macro.rs index 0d48297..463ebb3 100644 --- a/tests/macro.rs +++ b/tests/macro.rs @@ -1,17 +1,17 @@ -use bst_rs::bst; +use bst_rs::{bst, BinarySearchTree, IterativeBST}; #[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]; + 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]; + let actual_bst = bst![2, 3]; + let expected_bst = bst![3, 2]; assert_eq!(actual_bst, expected_bst); }