Add Common Trait Impls + Improve Tests

pull/3/head
sgoudham 3 years ago
parent 54ca04838c
commit f2ce7fc235
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -1,22 +1,36 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::vec::IntoIter; use std::vec::IntoIter;
#[derive(Debug)] #[derive(Debug, Eq)]
pub struct BinarySearchTree<T: Ord> { pub struct BinarySearchTree<T: Ord> {
root: Child<T>, root: HeapNode<T>,
size: usize,
} }
type Child<T> = Option<Box<Node<T>>>; #[derive(Debug, Eq)]
#[derive(Debug)]
struct Node<T: Ord> { struct Node<T: Ord> {
value: T, value: T,
left: Child<T>, left: HeapNode<T>,
right: Child<T>, right: HeapNode<T>,
}
type HeapNode<T> = Option<Box<Node<T>>>;
impl<T: Ord + Display> Display for Node<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}
impl<T: Ord> PartialEq for Node<T> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
} }
impl<T: Ord> Node<T> { impl<T: Ord> Node<T> {
fn new(value: T) -> Self { fn new(value: T) -> Node<T> {
Node { Node {
value, value,
left: None, left: None,
@ -24,21 +38,27 @@ impl<T: Ord> Node<T> {
} }
} }
fn insert(&mut self, value: T) { fn insert(&mut self, value: T) -> bool {
match value.cmp(&self.value) { match value.cmp(&self.value) {
Ordering::Equal => {} Ordering::Equal => false,
Ordering::Less => match self.left { Ordering::Less => match self.left {
None => self.left = Some(Box::from(Node::new(value))), None => {
self.left = Some(Box::from(Node::new(value)));
true
}
Some(ref mut node) => node.insert(value), Some(ref mut node) => node.insert(value),
}, },
Ordering::Greater => match self.right { Ordering::Greater => match self.right {
None => self.right = Some(Box::from(Node::new(value))), None => {
self.right = Some(Box::from(Node::new(value)));
true
}
Some(ref mut node) => node.insert(value), Some(ref mut node) => node.insert(value),
}, },
} }
} }
fn contains(&self, value: T) -> bool { fn contains(&self, value: &T) -> bool {
match value.cmp(&self.value) { match value.cmp(&self.value) {
Ordering::Equal => true, Ordering::Equal => true,
Ordering::Less => match self.left { Ordering::Less => match self.left {
@ -80,169 +100,158 @@ impl<T: Ord> Node<T> {
} }
} }
// https://users.rust-lang.org/t/binary-search-tree-node-removal-without-unsafe-code/56078 fn remove(root: &mut HeapNode<T>, value: &T) -> bool {
fn remove(mut root: &mut Child<T>, value: T) { if let Some(ref mut node) = root {
while let Some(ref mut node) = root { return match value.cmp(&node.value) {
match node.value.cmp(&value) { Ordering::Less => Node::remove(&mut node.left, value),
Ordering::Less => root = &mut root.as_mut().unwrap().right, Ordering::Greater => Node::remove(&mut node.right, value),
Ordering::Greater => root = &mut root.as_mut().unwrap().left, Ordering::Equal => {
Ordering::Equal => match (node.left.as_mut(), node.right.as_mut()) { match (&node.left, &node.right) {
(None, None) => *root = None, (None, None) => *root = None,
(Some(_), None) => *root = node.left.take(), (Some(_), None) => *root = node.left.take(),
(None, Some(_)) => *root = node.right.take(), (None, Some(_)) => *root = node.right.take(),
(Some(_), Some(_)) => { (Some(_), Some(_)) => node.value = Node::extract_min(&mut node.right),
root.as_mut().unwrap().value = Self::extract_min(&mut node.right).unwrap()
} }
},
}
}
}
// https://users.rust-lang.org/t/binary-search-tree-node-removal-without-unsafe-code/56078 true
fn extract_min(node: &mut Child<T>) -> Option<T> { }
let mut value = None; };
}
if node.is_some() {
let mut current = node;
while current.as_ref().unwrap().left.is_some() { false
current = &mut current.as_mut().unwrap().left; }
}
let node = current.take().unwrap(); fn extract_min(root: &mut HeapNode<T>) -> T {
value = Some(node.value); if root.as_ref().unwrap().left.is_some() {
*current = node.right; Node::extract_min(&mut root.as_mut().unwrap().left)
} else {
let node = root.take().unwrap();
*root = node.right;
node.value
} }
value
} }
fn pre_order_traversal<'a>(node: &'a Child<T>, elements: &mut Vec<&'a T>) { fn pre_order_traversal<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) {
if let Some(ref node) = node { if let Some(ref node) = node {
elements.push(&node.value); elements.push(&node.value);
Self::pre_order_traversal(&node.left, elements); Node::pre_order_traversal(&node.left, elements);
Self::pre_order_traversal(&node.right, elements); Node::pre_order_traversal(&node.right, elements);
} }
} }
fn in_order_traversal<'a>(node: &'a Child<T>, elements: &mut Vec<&'a T>) { fn in_order_traversal<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) {
if let Some(ref node) = node { if let Some(ref node) = node {
Self::in_order_traversal(&node.left, elements); Node::in_order_traversal(&node.left, elements);
elements.push(&node.value); elements.push(&node.value);
Self::in_order_traversal(&node.right, elements); Node::in_order_traversal(&node.right, elements);
} }
} }
fn post_order_traversal<'a>(node: &'a Child<T>, elements: &mut Vec<&'a T>) { fn post_order_traversal<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) {
if let Some(ref node) = node { if let Some(ref node) = node {
Self::post_order_traversal(&node.left, elements); Node::post_order_traversal(&node.left, elements);
Self::post_order_traversal(&node.right, elements); Node::post_order_traversal(&node.right, elements);
elements.push(&node.value); elements.push(&node.value);
} }
} }
fn into_pre_order_traversal(node: Child<T>, elements: &mut Vec<T>) { fn into_pre_order_traversal(node: HeapNode<T>, elements: &mut Vec<T>) {
if let Some(node) = node { if let Some(node) = node {
elements.push(node.value); elements.push(node.value);
Self::into_pre_order_traversal(node.left, elements); Node::into_pre_order_traversal(node.left, elements);
Self::into_pre_order_traversal(node.right, elements); Node::into_pre_order_traversal(node.right, elements);
} }
} }
fn into_in_order_traversal(node: Child<T>, elements: &mut Vec<T>) { fn into_in_order_traversal(node: HeapNode<T>, elements: &mut Vec<T>) {
if let Some(node) = node { if let Some(node) = node {
Self::into_in_order_traversal(node.left, elements); Node::into_in_order_traversal(node.left, elements);
elements.push(node.value); elements.push(node.value);
Self::into_in_order_traversal(node.right, elements); Node::into_in_order_traversal(node.right, elements);
} }
} }
fn into_post_order_traversal(node: Child<T>, elements: &mut Vec<T>) { fn into_post_order_traversal(node: HeapNode<T>, elements: &mut Vec<T>) {
if let Some(node) = node { if let Some(node) = node {
Self::into_post_order_traversal(node.left, elements); Node::into_post_order_traversal(node.left, elements);
Self::into_post_order_traversal(node.right, elements); Node::into_post_order_traversal(node.right, elements);
elements.push(node.value); elements.push(node.value);
} }
} }
} }
impl<T: Ord + Copy> Node<T> { impl<T: Ord + Display> Display for BinarySearchTree<T> {
fn pre_order_traversal_copy(node: &Child<T>, elements: &mut Vec<T>) { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(ref node) = node { write!(f, "[")?;
elements.push(node.value);
Self::pre_order_traversal_copy(&node.left, elements); let vec = self.in_order_iter();
Self::pre_order_traversal_copy(&node.right, elements); let len = vec.len();
} for (index, elem) in vec.enumerate() {
} if index == len - 1 {
write!(f, "{}", elem)?;
fn in_order_traversal_copy(node: &Child<T>, elements: &mut Vec<T>) { } else {
if let Some(ref node) = node { write!(f, "{}, ", elem)?;
Self::in_order_traversal_copy(&node.left, elements); }
elements.push(node.value);
Self::in_order_traversal_copy(&node.right, elements);
} }
}
fn post_order_traversal_copy(node: &Child<T>, elements: &mut Vec<T>) { write!(f, "]")
if let Some(ref node) = node {
Self::post_order_traversal_copy(&node.left, elements);
Self::post_order_traversal_copy(&node.right, elements);
elements.push(node.value);
}
} }
} }
impl<T: Ord + Copy> BinarySearchTree<T> { impl<T: Ord> PartialEq for BinarySearchTree<T> {
pub fn pre_order(&self) -> Vec<T> { fn eq(&self, other: &Self) -> bool {
let mut elements: Vec<T> = Vec::new(); self.in_order() == other.in_order()
Node::pre_order_traversal_copy(&self.root, &mut elements);
elements
}
pub fn in_order(&self) -> Vec<T> {
let mut elements: Vec<T> = Vec::new();
Node::in_order_traversal_copy(&self.root, &mut elements);
elements
}
pub fn post_order(&self) -> Vec<T> {
let mut elements: Vec<T> = Vec::new();
Node::post_order_traversal_copy(&self.root, &mut elements);
elements
} }
} }
impl<T: Ord> BinarySearchTree<T> { impl<T: Ord> BinarySearchTree<T> {
pub fn empty() -> Self { pub fn empty() -> BinarySearchTree<T> {
Self { root: None } BinarySearchTree {
root: None,
size: 0,
}
} }
pub fn new(value: T) -> Self { pub fn new(value: T) -> BinarySearchTree<T> {
Self { BinarySearchTree {
root: Some(Box::from(Node::new(value))), root: Some(Box::from(Node::new(value))),
size: 1,
} }
} }
pub fn size(&self) -> usize {
self.size
}
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.root.is_none() self.root.is_none()
} }
pub fn insert(&mut self, value: T) { pub fn insert(&mut self, value: T) {
match self.root { match self.root {
None => self.root = Some(Box::from(Node::new(value))), None => {
Some(ref mut node) => node.insert(value), self.root = Some(Box::from(Node::new(value)));
self.size += 1;
}
Some(ref mut node) => {
if node.insert(value) {
self.size += 1;
}
}
} }
} }
pub fn contains(&self, value: T) -> bool { pub fn contains(&self, value: &T) -> bool {
match self.root { match self.root {
None => false, None => false,
Some(ref node) => node.contains(value), Some(ref node) => node.contains(value),
} }
} }
pub fn remove(&mut self, value: T) { pub fn remove(&mut self, value: &T) {
Node::remove(&mut self.root, value); if Node::remove(&mut self.root, value) {
self.size -= 1;
}
} }
pub fn retrieve(&self, value: T) -> Option<&T> { pub fn retrieve(&self, value: T) -> Option<&T> {
@ -259,6 +268,24 @@ impl<T: Ord> BinarySearchTree<T> {
} }
} }
pub fn pre_order(&self) -> Vec<&T> {
let mut elements: Vec<&T> = Vec::new();
Node::pre_order_traversal(&self.root, &mut elements);
elements
}
pub fn in_order(&self) -> Vec<&T> {
let mut elements: Vec<&T> = Vec::new();
Node::in_order_traversal(&self.root, &mut elements);
elements
}
pub fn post_order(&self) -> Vec<&T> {
let mut elements: Vec<&T> = Vec::new();
Node::post_order_traversal(&self.root, &mut elements);
elements
}
pub fn pre_order_iter(&self) -> IntoIter<&T> { pub fn pre_order_iter(&self) -> IntoIter<&T> {
let mut elements: Vec<&T> = Vec::new(); let mut elements: Vec<&T> = Vec::new();
Node::pre_order_traversal(&self.root, &mut elements); Node::pre_order_traversal(&self.root, &mut elements);
@ -327,10 +354,7 @@ mod bst_test {
bst.insert(1); bst.insert(1);
bst.insert(2); bst.insert(2);
assert_eq!(bst.retrieve(-1), Some(&-1)); assert_eq!(bst.size(), 4);
assert_eq!(bst.retrieve(0), Some(&0));
assert_eq!(bst.retrieve(1), Some(&1));
assert_eq!(bst.retrieve(2), Some(&2));
} }
#[test] #[test]
@ -349,9 +373,9 @@ mod bst_test {
bst.insert(1); bst.insert(1);
bst.insert(5); bst.insert(5);
assert!(!bst.contains(10)); assert!(!bst.contains(&10));
assert!(bst.contains(1)); assert!(bst.contains(&1));
assert!(bst.contains(5)); assert!(bst.contains(&5));
} }
#[test] #[test]
@ -359,83 +383,112 @@ mod bst_test {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(0); bst.insert(0);
bst.remove(0); assert!(!bst.is_empty());
assert_eq!(bst.size(), 1);
bst.remove(&0);
assert!(bst.is_empty()) assert!(bst.is_empty());
assert_eq!(bst.size(), 0)
} }
#[test] #[test]
fn remove_leaf_node() { fn remove_leaf_node() {
let mut bst = BinarySearchTree::empty(); let mut expected_bst = BinarySearchTree::empty();
bst.insert(5); expected_bst.insert(5);
bst.insert(4); expected_bst.insert(4);
bst.insert(6); expected_bst.insert(6);
bst.insert(7); let mut actual_bst = BinarySearchTree::empty();
actual_bst.insert(5);
actual_bst.insert(4);
actual_bst.insert(6);
actual_bst.insert(7);
bst.remove(7); actual_bst.remove(&7);
assert!(!bst.contains(7)); assert_eq!(actual_bst.size(), 3);
assert_eq!(bst.in_order(), vec![4, 5, 6]) assert_eq!(actual_bst, expected_bst);
} }
#[test] #[test]
fn remove_single_right_node_with_children() { fn remove_single_right_node_with_children() {
let mut bst = BinarySearchTree::empty(); let mut expected_bst = BinarySearchTree::empty();
bst.insert(5); expected_bst.insert(5);
bst.insert(4); expected_bst.insert(4);
bst.insert(6); expected_bst.insert(7);
bst.insert(7); expected_bst.insert(8);
bst.insert(8); let mut actual_bst = BinarySearchTree::empty();
actual_bst.insert(5);
actual_bst.insert(4);
actual_bst.insert(6);
actual_bst.insert(7);
actual_bst.insert(8);
bst.remove(6); actual_bst.remove(&6);
assert!(!bst.contains(6)); assert_eq!(actual_bst.size(), 4);
assert_eq!(bst.in_order(), vec![4, 5, 7, 8]) assert_eq!(actual_bst, expected_bst);
} }
#[test] #[test]
fn remove_single_left_node_with_children() { fn remove_single_left_node_with_children() {
let mut bst = BinarySearchTree::empty(); let mut expected_bst = BinarySearchTree::empty();
bst.insert(5); expected_bst.insert(5);
bst.insert(4); expected_bst.insert(3);
bst.insert(6); expected_bst.insert(2);
bst.insert(3); expected_bst.insert(6);
bst.insert(2); let mut actual_bst = BinarySearchTree::empty();
actual_bst.insert(5);
actual_bst.insert(4);
actual_bst.insert(6);
actual_bst.insert(3);
actual_bst.insert(2);
bst.remove(4); actual_bst.remove(&4);
assert!(!bst.contains(4)); assert_eq!(actual_bst.size(), 4);
assert_eq!(bst.in_order(), vec![2, 3, 5, 6]) assert_eq!(actual_bst, expected_bst);
} }
#[test] #[test]
fn remove_node_with_two_children() { fn remove_node_with_two_children() {
let mut bst = BinarySearchTree::empty(); let mut expected_bst = BinarySearchTree::empty();
bst.insert(10); expected_bst.insert(10);
bst.insert(5); expected_bst.insert(3);
bst.insert(8); expected_bst.insert(8);
bst.insert(3); expected_bst.insert(15);
bst.insert(15); let mut actual_bst = BinarySearchTree::empty();
actual_bst.insert(10);
actual_bst.insert(5);
actual_bst.insert(8);
actual_bst.insert(3);
actual_bst.insert(15);
bst.remove(5); actual_bst.remove(&5);
assert!(!bst.contains(5)); assert_eq!(actual_bst, expected_bst);
assert_eq!(bst.in_order(), vec![3, 8, 10, 15])
} }
#[test] #[test]
fn does_not_fail_when_removing_non_existing_element() { fn does_not_fail_when_removing_non_existing_element() {
let mut bst = BinarySearchTree::empty(); let mut expected_bst = BinarySearchTree::empty();
bst.insert(10); expected_bst.insert(10);
bst.insert(5); expected_bst.insert(5);
bst.insert(8); expected_bst.insert(8);
bst.insert(3); expected_bst.insert(3);
bst.insert(15); expected_bst.insert(15);
bst.remove(20); let mut actual_bst = BinarySearchTree::empty();
actual_bst.insert(10);
actual_bst.insert(5);
actual_bst.insert(8);
actual_bst.insert(3);
actual_bst.insert(15);
assert!(!bst.contains(20)); actual_bst.remove(&20);
assert_eq!(bst.in_order(), vec![3, 5, 8, 10, 15])
assert_eq!(actual_bst.size(), 5);
assert_eq!(actual_bst, expected_bst);
} }
#[test] #[test]
@ -453,16 +506,18 @@ mod bst_test {
#[test] #[test]
fn retrieve_element_as_mut_and_modify_bst() { fn retrieve_element_as_mut_and_modify_bst() {
let mut bst = BinarySearchTree::empty(); let mut expected_bst = BinarySearchTree::empty();
bst.insert(10); expected_bst.insert(10);
bst.insert(5); expected_bst.insert(2);
let mut actual_bst = BinarySearchTree::empty();
actual_bst.insert(10);
actual_bst.insert(5);
let _retrieved_value_as_mut: &mut i32 = bst.retrieve_as_mut(5).unwrap(); let _retrieved_value_as_mut: &mut i32 = actual_bst.retrieve_as_mut(5).unwrap();
*_retrieved_value_as_mut = 2; *_retrieved_value_as_mut = 2;
assert!(bst.contains(10)); assert_eq!(actual_bst, expected_bst);
assert!(bst.contains(2));
assert!(!bst.contains(5));
} }
#[test] #[test]
@ -617,7 +672,7 @@ mod bst_test {
} }
#[test] #[test]
fn pre_order_traversal_copy() { fn pre_order() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -625,11 +680,11 @@ mod bst_test {
bst.insert(1); bst.insert(1);
bst.insert(2); bst.insert(2);
assert_eq!(bst.pre_order(), vec![3, 1, 2, 4, 5]); assert_eq!(bst.pre_order(), vec![&3, &1, &2, &4, &5]);
} }
#[test] #[test]
fn in_order_traversal_copy() { fn in_order() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -637,11 +692,11 @@ mod bst_test {
bst.insert(1); bst.insert(1);
bst.insert(2); bst.insert(2);
assert_eq!(bst.in_order(), vec![1, 2, 3, 4, 5]); assert_eq!(bst.in_order(), vec![&1, &2, &3, &4, &5]);
} }
#[test] #[test]
fn post_order_traversal_copy() { fn post_order() {
let mut bst = BinarySearchTree::empty(); let mut bst = BinarySearchTree::empty();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);
@ -649,6 +704,6 @@ mod bst_test {
bst.insert(1); bst.insert(1);
bst.insert(2); bst.insert(2);
assert_eq!(bst.post_order(), vec![2, 1, 5, 4, 3]); assert_eq!(bst.post_order(), vec![&2, &1, &5, &4, &3]);
} }
} }
Loading…
Cancel
Save