Created
April 15, 2018 18:43
-
-
Save Boot-Error/74462c2aec6ccabbb6e7aa2586636615 to your computer and use it in GitHub Desktop.
Binary Search Tree implementation in Rust
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
| use std::cmp::Ordering; | |
| #[derive(Debug)] | |
| struct Node { | |
| value: i32, | |
| left_child: Box<Option<Node>>, | |
| right_child: Box<Option<Node>>, | |
| } | |
| impl Node { | |
| fn new(value: i32) -> Node { | |
| Node { | |
| value: value, | |
| left_child: Box::new(Option::None), | |
| right_child: Box::new(Option::None), | |
| } | |
| } | |
| fn add_child(&mut self, val: i32) { | |
| match val.cmp(&self.value) { | |
| Ordering::Less => { | |
| match *self.left_child { | |
| None => self.left_child = Box::new(Some(Node::new(val))), | |
| Some(ref mut node) => node.add_child(val) | |
| } | |
| } | |
| Ordering::Greater => { | |
| match *self.right_child { | |
| None => self.right_child = Box::new(Some(Node::new(val))), | |
| Some(ref mut node) => node.add_child(val) | |
| } | |
| } | |
| Ordering::Equal => {} | |
| } | |
| } | |
| fn inorder_walk(&self) { | |
| match *self.left_child { | |
| Some(ref node) => node.inorder_walk(), | |
| None => {} | |
| }; | |
| println!("{}", self.value); | |
| match *self.right_child { | |
| Some(ref node) => node.inorder_walk(), | |
| None => {} | |
| } | |
| } | |
| fn tree_search(&self, val: i32) -> Option<&Node> { | |
| match val.cmp(&self.value) { | |
| Ordering::Equal => Some(self), | |
| Ordering::Less => { | |
| match *self.left_child { | |
| Some(ref node) => node.tree_search(val), | |
| None => None | |
| } | |
| }, | |
| Ordering::Greater => { | |
| match *self.right_child { | |
| Some(ref node) => node.tree_search(val), | |
| None => None | |
| } | |
| } | |
| } | |
| } | |
| fn get_parent<'a>(&self, root_node: &'a Node) -> Option<&'a Node> { | |
| match self.value.cmp(&root_node.value) { | |
| Ordering::Equal => None, | |
| Ordering::Less => { | |
| match *root_node.left_child { | |
| Some(ref node) => { | |
| if node.value == self.value { | |
| Some(root_node) | |
| } else { | |
| self.get_parent(node) | |
| } | |
| }, | |
| None => None | |
| } | |
| }, | |
| Ordering::Greater => { | |
| match *root_node.right_child { | |
| Some(ref node) => { | |
| if node.value == self.value { | |
| Some(root_node) | |
| } else { | |
| self.get_parent(node) | |
| } | |
| }, | |
| None => None | |
| } | |
| } | |
| } | |
| } | |
| fn minimum(&self) -> &Node { | |
| match *self.left_child { | |
| Some(ref node) => node.minimum(), | |
| None => self | |
| } | |
| } | |
| fn maximum(&self) -> &Node { | |
| match *self.right_child{ | |
| Some(ref node) => node.minimum(), | |
| None => self | |
| } | |
| } | |
| } | |
| fn main() { | |
| let mut my_node = Node::new(2); | |
| my_node.add_child(3); | |
| my_node.add_child(1); | |
| my_node.add_child(5); | |
| my_node.add_child(4); | |
| println!("{:?}", my_node); | |
| my_node.inorder_walk(); | |
| let some_node = my_node.tree_search(5); | |
| println!("{:?}", some_node); | |
| println!("{:?}", my_node.minimum()); | |
| println!("{:?}", match some_node { | |
| Some(node) => node.get_parent(&my_node), | |
| None => None | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment