Created
June 26, 2017 11:21
-
-
Save willmurphyscode/f835004fd3b5b1623b421bb05a2a8419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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