Advertisement
Cornwheat

Binary Tree

Dec 3rd, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.54 KB | None | 0 0
  1. pub struct Tree<T>
  2. {
  3.     gen_Key : Option<T> ,
  4.     gen_ptr_LeftBranch : Option<Box<Tree<T>>> ,
  5.     gen_ptr_RightBranch : Option<Box<Tree<T>>> ,
  6. }
  7.  
  8. impl<T: Ord> Tree<T> {
  9.     /// Creates an empty tree
  10.     pub fn new() -> Self
  11.     {
  12.         let strct_EmptyTree = Tree { gen_Key: None, gen_ptr_LeftBranch: None, gen_ptr_RightBranch: None };
  13.         return strct_EmptyTree;
  14.     }
  15.  
  16.     /// Returns `false` if `key` already exists in the tree, and `true` otherwise.
  17.     pub fn insert(&mut self, key: T) -> bool
  18.     {
  19.         match self.gen_Key
  20.         {
  21.             Some (ref gen_NodeValue) =>
  22.             {
  23.                 if (key == *gen_NodeValue)
  24.                 {
  25.                     return false;
  26.                 }
  27.                 else
  28.                 {
  29.                     let NextNode = if (key < *gen_NodeValue) {&mut self.gen_ptr_LeftBranch} else {&mut self.gen_ptr_RightBranch};
  30.                     match NextNode
  31.                     {
  32.                         &mut Some(ref mut NewNode) => return NewNode.insert(key),
  33.                         &mut None => {
  34.                                          let NewNode = Tree { gen_Key: Some(key), gen_ptr_LeftBranch: None, gen_ptr_RightBranch: None};
  35.                                          let BoxedNode = Some(Box::new(NewNode));
  36.                                          *NextNode = BoxedNode;
  37.                                          return true;
  38.                                      },
  39.                     }
  40.                 }
  41.             }
  42.             None =>
  43.             {
  44.                 self.gen_Key = Some(key);
  45.                 return true;
  46.             },
  47.         }
  48.     }
  49.  
  50.     /// Returns `true` if `key` exists in the tree, and `false` otherwise.
  51.     pub fn find(&self, key: &T) -> bool
  52.     {
  53.         match self.gen_Key
  54.         {
  55.             Some (ref gen_NodeValue) =>
  56.             {
  57.                 if (*key == *gen_NodeValue)
  58.                 {
  59.                     return true;
  60.                 }
  61.                 else
  62.                 {
  63.                     let NextNode = if (*key < *gen_NodeValue) {&self.gen_ptr_LeftBranch} else {&self.gen_ptr_RightBranch};
  64.                     match NextNode
  65.                     {
  66.                         &Some(ref NewNode) => return NewNode.find(key),
  67.                         &None => return false,
  68.                     }
  69.                 }
  70.             }
  71.             None =>
  72.             {
  73.                 return false;
  74.             },
  75.         }
  76.         return false;
  77.     }
  78.  
  79.     /// Returns the preorder traversal of the tree.
  80.     pub fn preorder(&self) -> Vec<&T>
  81.     {
  82.         let mut vec_PreOrderResult = vec!();
  83.         match &self.gen_Key
  84.         {
  85.             &Some(ref NodeValue) => vec_PreOrderResult.push(NodeValue),
  86.             &None => {},
  87.         }
  88.         match &self.gen_ptr_LeftBranch
  89.         {
  90.             &Some(ref LeftNode) => vec_PreOrderResult.append(&mut LeftNode.preorder()),
  91.             &None => {},
  92.         }
  93.         match &self.gen_ptr_RightBranch
  94.         {
  95.             &Some(ref RightNode) => vec_PreOrderResult.append(&mut RightNode.preorder()),
  96.             &None => {},
  97.         }
  98.         return vec_PreOrderResult;
  99.     }
  100.  
  101.     /// Returns the inorder traversal of the tree.
  102.     pub fn inorder(&self) -> Vec<&T>
  103.     {
  104.         let mut vec_InOrderResult = vec!();
  105.         match &self.gen_ptr_LeftBranch
  106.         {
  107.             &Some(ref LeftNode) => vec_InOrderResult.append(&mut LeftNode.inorder()),
  108.             &None => {},
  109.         }
  110.         match &self.gen_Key
  111.         {
  112.             &Some(ref NodeValue) => vec_InOrderResult.push(NodeValue),
  113.             &None => {},
  114.         }
  115.          match &self.gen_ptr_RightBranch
  116.         {
  117.             &Some(ref RightNode) => vec_InOrderResult.append(&mut RightNode.inorder()),
  118.             &None => {},
  119.         }
  120.         return vec_InOrderResult;
  121.     }
  122.  
  123.     /// Returns the postorder traversal of the tree.
  124.     pub fn postorder(&self) -> Vec<&T>
  125.     {
  126.         let mut vec_PostOrderResult = vec!();
  127.         match &self.gen_ptr_LeftBranch
  128.         {
  129.             &Some(ref LeftNode) => vec_PostOrderResult.append(&mut LeftNode.postorder()),
  130.             &None => {},
  131.         }
  132.         match &self.gen_ptr_RightBranch
  133.         {
  134.             &Some(ref RightNode) => vec_PostOrderResult.append(&mut RightNode.postorder()),
  135.             &None => {},
  136.         }
  137.         match &self.gen_Key
  138.         {
  139.             &Some(ref NodeValue) => vec_PostOrderResult.push(NodeValue),
  140.             &None => {},
  141.         }
  142.         return vec_PostOrderResult;
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement