Created
July 6, 2021 16:24
-
-
Save alfonmga/978de58f0a717e117e0f988c6366ac7a to your computer and use it in GitHub Desktop.
Rust: binary tree lexically ordered
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
fn insert(&mut self, data: &str) { | |
if data < &self.payload { | |
match self.left { | |
Some(ref mut n) => n.insert(data), | |
None => self.set_left(Self::new(data)), | |
} | |
} else { | |
match self.right { | |
Some(ref mut n) => n.insert(data), | |
None => self.set_right(Self::new(data)), | |
} | |
} | |
} | |
... | |
fn main() { | |
let mut root = Node::new("root"); | |
root.insert("one"); | |
root.insert("two"); | |
root.insert("four"); | |
println!("root {:#?}", root); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output result: