Skip to content

Commit

Permalink
Merge pull request #8 from sheroz/sheroz-patch
Browse files Browse the repository at this point in the history
refactored
  • Loading branch information
sheroz committed Sep 25, 2023
2 parents 3bfec5c + a6e8c6e commit eeefb52
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
22 changes: 9 additions & 13 deletions src/binary_search_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl BinarySearchTree {
new_node_ref: &BinaryTreeNodeRef,
) -> Option<BinaryTreeNodeRef> {
match node {
None => return Some(new_node_ref.clone()),
None => Some(new_node_ref.clone()),
Some(node_ref) => {
let mut parent = None;
if node_ref.borrow().data > new_node_ref.borrow().data {
Expand All @@ -34,10 +34,10 @@ impl BinarySearchTree {
}
node_ref.borrow_mut().right = Self::insert_recursion(right, new_node_ref);
}
if parent.is_some() {
new_node_ref.borrow_mut().parent = parent.unwrap();
if let Some(p) = parent {
new_node_ref.borrow_mut().parent = p;
}
return Some(node_ref);
Some(node_ref)
}
}
}
Expand All @@ -63,7 +63,7 @@ impl BinarySearchTree {
}
else {
let node_ref = insert_node.as_ref().unwrap();
new_node.borrow_mut().parent = Rc::downgrade(&node_ref);
new_node.borrow_mut().parent = Rc::downgrade(node_ref);
let mut node = node_ref.borrow_mut();
if new_node.borrow().data < node.data {
node.left = Some(new_node.clone());
Expand All @@ -72,17 +72,15 @@ impl BinarySearchTree {
node.right = Some(new_node.clone());
}
}
return insert_node;
insert_node
}

pub fn is_binary_search_tree(node: &BinaryTreeNodeRef) -> bool {
BinarySearchTree::is_binary_search_tree_v3(node)
}

pub fn search(&self, data: u32) -> Option<BinaryTreeNodeRef> {
if self.tree.root.is_none() {
return None;
}
self.tree.root.as_ref()?;

let node = self.tree.root.as_ref().unwrap();

Expand All @@ -101,10 +99,8 @@ impl BinarySearchTree {
if let Some(left) = n.left.as_ref() {
queue.push_back(left.clone());
}
} else {
if let Some(right) = n.right.as_ref() {
queue.push_back(right.clone());
}
} else if let Some(right) = n.right.as_ref() {
queue.push_back(right.clone());
}
}
None
Expand Down
5 changes: 1 addition & 4 deletions src/binary_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ impl BinaryTree {
}

fn get_node_id(v: &Option<BinaryTreeNodeRef>) -> Option<Uuid> {
match v {
Some(node) => Some(node.borrow().id),
None => None,
}
v.as_ref().map(|node| node.borrow().id)
}
}

Expand Down
26 changes: 19 additions & 7 deletions src/generic_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ impl TreeNodeRefBuild for TreeNodeRef {
}
}

impl Default for TreeNode {
fn default() -> Self {
Self::new()
}
}

impl TreeNode {
pub fn new() -> Self {
TreeNode {
Expand All @@ -42,6 +48,12 @@ impl TreeNode {
}
}

impl Default for Tree {
fn default() -> Self {
Self::new()
}
}

impl Tree {
pub fn new() -> Self {
Tree { root: None }
Expand Down Expand Up @@ -215,15 +227,15 @@ mod tests {
let flatten = tree.flatten();
let node_ref = flatten[2].as_ref();
let uuid = node_ref.borrow().uuid;
println!("Looking for:\n{}", uuid.to_string());
println!("Looking for:\n{}", uuid);

let r = tree.search(uuid);
assert!(r.is_some());

let v = r.unwrap();
let out = format!(
"{}:{}",
v.as_ref().borrow().uuid.to_string(),
v.as_ref().borrow().uuid,
v.as_ref().borrow().text.clone()
);
println!("Found:\n{}", out);
Expand All @@ -239,7 +251,7 @@ mod tests {
.map(|v| {
format!(
"{}:{}",
v.as_ref().borrow().uuid.to_string(),
v.as_ref().borrow().uuid,
v.as_ref().borrow().text.clone()
)
})
Expand Down Expand Up @@ -283,7 +295,7 @@ mod tests {
.map(|v| {
format!(
"{}:{}",
v.as_ref().borrow().uuid.to_string(),
v.as_ref().borrow().uuid,
v.as_ref().borrow().text.clone()
)
})
Expand All @@ -292,14 +304,14 @@ mod tests {

let node_ref = flatten[4].as_ref();
let uuid = node_ref.borrow().uuid;
println!("Looking for:\n{}", uuid.to_string());
println!("Looking for:\n{}", uuid);
let removed = tree.remove(uuid);
assert!(removed.is_some());

let v = removed.unwrap();
let out = format!(
"{}:{}",
v.as_ref().borrow().uuid.to_string(),
v.as_ref().borrow().uuid,
v.as_ref().borrow().text.clone()
);
println!("Removed:\n{}", out);
Expand All @@ -310,7 +322,7 @@ mod tests {
.map(|v| {
format!(
"{}:{}",
v.as_ref().borrow().uuid.to_string(),
v.as_ref().borrow().uuid,
v.as_ref().borrow().text.clone()
)
})
Expand Down

0 comments on commit eeefb52

Please sign in to comment.