Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
Created June 26, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willmurphyscode/f835004fd3b5b1623b421bb05a2a8419 to your computer and use it in GitHub Desktop.
Save willmurphyscode/f835004fd3b5b1623b421bb05a2a8419 to your computer and use it in GitHub Desktop.
extern crate rand;
mod node;
mod helpers;
fn recursive_count_fruit(node: Option<Box<node::Node>>) -> i32 {
match node {
Some(val) => {
let our_fruit = if val.has_fruit { 1 } else { 0 };
let unboxed = *val;
println!("{}", unboxed);
(our_fruit + recursive_count_fruit(unboxed.left_child) +
recursive_count_fruit(unboxed.right_child))
}
None => 0,
}
}
fn count_fruit(root: node::Node) -> i32 {
recursive_count_fruit(Some(Box::new(root)))
}
fn main() {
let root = node::Node::random_tree();
let count = count_fruit(root);
println!("There were {} pieces of fruit", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment