Created
October 24, 2016 21:05
-
-
Save bl4ckb0ne/a1f8a6040cd9e4abc8e1dba23310cf00 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
struct Node<T> | |
{ | |
id: u32, | |
data: T, | |
left: Option<Box<Node<T>>>, | |
right: Option<Box<Node<T>>>, | |
parent: Option<Box<Node<T>>>, | |
} | |
struct Tree<T> | |
{ | |
root: Option<Node<T>>, | |
} | |
impl<T> Node<T> | |
{ | |
pub fn new(value: Option<T>, left: Option<Box<Node<T>>>, right: Option<Box<Node<T>>>, parent: Option<Box<Node<T>>>) -> Node<T> | |
{ | |
Node { | |
data: value.unwrap(), | |
left: left, | |
right: right, | |
parent: parent, | |
} | |
} | |
} | |
impl<T> Tree<T> | |
{ | |
pub fn new() -> Tree<T> | |
{ | |
Tree { | |
root: None, | |
} | |
} | |
pub fn insert(&mut self, value: T) | |
{ | |
match self.root | |
{ | |
Some(ref n) => { | |
println!("Root is not empty"); | |
}, | |
None => { | |
println!("Root is empty"); | |
self.root = Some(Node::new(Some(value), None, None, None)); | |
}, | |
} | |
} | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let mut tree: Tree<i32> = Tree::new(); | |
tree.insert(42); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment