SHARE
TWEET

Untitled

a guest Feb 17th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct BST {
  2.     l: Option< Box<BST> >,
  3.     r: Option< Box<BST> >,
  4.     v: i32,
  5. }
  6.  
  7. impl BST {
  8.     fn new(x: i32) -> Self {
  9.         BST { l: None, r: None, v:x }
  10.     }
  11.  
  12.     fn add(&mut self, x: i32) {
  13.         if self.v < x {
  14.             if let Some(ref mut t) = self.l {
  15.                 t.add(x);
  16.             } else {
  17.                 self.l = Some( Box::new(BST::new(x)) );
  18.             }
  19.         }
  20.         if self.v > x {
  21.             if let Some(ref mut t) = self.r {
  22.                 t.add(x);
  23.             } else {
  24.                 self.r = Some( Box::new(BST::new(x)) );
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. fn main() {
  31.     let mut b = BST::new(5);
  32.     b.add(4);
  33.     b.add(6);
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top