Genericized Tree struct implementation.
This commit is contained in:
parent
dd282236fd
commit
8946a3cfb8
4 changed files with 49 additions and 48 deletions
|
@ -1,6 +1,7 @@
|
||||||
mod tree;
|
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
|
use super::tree;
|
||||||
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
// pub struct Bracket {
|
// pub struct Bracket {
|
||||||
|
@ -9,8 +10,8 @@ use uuid::Uuid;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
/// Constructs a tree with a given height while simultaneously running a simulation on each node.
|
/// Constructs a tree with a given height while simultaneously running a simulation on each node.
|
||||||
fn build_tree(h: u32) -> Option<Box<tree::Tree>> {
|
fn build_tree(h: u32) -> Option<Box<tree::Tree<Uuid>>> {
|
||||||
let mut result: Option<Box<tree::Tree>> = None;
|
let mut result: Option<Box<tree::Tree<Uuid>>> = None;
|
||||||
|
|
||||||
// Recursively building a tree and running the simulation after wards to ensure a bottom-up
|
// Recursively building a tree and running the simulation after wards to ensure a bottom-up
|
||||||
// execution order.
|
// execution order.
|
||||||
|
@ -30,7 +31,7 @@ fn build_tree(h: u32) -> Option<Box<tree::Tree>> {
|
||||||
/// TODO: Explain reasoning for bracket system against genetic algorithm.
|
/// TODO: Explain reasoning for bracket system against genetic algorithm.
|
||||||
pub fn run_bracket() {
|
pub fn run_bracket() {
|
||||||
let mut height = 1;
|
let mut height = 1;
|
||||||
let mut tree: tree::Tree = *build_tree(height).expect("Error getting result from build_tree.");
|
let mut tree: tree::Tree<Uuid> = *build_tree(height).expect("Error getting result from build_tree.");
|
||||||
|
|
||||||
// Building tree one node at a time, appending to the top.
|
// Building tree one node at a time, appending to the top.
|
||||||
loop {
|
loop {
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
use std::fmt;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub struct Tree {
|
|
||||||
id: Uuid,
|
|
||||||
left: Option<Box<Tree>>,
|
|
||||||
right: Option<Box<Tree>>
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn combine_trees(id: Uuid, l: Option<Box<Tree>>, r: Option<Box<Tree>>) -> Tree {
|
|
||||||
Tree {
|
|
||||||
id: id,
|
|
||||||
left: l,
|
|
||||||
right: r
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Tree {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let node_str = |t: &Option<Box<Tree>>| -> String {
|
|
||||||
match t {
|
|
||||||
Some(n) => format!("{}", *n),
|
|
||||||
_ => String::from("_")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
write!(f, "({} :{}|{})", self.id, node_str(&self.left), node_str(&self.right))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fmt_node(t: &Option<Box<Tree>>) -> String {
|
|
||||||
match t {
|
|
||||||
Some(n) => format!("{}", (*n).id),
|
|
||||||
_ => String::from("_")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Tree {
|
|
||||||
pub fn run_simulation(&self) {
|
|
||||||
println!("================================");
|
|
||||||
println!("Running simulation for node: {}", self.id);
|
|
||||||
println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,5 @@
|
||||||
mod bracket;
|
mod bracket;
|
||||||
|
mod tree;
|
||||||
mod constants;
|
mod constants;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|
43
gemla/src/tree/mod.rs
Normal file
43
gemla/src/tree/mod.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
pub struct Tree<T> {
|
||||||
|
val: T,
|
||||||
|
left: Option<Box<Tree<T>>>,
|
||||||
|
right: Option<Box<Tree<T>>>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn combine_trees<T>(v: T, l: Option<Box<Tree<T>>>, r: Option<Box<Tree<T>>>) -> Tree<T> {
|
||||||
|
Tree {
|
||||||
|
val: v,
|
||||||
|
left: l,
|
||||||
|
right: r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Display> fmt::Display for Tree<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let node_str = |t: &Option<Box<Tree<T>>>| -> String {
|
||||||
|
match t {
|
||||||
|
Some(n) => format!("{}", *n),
|
||||||
|
_ => String::from("_")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
write!(f, "({} :{}|{})", self.val, node_str(&self.left), node_str(&self.right))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fmt_node<T: fmt::Display>(t: &Option<Box<Tree<T>>>) -> String {
|
||||||
|
match t {
|
||||||
|
Some(n) => format!("{}", (*n).val),
|
||||||
|
_ => String::from("_")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Display> Tree<T> {
|
||||||
|
pub fn run_simulation(&self) {
|
||||||
|
println!("================================");
|
||||||
|
println!("Running simulation for node: {}", self.val);
|
||||||
|
println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue