Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct Tree<T>
- {
- gen_Key : Option<T> ,
- gen_ptr_LeftBranch : Option<Box<Tree<T>>> ,
- gen_ptr_RightBranch : Option<Box<Tree<T>>> ,
- }
- impl<T: Ord> Tree<T> {
- /// Creates an empty tree
- pub fn new() -> Self
- {
- let strct_EmptyTree = Tree { gen_Key: None, gen_ptr_LeftBranch: None, gen_ptr_RightBranch: None };
- return strct_EmptyTree;
- }
- /// Returns `false` if `key` already exists in the tree, and `true` otherwise.
- pub fn insert(&mut self, key: T) -> bool
- {
- match self.gen_Key
- {
- Some (ref gen_NodeValue) =>
- {
- if (key == *gen_NodeValue)
- {
- return false;
- }
- else
- {
- let NextNode = if (key < *gen_NodeValue) {&mut self.gen_ptr_LeftBranch} else {&mut self.gen_ptr_RightBranch};
- match NextNode
- {
- &mut Some(ref mut NewNode) => return NewNode.insert(key),
- &mut None => {
- let NewNode = Tree { gen_Key: Some(key), gen_ptr_LeftBranch: None, gen_ptr_RightBranch: None};
- let BoxedNode = Some(Box::new(NewNode));
- *NextNode = BoxedNode;
- return true;
- },
- }
- }
- }
- None =>
- {
- self.gen_Key = Some(key);
- return true;
- },
- }
- }
- /// Returns `true` if `key` exists in the tree, and `false` otherwise.
- pub fn find(&self, key: &T) -> bool
- {
- match self.gen_Key
- {
- Some (ref gen_NodeValue) =>
- {
- if (*key == *gen_NodeValue)
- {
- return true;
- }
- else
- {
- let NextNode = if (*key < *gen_NodeValue) {&self.gen_ptr_LeftBranch} else {&self.gen_ptr_RightBranch};
- match NextNode
- {
- &Some(ref NewNode) => return NewNode.find(key),
- &None => return false,
- }
- }
- }
- None =>
- {
- return false;
- },
- }
- return false;
- }
- /// Returns the preorder traversal of the tree.
- pub fn preorder(&self) -> Vec<&T>
- {
- let mut vec_PreOrderResult = vec!();
- match &self.gen_Key
- {
- &Some(ref NodeValue) => vec_PreOrderResult.push(NodeValue),
- &None => {},
- }
- match &self.gen_ptr_LeftBranch
- {
- &Some(ref LeftNode) => vec_PreOrderResult.append(&mut LeftNode.preorder()),
- &None => {},
- }
- match &self.gen_ptr_RightBranch
- {
- &Some(ref RightNode) => vec_PreOrderResult.append(&mut RightNode.preorder()),
- &None => {},
- }
- return vec_PreOrderResult;
- }
- /// Returns the inorder traversal of the tree.
- pub fn inorder(&self) -> Vec<&T>
- {
- let mut vec_InOrderResult = vec!();
- match &self.gen_ptr_LeftBranch
- {
- &Some(ref LeftNode) => vec_InOrderResult.append(&mut LeftNode.inorder()),
- &None => {},
- }
- match &self.gen_Key
- {
- &Some(ref NodeValue) => vec_InOrderResult.push(NodeValue),
- &None => {},
- }
- match &self.gen_ptr_RightBranch
- {
- &Some(ref RightNode) => vec_InOrderResult.append(&mut RightNode.inorder()),
- &None => {},
- }
- return vec_InOrderResult;
- }
- /// Returns the postorder traversal of the tree.
- pub fn postorder(&self) -> Vec<&T>
- {
- let mut vec_PostOrderResult = vec!();
- match &self.gen_ptr_LeftBranch
- {
- &Some(ref LeftNode) => vec_PostOrderResult.append(&mut LeftNode.postorder()),
- &None => {},
- }
- match &self.gen_ptr_RightBranch
- {
- &Some(ref RightNode) => vec_PostOrderResult.append(&mut RightNode.postorder()),
- &None => {},
- }
- match &self.gen_Key
- {
- &Some(ref NodeValue) => vec_PostOrderResult.push(NodeValue),
- &None => {},
- }
- return vec_PostOrderResult;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement