Created
June 24, 2017 13:30
-
-
Save willmurphyscode/be889914cce4aa06a1c91177dfb11b3a to your computer and use it in GitHub Desktop.
Simple node struct for making a tree
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
use helpers; | |
use std::fmt; | |
#[derive(Debug)] | |
pub struct Node { | |
pub left_child: Option<Box<Node>>, | |
pub right_child: Option<Box<Node>>, | |
pub has_fruit: bool, | |
} | |
impl Node { | |
pub fn random_tree() -> Node { | |
let has_left = helpers::coin_flip(); | |
let has_right = helpers::coin_flip(); | |
let mut left: Option<Box<Node>> = None; | |
if has_left { | |
left = Some(Box::new(Node::random_tree())); | |
} | |
let mut right: Option<Box<Node>> = None; | |
if has_right { | |
right = Some(Box::new(Node::random_tree())); | |
} | |
Node { | |
left_child: left, | |
right_child: right, | |
has_fruit: helpers::coin_flip(), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment