From 316d04a7f2d497ecb16a4c25b9157ff102285acb Mon Sep 17 00:00:00 2001 From: sgoudham Date: Mon, 21 Feb 2022 00:58:20 +0000 Subject: [PATCH] Add function is_not_empty() for increased readability --- src/lib.rs | 1045 ++++++++++++++++++++-------------------- tests/iterative_bst.rs | 9 + 2 files changed, 536 insertions(+), 518 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 99e064c..5221882 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,7 @@ pub trait BinarySearchTree { fn new() -> Self; fn size(&self) -> usize; fn is_empty(&self) -> bool; + fn is_not_empty(&self) -> bool; fn insert(&mut self, value: T); fn contains(&self, value: &T) -> bool; fn remove(&mut self, value: &T); @@ -159,13 +160,13 @@ struct Node { right: HeapNode, } -impl PartialEq for RecursiveBST { +impl PartialEq for IterativeBST { fn eq(&self, other: &Self) -> bool { self.sorted_vec() == other.sorted_vec() } } -impl Extend for RecursiveBST { +impl Extend for IterativeBST { fn extend>(&mut self, iter: I) { for value in iter.into_iter() { self.insert(value) @@ -173,17 +174,17 @@ impl Extend for RecursiveBST { } } -impl FromIterator for RecursiveBST { +impl FromIterator for IterativeBST { fn from_iter>(iter: I) -> Self { - let mut bst = RecursiveBST::new(); + let mut bst = IterativeBST::new(); bst.extend(iter); bst } } -impl From> for RecursiveBST { +impl From> for IterativeBST { fn from(vec: Vec) -> Self { - let mut bst = RecursiveBST::new(); + let mut bst = IterativeBST::new(); for value in vec.into_iter() { bst.insert(value); } @@ -191,9 +192,9 @@ impl From> for RecursiveBST { } } -impl From<&[T]> for RecursiveBST { +impl From<&[T]> for IterativeBST { fn from(slice: &[T]) -> Self { - let mut bst = RecursiveBST::new(); + let mut bst = IterativeBST::new(); for value in slice { bst.insert((*value).clone()); } @@ -201,9 +202,9 @@ impl From<&[T]> for RecursiveBST { } } -impl Clone for RecursiveBST { +impl Clone for IterativeBST { fn clone(&self) -> Self { - let mut bst = RecursiveBST::new(); + let mut bst = IterativeBST::new(); for value in self.in_order_iter() { bst.insert((*value).clone()); @@ -213,19 +214,19 @@ impl Clone for RecursiveBST { } } -impl Display for RecursiveBST { +impl Display for IterativeBST { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.sorted_vec()) } } -impl PartialEq for IterativeBST { +impl PartialEq for RecursiveBST { fn eq(&self, other: &Self) -> bool { self.sorted_vec() == other.sorted_vec() } } -impl Extend for IterativeBST { +impl Extend for RecursiveBST { fn extend>(&mut self, iter: I) { for value in iter.into_iter() { self.insert(value) @@ -233,17 +234,17 @@ impl Extend for IterativeBST { } } -impl FromIterator for IterativeBST { +impl FromIterator for RecursiveBST { fn from_iter>(iter: I) -> Self { - let mut bst = IterativeBST::new(); + let mut bst = RecursiveBST::new(); bst.extend(iter); bst } } -impl From> for IterativeBST { +impl From> for RecursiveBST { fn from(vec: Vec) -> Self { - let mut bst = IterativeBST::new(); + let mut bst = RecursiveBST::new(); for value in vec.into_iter() { bst.insert(value); } @@ -251,9 +252,9 @@ impl From> for IterativeBST { } } -impl From<&[T]> for IterativeBST { +impl From<&[T]> for RecursiveBST { fn from(slice: &[T]) -> Self { - let mut bst = IterativeBST::new(); + let mut bst = RecursiveBST::new(); for value in slice { bst.insert((*value).clone()); } @@ -261,9 +262,9 @@ impl From<&[T]> for IterativeBST { } } -impl Clone for IterativeBST { +impl Clone for RecursiveBST { fn clone(&self) -> Self { - let mut bst = IterativeBST::new(); + let mut bst = RecursiveBST::new(); for value in self.in_order_iter() { bst.insert((*value).clone()); @@ -273,701 +274,709 @@ impl Clone for IterativeBST { } } -impl Display for IterativeBST { +impl Display for RecursiveBST { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.sorted_vec()) } } -impl Node { - fn new(value: T) -> Node { - Node { - value, - left: None, - right: None, +impl BinarySearchTree for IterativeBST { + fn new() -> IterativeBST { + IterativeBST { + root: None, + size: 0, } } - fn iterative_insert(mut root: &mut HeapNode, value: T) -> Result<(), ()> { - while let Some(ref mut node) = root { - match value.cmp(&node.value) { - Ordering::Equal => return Err(()), - Ordering::Less => root = &mut node.left, - Ordering::Greater => root = &mut node.right, - } - } - *root = Some(Box::new(Node::new(value))); + fn size(&self) -> usize { + self.size + } - Ok(()) + fn is_empty(&self) -> bool { + self.size == 0 } - fn recursive_insert(&mut self, value: T) -> Result<(), ()> { - match value.cmp(&self.value) { - Ordering::Equal => Err(()), - Ordering::Less => match self.left { - None => { - self.left = Some(Box::from(Node::new(value))); - Ok(()) - } - Some(ref mut node) => node.recursive_insert(value), - }, - Ordering::Greater => match self.right { - None => { - self.right = Some(Box::from(Node::new(value))); - Ok(()) - } - Some(ref mut node) => node.recursive_insert(value), - }, - } + fn is_not_empty(&self) -> bool { + self.size != 0 } - fn iterative_contains(mut root: &HeapNode, value: &T) -> bool { - while let Some(current) = root { - match value.cmp(¤t.value) { - Ordering::Equal => return true, - Ordering::Less => root = ¤t.left, - Ordering::Greater => root = ¤t.right, - } + fn insert(&mut self, value: T) { + if Node::iterative_insert(&mut self.root, value).is_ok() { + self.size += 1; } + } - false + fn contains(&self, value: &T) -> bool { + Node::iterative_contains(&self.root, value) } - fn recursive_contains(&self, value: &T) -> bool { - match value.cmp(&self.value) { - Ordering::Equal => true, - Ordering::Less => match self.left { - None => false, - Some(ref node) => node.recursive_contains(value), - }, - Ordering::Greater => match self.right { - None => false, - Some(ref node) => node.recursive_contains(value), - }, + fn remove(&mut self, value: &T) { + if Node::iterative_remove(&mut self.root, value).is_ok() { + self.size -= 1; } } - fn iterative_retrieve<'a>(mut root: &'a HeapNode, value: &T) -> Option<&'a T> { - while let Some(current) = root { - match value.cmp(¤t.value) { - Ordering::Equal => return Some(¤t.value), - Ordering::Less => root = ¤t.left, - Ordering::Greater => root = ¤t.right, - } - } + fn retrieve(&self, value: &T) -> Option<&T> { + Node::iterative_retrieve(&self.root, value) + } - None + fn retrieve_as_mut(&mut self, value: &T) -> Option<&mut T> { + Node::iterative_retrieve_as_mut(&mut self.root, value) } - fn recursive_retrieve(&self, value: &T) -> Option<&T> { - match value.cmp(&self.value) { - Ordering::Equal => Some(&self.value), - Ordering::Less => match self.left { - None => None, - Some(ref node) => node.recursive_retrieve(value), - }, - Ordering::Greater => match self.right { - None => None, - Some(ref node) => node.recursive_retrieve(value), - }, - } + fn min(&self) -> Option<&T> { + Node::iterative_min(&self.root) } - fn iterative_retrieve_as_mut<'a>( - mut root: &'a mut HeapNode, - value: &T, - ) -> Option<&'a mut T> { - while let Some(current) = root { - match value.cmp(¤t.value) { - Ordering::Equal => return Some(&mut current.value), - Ordering::Less => root = &mut current.left, - Ordering::Greater => root = &mut current.right, - } - } + fn max(&self) -> Option<&T> { + Node::iterative_max(&self.root) + } - None + fn remove_min(&mut self) -> Option { + let removed_min = Node::iterative_remove_min(&mut self.root); + if removed_min.is_some() { + self.size -= 1; + } + removed_min } - fn recursive_retrieve_as_mut(&mut self, value: &T) -> Option<&mut T> { - match value.cmp(&self.value) { - Ordering::Equal => Some(&mut self.value), - Ordering::Less => match self.left { - None => None, - Some(ref mut node) => node.recursive_retrieve_as_mut(value), - }, - Ordering::Greater => match self.right { - None => None, - Some(ref mut node) => node.recursive_retrieve_as_mut(value), - }, + fn remove_max(&mut self) -> Option { + let removed_max = Node::iterative_remove_max(&mut self.root); + if removed_max.is_some() { + self.size -= 1; } + removed_max } - fn iterative_remove(mut root: &mut HeapNode, value: &T) -> Result<(), ()> { - while let Some(ref mut current) = root { - match value.cmp(¤t.value) { - Ordering::Less => root = &mut root.as_mut().unwrap().left, - Ordering::Greater => root = &mut root.as_mut().unwrap().right, - Ordering::Equal => { - match (current.left.as_mut(), current.right.as_mut()) { - (None, None) => *root = None, - (Some(_), None) => *root = current.left.take(), - (None, Some(_)) => *root = current.right.take(), - (Some(_), Some(_)) => { - root.as_mut().unwrap().value = - Node::recursive_remove_min(&mut current.right).unwrap() - } - } + fn sorted_vec(&self) -> Vec<&T> { + Node::iterative_in_order_vec(&self.root) + } - return Ok(()); - } - } - } + fn into_sorted_vec(self) -> Vec { + Node::iterative_consume_in_order_vec(self.root) + } - Err(()) + fn pre_order_vec(&self) -> Vec<&T> { + Node::iterative_pre_order_vec(&self.root) } - fn recursive_remove(root: &mut HeapNode, value: &T) -> Result<(), ()> { - if let Some(ref mut node) = root { - return match value.cmp(&node.value) { - Ordering::Less => Node::recursive_remove(&mut node.left, value), - Ordering::Greater => Node::recursive_remove(&mut node.right, value), - Ordering::Equal => { - match (&node.left, &node.right) { - (None, None) => *root = None, - (Some(_), None) => *root = node.left.take(), - (None, Some(_)) => *root = node.right.take(), - (Some(_), Some(_)) => { - node.value = Node::recursive_remove_min(&mut node.right).unwrap() - } - } + fn in_order_vec(&self) -> Vec<&T> { + Node::iterative_in_order_vec(&self.root) + } - Ok(()) - } - }; - } + fn post_order_vec(&self) -> Vec<&T> { + Node::iterative_post_order_vec(&self.root) + } - Err(()) + fn pre_order_iter(&self) -> IntoIter<&T> { + Node::iterative_pre_order_vec(&self.root).into_iter() } - fn iterative_min(mut root: &HeapNode) -> Option<&T> { - while let Some(current) = root { - if current.left.is_none() { - return Some(¤t.value); - } - root = ¤t.left; - } + fn in_order_iter(&self) -> IntoIter<&T> { + Node::iterative_in_order_vec(&self.root).into_iter() + } - None + fn post_order_iter(&self) -> IntoIter<&T> { + Node::iterative_post_order_vec(&self.root).into_iter() } - fn recursive_min(&self) -> Option<&T> { - match &self.left { - None => Some(&self.value), - Some(node) => node.recursive_min(), - } + fn into_pre_order_iter(self) -> IntoIter { + Node::iterative_consume_pre_order_vec(self.root).into_iter() } - fn iterative_max(mut root: &HeapNode) -> Option<&T> { - while let Some(current) = root { - if current.right.is_none() { - return Some(¤t.value); - } - root = ¤t.right; - } - - None + fn into_in_order_iter(self) -> IntoIter { + Node::iterative_consume_in_order_vec(self.root).into_iter() } - fn recursive_max(&self) -> Option<&T> { - match &self.right { - None => Some(&self.value), - Some(node) => node.recursive_max(), - } + fn into_post_order_iter(self) -> IntoIter { + Node::iterative_consume_post_order_vec(self.root).into_iter() } +} - fn iterative_remove_min(mut root: &mut HeapNode) -> Option { - if root.is_some() { - while root.as_ref().unwrap().left.is_some() { - root = &mut root.as_mut().unwrap().left - } - - let node = root.take().unwrap(); - *root = node.right; - return Some(node.value); +impl BinarySearchTree for RecursiveBST { + fn new() -> RecursiveBST { + RecursiveBST { + root: None, + size: 0, } - - None } - fn recursive_remove_min(root: &mut HeapNode) -> Option { - if root.as_ref().unwrap().left.is_some() { - Node::recursive_remove_min(&mut root.as_mut().unwrap().left) - } else { - let node = root.take().unwrap(); - *root = node.right; - Some(node.value) - } + fn size(&self) -> usize { + self.size } - fn iterative_remove_max(mut root: &mut HeapNode) -> Option { - if root.is_some() { - while root.as_ref().unwrap().right.is_some() { - root = &mut root.as_mut().unwrap().right - } - - let node = root.take().unwrap(); - *root = node.left; - return Some(node.value); - } - - None + fn is_empty(&self) -> bool { + self.size == 0 } - fn recursive_remove_max(root: &mut HeapNode) -> Option { - if root.as_ref().unwrap().right.is_some() { - Node::recursive_remove_max(&mut root.as_mut().unwrap().right) - } else { - let node = root.take().unwrap(); - *root = node.left; - Some(node.value) - } + fn is_not_empty(&self) -> bool { + self.size != 0 } - fn iterative_pre_order_vec(node: &HeapNode) -> Vec<&T> { - let mut elements = Vec::new(); - let mut stack = vec![node.as_ref()]; - - while let Some(current) = stack.pop().unwrap_or(None) { - elements.push(¤t.value); - if current.right.is_some() { - stack.push(current.right.as_ref()); + fn insert(&mut self, value: T) { + match self.root { + None => { + self.root = Some(Box::from(Node::new(value))); + self.size += 1; } - if current.left.is_some() { - stack.push(current.left.as_ref()); + Some(ref mut node) => { + if node.recursive_insert(value).is_ok() { + self.size += 1; + } } } - - elements } - fn recursive_pre_order_vec<'a>(node: &'a HeapNode, elements: &mut Vec<&'a T>) { - if let Some(ref node) = node { - elements.push(&node.value); - Node::recursive_pre_order_vec(&node.left, elements); - Node::recursive_pre_order_vec(&node.right, elements); + fn contains(&self, value: &T) -> bool { + match self.root { + None => false, + Some(ref node) => node.recursive_contains(value), } } - fn iterative_in_order_vec(mut root: &HeapNode) -> Vec<&T> { - let mut elements = Vec::new(); - let mut stack = Vec::new(); - - while !stack.is_empty() || root.is_some() { - if root.is_some() { - stack.push(root); - root = &root.as_ref().unwrap().left; - } else { - let node = stack.pop().unwrap(); - elements.push(&node.as_ref().unwrap().value); - root = &node.as_ref().unwrap().right; - } + fn remove(&mut self, value: &T) { + if Node::recursive_remove(&mut self.root, value).is_ok() { + self.size -= 1; } - - elements } - fn recursive_in_order_vec<'a>(node: &'a HeapNode, elements: &mut Vec<&'a T>) { - if let Some(ref node) = node { - Node::recursive_in_order_vec(&node.left, elements); - elements.push(&node.value); - Node::recursive_in_order_vec(&node.right, elements); + fn retrieve(&self, value: &T) -> Option<&T> { + match self.root { + None => None, + Some(ref node) => node.recursive_retrieve(value), } } - fn iterative_post_order_vec(root: &HeapNode) -> Vec<&T> { - let mut elements = Vec::new(); - let mut stack_one = vec![root]; - let mut stack_two = vec![]; - - while let Some(node) = stack_one.pop().unwrap_or(&None) { - if node.left.is_some() { - stack_one.push(&node.left); - } - if node.right.is_some() { - stack_one.push(&node.right); - } - stack_two.push(node); + fn retrieve_as_mut(&mut self, value: &T) -> Option<&mut T> { + match self.root { + None => None, + Some(ref mut node) => node.recursive_retrieve_as_mut(value), } + } - while let Some(node) = stack_two.pop() { - elements.push(&node.value); + fn min(&self) -> Option<&T> { + match self.root { + None => None, + Some(ref node) => node.recursive_min(), } - - elements } - fn recursive_post_order_vec<'a>(node: &'a HeapNode, elements: &mut Vec<&'a T>) { - if let Some(ref node) = node { - Node::recursive_post_order_vec(&node.left, elements); - Node::recursive_post_order_vec(&node.right, elements); - elements.push(&node.value); + fn max(&self) -> Option<&T> { + match self.root { + None => None, + Some(ref node) => node.recursive_max(), } } - fn iterative_consume_pre_order_vec(node: HeapNode) -> Vec { - let mut elements = Vec::new(); - let mut stack = vec![node]; + fn remove_min(&mut self) -> Option { + let removed_min = match self.root { + None => None, + Some(_) => Node::recursive_remove_min(&mut self.root), + }; - while let Some(current) = stack.pop().unwrap_or(None) { - elements.push(current.value); - if current.right.is_some() { - stack.push(current.right); - } - if current.left.is_some() { - stack.push(current.left); - } + if removed_min.is_some() { + self.size -= 1; } - elements - } - - fn recursive_consume_pre_order_vec(node: HeapNode, elements: &mut Vec) { - if let Some(node) = node { - elements.push(node.value); - Node::recursive_consume_pre_order_vec(node.left, elements); - Node::recursive_consume_pre_order_vec(node.right, elements); - } + removed_min } - fn iterative_consume_in_order_vec(root: HeapNode) -> Vec { - let mut elements = Vec::new(); - let mut stack = vec![root]; + fn remove_max(&mut self) -> Option { + let removed_max = match self.root { + None => None, + Some(_) => Node::recursive_remove_max(&mut self.root), + }; - while !stack.is_empty() { - if let Some(mut current) = stack.pop().unwrap() { - if current.left.is_some() { - let left_node = current.left.take(); - stack.push(Some(current)); - stack.push(left_node); - } else { - let right_node = current.right.take(); - elements.push(current.value); - stack.push(right_node); - } - } + if removed_max.is_some() { + self.size -= 1; } - elements + removed_max } - fn recursive_consume_in_order_vec(node: HeapNode, elements: &mut Vec) { - if let Some(node) = node { - Node::recursive_consume_in_order_vec(node.left, elements); - elements.push(node.value); - Node::recursive_consume_in_order_vec(node.right, elements); - } + fn sorted_vec(&self) -> Vec<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::recursive_in_order_vec(&self.root, &mut elements); + elements } - fn iterative_consume_post_order_vec(root: HeapNode) -> Vec { + fn into_sorted_vec(self) -> Vec { let mut elements = Vec::new(); - let mut stack_one = vec![root]; - let mut stack_two = vec![]; - - while let Some(mut node) = stack_one.pop().unwrap_or(None) { - if let Some(left_node) = node.left.take() { - stack_one.push(Some(left_node)); - } - if let Some(right_node) = node.right.take() { - stack_one.push(Some(right_node)); - } - stack_two.push(node); - } - - while let Some(node) = stack_two.pop() { - elements.push(node.value); - } + Node::recursive_consume_in_order_vec(self.root, &mut elements); + elements + } + fn pre_order_vec(&self) -> Vec<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::recursive_pre_order_vec(&self.root, &mut elements); elements } - fn recursive_consume_post_order_vec(node: HeapNode, elements: &mut Vec) { - if let Some(node) = node { - Node::recursive_consume_post_order_vec(node.left, elements); - Node::recursive_consume_post_order_vec(node.right, elements); - elements.push(node.value); - } + fn in_order_vec(&self) -> Vec<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::recursive_in_order_vec(&self.root, &mut elements); + elements } -} -impl BinarySearchTree for IterativeBST { - fn new() -> IterativeBST { - IterativeBST { - root: None, - size: 0, - } + fn post_order_vec(&self) -> Vec<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::recursive_post_order_vec(&self.root, &mut elements); + elements } - fn size(&self) -> usize { - self.size + fn pre_order_iter(&self) -> IntoIter<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::recursive_pre_order_vec(&self.root, &mut elements); + elements.into_iter() } - fn is_empty(&self) -> bool { - self.size == 0 + fn in_order_iter(&self) -> IntoIter<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::recursive_in_order_vec(&self.root, &mut elements); + elements.into_iter() } - fn insert(&mut self, value: T) { - if Node::iterative_insert(&mut self.root, value).is_ok() { - self.size += 1; - } + fn post_order_iter(&self) -> IntoIter<&T> { + let mut elements: Vec<&T> = Vec::new(); + Node::recursive_post_order_vec(&self.root, &mut elements); + elements.into_iter() } - fn contains(&self, value: &T) -> bool { - Node::iterative_contains(&self.root, value) + fn into_pre_order_iter(self) -> IntoIter { + let mut elements = Vec::new(); + Node::recursive_consume_pre_order_vec(self.root, &mut elements); + elements.into_iter() } - fn remove(&mut self, value: &T) { - if Node::iterative_remove(&mut self.root, value).is_ok() { - self.size -= 1; - } + fn into_in_order_iter(self) -> IntoIter { + let mut elements = Vec::new(); + Node::recursive_consume_in_order_vec(self.root, &mut elements); + elements.into_iter() } - fn retrieve(&self, value: &T) -> Option<&T> { - Node::iterative_retrieve(&self.root, value) + fn into_post_order_iter(self) -> IntoIter { + let mut elements = Vec::new(); + Node::recursive_consume_post_order_vec(self.root, &mut elements); + elements.into_iter() } +} - fn retrieve_as_mut(&mut self, value: &T) -> Option<&mut T> { - Node::iterative_retrieve_as_mut(&mut self.root, value) +impl Node { + fn new(value: T) -> Node { + Node { + value, + left: None, + right: None, + } } - fn min(&self) -> Option<&T> { - Node::iterative_min(&self.root) - } + fn iterative_insert(mut root: &mut HeapNode, value: T) -> Result<(), ()> { + while let Some(ref mut node) = root { + match value.cmp(&node.value) { + Ordering::Equal => return Err(()), + Ordering::Less => root = &mut node.left, + Ordering::Greater => root = &mut node.right, + } + } + *root = Some(Box::new(Node::new(value))); - fn max(&self) -> Option<&T> { - Node::iterative_max(&self.root) + Ok(()) } - fn remove_min(&mut self) -> Option { - let removed_min = Node::iterative_remove_min(&mut self.root); - if removed_min.is_some() { - self.size -= 1; + fn recursive_insert(&mut self, value: T) -> Result<(), ()> { + match value.cmp(&self.value) { + Ordering::Equal => Err(()), + Ordering::Less => match self.left { + None => { + self.left = Some(Box::from(Node::new(value))); + Ok(()) + } + Some(ref mut node) => node.recursive_insert(value), + }, + Ordering::Greater => match self.right { + None => { + self.right = Some(Box::from(Node::new(value))); + Ok(()) + } + Some(ref mut node) => node.recursive_insert(value), + }, } - removed_min } - fn remove_max(&mut self) -> Option { - let removed_max = Node::iterative_remove_max(&mut self.root); - if removed_max.is_some() { - self.size -= 1; + fn iterative_contains(mut root: &HeapNode, value: &T) -> bool { + while let Some(current) = root { + match value.cmp(¤t.value) { + Ordering::Equal => return true, + Ordering::Less => root = ¤t.left, + Ordering::Greater => root = ¤t.right, + } } - removed_max - } - fn sorted_vec(&self) -> Vec<&T> { - Node::iterative_in_order_vec(&self.root) + false } - fn into_sorted_vec(self) -> Vec { - Node::iterative_consume_in_order_vec(self.root) + fn recursive_contains(&self, value: &T) -> bool { + match value.cmp(&self.value) { + Ordering::Equal => true, + Ordering::Less => match self.left { + None => false, + Some(ref node) => node.recursive_contains(value), + }, + Ordering::Greater => match self.right { + None => false, + Some(ref node) => node.recursive_contains(value), + }, + } } - fn pre_order_vec(&self) -> Vec<&T> { - Node::iterative_pre_order_vec(&self.root) - } + fn iterative_retrieve<'a>(mut root: &'a HeapNode, value: &T) -> Option<&'a T> { + while let Some(current) = root { + match value.cmp(¤t.value) { + Ordering::Equal => return Some(¤t.value), + Ordering::Less => root = ¤t.left, + Ordering::Greater => root = ¤t.right, + } + } - fn in_order_vec(&self) -> Vec<&T> { - Node::iterative_in_order_vec(&self.root) + None } - fn post_order_vec(&self) -> Vec<&T> { - Node::iterative_post_order_vec(&self.root) + fn recursive_retrieve(&self, value: &T) -> Option<&T> { + match value.cmp(&self.value) { + Ordering::Equal => Some(&self.value), + Ordering::Less => match self.left { + None => None, + Some(ref node) => node.recursive_retrieve(value), + }, + Ordering::Greater => match self.right { + None => None, + Some(ref node) => node.recursive_retrieve(value), + }, + } } - fn pre_order_iter(&self) -> IntoIter<&T> { - Node::iterative_pre_order_vec(&self.root).into_iter() - } + fn iterative_retrieve_as_mut<'a>( + mut root: &'a mut HeapNode, + value: &T, + ) -> Option<&'a mut T> { + while let Some(current) = root { + match value.cmp(¤t.value) { + Ordering::Equal => return Some(&mut current.value), + Ordering::Less => root = &mut current.left, + Ordering::Greater => root = &mut current.right, + } + } - fn in_order_iter(&self) -> IntoIter<&T> { - Node::iterative_in_order_vec(&self.root).into_iter() + None } - fn post_order_iter(&self) -> IntoIter<&T> { - Node::iterative_post_order_vec(&self.root).into_iter() + fn recursive_retrieve_as_mut(&mut self, value: &T) -> Option<&mut T> { + match value.cmp(&self.value) { + Ordering::Equal => Some(&mut self.value), + Ordering::Less => match self.left { + None => None, + Some(ref mut node) => node.recursive_retrieve_as_mut(value), + }, + Ordering::Greater => match self.right { + None => None, + Some(ref mut node) => node.recursive_retrieve_as_mut(value), + }, + } } - fn into_pre_order_iter(self) -> IntoIter { - Node::iterative_consume_pre_order_vec(self.root).into_iter() - } + fn iterative_remove(mut root: &mut HeapNode, value: &T) -> Result<(), ()> { + while let Some(ref mut current) = root { + match value.cmp(¤t.value) { + Ordering::Less => root = &mut root.as_mut().unwrap().left, + Ordering::Greater => root = &mut root.as_mut().unwrap().right, + Ordering::Equal => { + match (current.left.as_mut(), current.right.as_mut()) { + (None, None) => *root = None, + (Some(_), None) => *root = current.left.take(), + (None, Some(_)) => *root = current.right.take(), + (Some(_), Some(_)) => { + root.as_mut().unwrap().value = + Node::recursive_remove_min(&mut current.right).unwrap() + } + } - fn into_in_order_iter(self) -> IntoIter { - Node::iterative_consume_in_order_vec(self.root).into_iter() - } + return Ok(()); + } + } + } - fn into_post_order_iter(self) -> IntoIter { - Node::iterative_consume_post_order_vec(self.root).into_iter() + Err(()) } -} -impl BinarySearchTree for RecursiveBST { - fn new() -> RecursiveBST { - RecursiveBST { - root: None, - size: 0, + fn recursive_remove(root: &mut HeapNode, value: &T) -> Result<(), ()> { + if let Some(ref mut node) = root { + return match value.cmp(&node.value) { + Ordering::Less => Node::recursive_remove(&mut node.left, value), + Ordering::Greater => Node::recursive_remove(&mut node.right, value), + Ordering::Equal => { + match (&node.left, &node.right) { + (None, None) => *root = None, + (Some(_), None) => *root = node.left.take(), + (None, Some(_)) => *root = node.right.take(), + (Some(_), Some(_)) => { + node.value = Node::recursive_remove_min(&mut node.right).unwrap() + } + } + + Ok(()) + } + }; } + + Err(()) } - fn size(&self) -> usize { - self.size + fn iterative_min(mut root: &HeapNode) -> Option<&T> { + while let Some(current) = root { + if current.left.is_none() { + return Some(¤t.value); + } + root = ¤t.left; + } + + None } - fn is_empty(&self) -> bool { - self.size == 0 + fn recursive_min(&self) -> Option<&T> { + match &self.left { + None => Some(&self.value), + Some(node) => node.recursive_min(), + } } - fn insert(&mut self, value: T) { - match self.root { - None => { - self.root = Some(Box::from(Node::new(value))); - self.size += 1; - } - Some(ref mut node) => { - if node.recursive_insert(value).is_ok() { - self.size += 1; - } + fn iterative_max(mut root: &HeapNode) -> Option<&T> { + while let Some(current) = root { + if current.right.is_none() { + return Some(¤t.value); } + root = ¤t.right; } - } - fn contains(&self, value: &T) -> bool { - match self.root { - None => false, - Some(ref node) => node.recursive_contains(value), - } + None } - fn remove(&mut self, value: &T) { - if Node::recursive_remove(&mut self.root, value).is_ok() { - self.size -= 1; + fn recursive_max(&self) -> Option<&T> { + match &self.right { + None => Some(&self.value), + Some(node) => node.recursive_max(), } } - fn retrieve(&self, value: &T) -> Option<&T> { - match self.root { - None => None, - Some(ref node) => node.recursive_retrieve(value), + fn iterative_remove_min(mut root: &mut HeapNode) -> Option { + if root.is_some() { + while root.as_ref().unwrap().left.is_some() { + root = &mut root.as_mut().unwrap().left + } + + let node = root.take().unwrap(); + *root = node.right; + return Some(node.value); } + + None } - fn retrieve_as_mut(&mut self, value: &T) -> Option<&mut T> { - match self.root { - None => None, - Some(ref mut node) => node.recursive_retrieve_as_mut(value), + fn recursive_remove_min(root: &mut HeapNode) -> Option { + if root.as_ref().unwrap().left.is_some() { + Node::recursive_remove_min(&mut root.as_mut().unwrap().left) + } else { + let node = root.take().unwrap(); + *root = node.right; + Some(node.value) } } - fn min(&self) -> Option<&T> { - match self.root { - None => None, - Some(ref node) => node.recursive_min(), + fn iterative_remove_max(mut root: &mut HeapNode) -> Option { + if root.is_some() { + while root.as_ref().unwrap().right.is_some() { + root = &mut root.as_mut().unwrap().right + } + + let node = root.take().unwrap(); + *root = node.left; + return Some(node.value); } + + None } - fn max(&self) -> Option<&T> { - match self.root { - None => None, - Some(ref node) => node.recursive_max(), + fn recursive_remove_max(root: &mut HeapNode) -> Option { + if root.as_ref().unwrap().right.is_some() { + Node::recursive_remove_max(&mut root.as_mut().unwrap().right) + } else { + let node = root.take().unwrap(); + *root = node.left; + Some(node.value) } } - fn remove_min(&mut self) -> Option { - let removed_min = match self.root { - None => None, - Some(_) => Node::recursive_remove_min(&mut self.root), - }; + fn iterative_pre_order_vec(node: &HeapNode) -> Vec<&T> { + let mut elements = Vec::new(); + let mut stack = vec![node.as_ref()]; - if removed_min.is_some() { - self.size -= 1; + while let Some(current) = stack.pop().unwrap_or(None) { + elements.push(¤t.value); + if current.right.is_some() { + stack.push(current.right.as_ref()); + } + if current.left.is_some() { + stack.push(current.left.as_ref()); + } } - removed_min + elements } - fn remove_max(&mut self) -> Option { - let removed_max = match self.root { - None => None, - Some(_) => Node::recursive_remove_max(&mut self.root), - }; + fn recursive_pre_order_vec<'a>(node: &'a HeapNode, elements: &mut Vec<&'a T>) { + if let Some(ref node) = node { + elements.push(&node.value); + Node::recursive_pre_order_vec(&node.left, elements); + Node::recursive_pre_order_vec(&node.right, elements); + } + } - if removed_max.is_some() { - self.size -= 1; + fn iterative_in_order_vec(mut root: &HeapNode) -> Vec<&T> { + let mut elements = Vec::new(); + let mut stack = Vec::new(); + + while !stack.is_empty() || root.is_some() { + if root.is_some() { + stack.push(root); + root = &root.as_ref().unwrap().left; + } else { + let node = stack.pop().unwrap(); + elements.push(&node.as_ref().unwrap().value); + root = &node.as_ref().unwrap().right; + } } - removed_max + elements } - fn sorted_vec(&self) -> Vec<&T> { - let mut elements: Vec<&T> = Vec::new(); - Node::recursive_in_order_vec(&self.root, &mut elements); - elements + fn recursive_in_order_vec<'a>(node: &'a HeapNode, elements: &mut Vec<&'a T>) { + if let Some(ref node) = node { + Node::recursive_in_order_vec(&node.left, elements); + elements.push(&node.value); + Node::recursive_in_order_vec(&node.right, elements); + } } - fn into_sorted_vec(self) -> Vec { + fn iterative_post_order_vec(root: &HeapNode) -> Vec<&T> { let mut elements = Vec::new(); - Node::recursive_consume_in_order_vec(self.root, &mut elements); - elements - } + let mut stack_one = vec![root]; + let mut stack_two = vec![]; + + while let Some(node) = stack_one.pop().unwrap_or(&None) { + if node.left.is_some() { + stack_one.push(&node.left); + } + if node.right.is_some() { + stack_one.push(&node.right); + } + stack_two.push(node); + } + + while let Some(node) = stack_two.pop() { + elements.push(&node.value); + } - fn pre_order_vec(&self) -> Vec<&T> { - let mut elements: Vec<&T> = Vec::new(); - Node::recursive_pre_order_vec(&self.root, &mut elements); elements } - fn in_order_vec(&self) -> Vec<&T> { - let mut elements: Vec<&T> = Vec::new(); - Node::recursive_in_order_vec(&self.root, &mut elements); - elements + fn recursive_post_order_vec<'a>(node: &'a HeapNode, elements: &mut Vec<&'a T>) { + if let Some(ref node) = node { + Node::recursive_post_order_vec(&node.left, elements); + Node::recursive_post_order_vec(&node.right, elements); + elements.push(&node.value); + } } - fn post_order_vec(&self) -> Vec<&T> { - let mut elements: Vec<&T> = Vec::new(); - Node::recursive_post_order_vec(&self.root, &mut elements); + fn iterative_consume_pre_order_vec(node: HeapNode) -> Vec { + let mut elements = Vec::new(); + let mut stack = vec![node]; + + while let Some(current) = stack.pop().unwrap_or(None) { + elements.push(current.value); + if current.right.is_some() { + stack.push(current.right); + } + if current.left.is_some() { + stack.push(current.left); + } + } + elements } - fn pre_order_iter(&self) -> IntoIter<&T> { - let mut elements: Vec<&T> = Vec::new(); - Node::recursive_pre_order_vec(&self.root, &mut elements); - elements.into_iter() + fn recursive_consume_pre_order_vec(node: HeapNode, elements: &mut Vec) { + if let Some(node) = node { + elements.push(node.value); + Node::recursive_consume_pre_order_vec(node.left, elements); + Node::recursive_consume_pre_order_vec(node.right, elements); + } } - fn in_order_iter(&self) -> IntoIter<&T> { - let mut elements: Vec<&T> = Vec::new(); - Node::recursive_in_order_vec(&self.root, &mut elements); - elements.into_iter() - } + fn iterative_consume_in_order_vec(root: HeapNode) -> Vec { + let mut elements = Vec::new(); + let mut stack = vec![root]; - fn post_order_iter(&self) -> IntoIter<&T> { - let mut elements: Vec<&T> = Vec::new(); - Node::recursive_post_order_vec(&self.root, &mut elements); - elements.into_iter() + while !stack.is_empty() { + if let Some(mut current) = stack.pop().unwrap() { + if current.left.is_some() { + let left_node = current.left.take(); + stack.push(Some(current)); + stack.push(left_node); + } else { + let right_node = current.right.take(); + elements.push(current.value); + stack.push(right_node); + } + } + } + + elements } - fn into_pre_order_iter(self) -> IntoIter { - let mut elements = Vec::new(); - Node::recursive_consume_pre_order_vec(self.root, &mut elements); - elements.into_iter() + fn recursive_consume_in_order_vec(node: HeapNode, elements: &mut Vec) { + if let Some(node) = node { + Node::recursive_consume_in_order_vec(node.left, elements); + elements.push(node.value); + Node::recursive_consume_in_order_vec(node.right, elements); + } } - fn into_in_order_iter(self) -> IntoIter { + fn iterative_consume_post_order_vec(root: HeapNode) -> Vec { let mut elements = Vec::new(); - Node::recursive_consume_in_order_vec(self.root, &mut elements); - elements.into_iter() + let mut stack_one = vec![root]; + let mut stack_two = vec![]; + + while let Some(mut node) = stack_one.pop().unwrap_or(None) { + if let Some(left_node) = node.left.take() { + stack_one.push(Some(left_node)); + } + if let Some(right_node) = node.right.take() { + stack_one.push(Some(right_node)); + } + stack_two.push(node); + } + + while let Some(node) = stack_two.pop() { + elements.push(node.value); + } + + elements } - fn into_post_order_iter(self) -> IntoIter { - let mut elements = Vec::new(); - Node::recursive_consume_post_order_vec(self.root, &mut elements); - elements.into_iter() + fn recursive_consume_post_order_vec(node: HeapNode, elements: &mut Vec) { + if let Some(node) = node { + Node::recursive_consume_post_order_vec(node.left, elements); + Node::recursive_consume_post_order_vec(node.right, elements); + elements.push(node.value); + } } } \ No newline at end of file diff --git a/tests/iterative_bst.rs b/tests/iterative_bst.rs index f56afce..9db43b5 100644 --- a/tests/iterative_bst.rs +++ b/tests/iterative_bst.rs @@ -30,6 +30,15 @@ fn check_if_bst_is_empty() { assert!(!bst.is_empty()); } +#[test] +fn check_if_bst_is_not_empty() { + let mut bst = IterativeBST::new(); + assert!(!bst.is_not_empty()); + + bst.insert(1); + assert!(bst.is_not_empty()); +} + #[test] fn check_if_bst_contains_elements() { let mut bst = IterativeBST::new();