Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
Created June 24, 2017 13:30
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/be889914cce4aa06a1c91177dfb11b3a to your computer and use it in GitHub Desktop.
Save willmurphyscode/be889914cce4aa06a1c91177dfb11b3a to your computer and use it in GitHub Desktop.
Simple node struct for making a tree
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