From b5bf9d257694301d2a5d4a1263d47409cc3dfda9 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Fri, 18 Feb 2022 17:58:18 +0000 Subject: [PATCH] Return Result<(),()> instead of bool --- src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 84ae57a..50dc32f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,20 +86,20 @@ impl Node { } } - fn insert(&mut self, value: T) -> bool { + fn insert(&mut self, value: T) -> Result<(), ()> { match value.cmp(&self.value) { - Ordering::Equal => false, + Ordering::Equal => Err(()), Ordering::Less => match self.left { None => { self.left = Some(Box::from(Node::new(value))); - true + Ok(()) } Some(ref mut node) => node.insert(value), }, Ordering::Greater => match self.right { None => { self.right = Some(Box::from(Node::new(value))); - true + Ok(()) } Some(ref mut node) => node.insert(value), }, @@ -148,7 +148,7 @@ impl Node { } } - fn remove(root: &mut HeapNode, value: &T) -> bool { + fn remove(root: &mut HeapNode, value: &T) -> Result<(), ()> { if let Some(ref mut node) = root { return match value.cmp(&node.value) { Ordering::Less => Node::remove(&mut node.left, value), @@ -161,12 +161,12 @@ impl Node { (Some(_), Some(_)) => node.value = Node::extract_min(&mut node.right), } - true + Ok(()) } }; } - false + Err(()) } // TODO -> Implement min(), max(), height(), is_valid(), invert() functions @@ -260,7 +260,7 @@ impl BinarySearchTree { self.size += 1; } Some(ref mut node) => { - if node.insert(value) { + if node.insert(value).is_ok() { self.size += 1; } } @@ -275,7 +275,7 @@ impl BinarySearchTree { } pub fn remove(&mut self, value: &T) { - if Node::remove(&mut self.root, value) { + if Node::remove(&mut self.root, value).is_ok() { self.size -= 1; } }