Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct BST {
- l: Option< Box<BST> >,
- r: Option< Box<BST> >,
- v: i32,
- }
- impl BST {
- fn new(x: i32) -> Self {
- BST { l: None, r: None, v:x }
- }
- fn add(&mut self, x: i32) {
- if self.v < x {
- if let Some(ref mut t) = self.l {
- t.add(x);
- } else {
- self.l = Some( Box::new(BST::new(x)) );
- }
- }
- if self.v > x {
- if let Some(ref mut t) = self.r {
- t.add(x);
- } else {
- self.r = Some( Box::new(BST::new(x)) );
- }
- }
- }
- }
- fn main() {
- let mut b = BST::new(5);
- b.add(4);
- b.add(6);
- }
Add Comment
Please, Sign In to add comment