Added more Tree documentation.

This commit is contained in:
Jacob VanDomelen 2019-06-02 01:05:10 -07:00
parent 1460f5e6b8
commit 978baef596
5 changed files with 76 additions and 10 deletions

View file

@ -11,7 +11,7 @@ impl tree::Tree<Uuid> {
println!("================================"); println!("================================");
println!("Running simulation for node: {}", self.val); println!("Running simulation for node: {}", self.val);
println!( println!(
"With children {} and {}", "With children {} and {}\n",
tree::Tree::fmt_node(&self.left), tree::Tree::fmt_node(&self.left),
tree::Tree::fmt_node(&self.right) tree::Tree::fmt_node(&self.right)
); );

View file

@ -2,6 +2,7 @@ use std::fs;
use std::str::FromStr; use std::str::FromStr;
use std::fmt; use std::fmt;
use std::string::String; use std::string::String;
use std::string::ToString;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
@ -12,7 +13,7 @@ pub struct FileLinked<T> {
impl<T> FileLinked<T> impl<T> FileLinked<T>
where where
T: FromStr + fmt::Display + Default, T: FromStr + ToString + Default,
{ {
pub fn from_file(path: &str) -> Result<FileLinked<T>, String> { pub fn from_file(path: &str) -> Result<FileLinked<T>, String> {
let meta = fs::metadata(path); let meta = fs::metadata(path);
@ -69,7 +70,7 @@ where
.open(&self.path) .open(&self.path)
.or_else(|_| Err(format!("Unable to open path {}", self.path)))?; .or_else(|_| Err(format!("Unable to open path {}", self.path)))?;
write!(file, "{}", self.val).or_else(|_| { write!(file, "{}", self.val.to_string()).or_else(|_| {
Err(String::from("Unable to write to file.")) Err(String::from("Unable to write to file."))
})?; })?;

View file

@ -3,10 +3,10 @@ extern crate clap;
extern crate regex; extern crate regex;
#[macro_use] #[macro_use]
mod tree; pub mod tree;
mod bracket; pub mod bracket;
mod constants; pub mod constants;
mod file_linked; pub mod file_linked;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View file

@ -29,9 +29,10 @@ fn test_fmt() {
#[test] #[test]
fn test_fmt_node() { fn test_fmt_node() {
let t = btree!(17, btree!(16), btree!(12));
assert_eq!( assert_eq!(
Tree::fmt_node(&Some(Box::new(btree!(17, btree!(16), btree!(12))))), Tree::fmt_node(&t.left),
"17" "16"
); );
assert_eq!( assert_eq!(
Tree::fmt_node(&Some(Box::new(btree!(btree!("foo"))))), Tree::fmt_node(&Some(Box::new(btree!(btree!("foo"))))),

View file

@ -1,7 +1,53 @@
//! An unbalanced binary tree type where each node has an optional left and right child.
//!
//! # Examples
//!
//! ```
//! let mut t = Tree::new(1, None, None);
//! let t2 = Tree::new(2, Some(Box::new(t)), Some(Box::new(Tree::new(3, None, None))));
//! let s = format!("{}", t2);
//!
//! assert_eq!(s, "(2: (1: _|_)|(3: _|_))");
//! t.left = Some(Box::new(Tree::new(4, None, None)));
//! assert_eq!(Tree::fmt_node(t.left), 4);
//! assert_eq!(Tree::from_str(s), t2);
//! ```
//!
//! Additionally the `btree!` macro can be used to conveniently initialize trees:
//!
//! ```
//! # #[macro_use] extern crate tree;
//! # fn main() {
//! let t1 = btree!(1,btree!(2),btree!(3))
//! assert_eq!(format!("{}", t1), "(1: (2: _|_)|(3: _|_)")
//! # }
//! ```
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use regex::Regex; use regex::Regex;
/// An unbalanced binary tree type where each node has an optional left and right child.
///
/// # Examples
///
/// ```
/// let mut t = Tree::new(1, None, None);
/// let t2 = Tree::new(2, Some(Box::new(t)), Some(Box::new(Tree::new(3, None, None))));
/// let s = format!("{}", t2);
///
/// assert_eq!(s, "(2: (1: _|_)|(3: _|_))");
/// t.left = Some(Box::new(Tree::new(4, None, None)));
/// assert_eq!(Tree::fmt_node(t.left), 4);
/// assert_eq!(Tree::from_str(s), t2);
/// ```
///
/// Additionally the `btree!` macro can be used to conveniently initialize trees:
///
/// ```
/// let t1 = btree!(1,btree!(2),btree!(3))
/// assert_eq!(format!("{}", t1), "(1: (2: _|_)|(3: _|_)")
/// ```
#[derive(Default, Clone, PartialEq, Debug)] #[derive(Default, Clone, PartialEq, Debug)]
pub struct Tree<T> { pub struct Tree<T> {
pub val: T, pub val: T,
@ -9,6 +55,23 @@ pub struct Tree<T> {
pub right: Option<Box<Tree<T>>>, pub right: Option<Box<Tree<T>>>,
} }
/// Used to construct trees in a cleaner manner. `btree!` takes 3 arguments, the first being the
/// value of the root node, and the other two being child nodes. The last two arguments are
/// optional.
///
/// ```
/// // A tree with two child nodes.
/// let t = btree!(1, btree!(2), btree!(3));
///
/// // A tree with only a left node.
/// let t_left = btree!(1, btree!(2),);
///
/// // A tree with only a right node.
/// let t_right = btree!(1, ,btree!(3));
///
/// // A tree with no children nodes.
/// let t_single = btree!(1)
/// ```
#[macro_export] #[macro_export]
macro_rules! btree { macro_rules! btree {
($val:expr, $l:expr, $r:expr) => { ($val:expr, $l:expr, $r:expr) => {
@ -23,10 +86,11 @@ macro_rules! btree {
} }
impl<T> Tree<T> { impl<T> Tree<T> {
/// Constructs a new tree object.
pub fn new(val: T, left: Option<Box<Tree<T>>>, right: Option<Box<Tree<T>>>) -> Tree<T> { pub fn new(val: T, left: Option<Box<Tree<T>>>, right: Option<Box<Tree<T>>>) -> Tree<T> {
Tree { val, left, right } Tree { val, left, right }
} }
pub fn fmt_node(t: &Option<Box<Tree<T>>>) -> String pub fn fmt_node(t: &Option<Box<Tree<T>>>) -> String
where where
T: fmt::Display, T: fmt::Display,