Implement iterative_consume_in_order_vec()

pull/3/head
sgoudham 3 years ago
parent 81aafcad77
commit 1e40b9f7d5
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -448,8 +448,7 @@ impl<T: Ord> Node<T> {
} }
} }
fn iterative_in_order_vec(node: &HeapNode<T>) -> Vec<&T> { fn iterative_in_order_vec(mut root: &HeapNode<T>) -> Vec<&T> {
let mut root = node;
let mut elements = Vec::new(); let mut elements = Vec::new();
let mut stack = Vec::new(); let mut stack = Vec::new();
@ -546,18 +545,67 @@ impl<T: Ord> Node<T> {
} }
} }
fn iterative_consume_in_order_vec(mut root: HeapNode<T>) -> Vec<T> { fn iterative_consume_in_order_vec(root: HeapNode<T>) -> Vec<T> {
let mut elements = Vec::new();
let mut stack = vec![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);
}
}
}
elements
}
fn recursive_consume_in_order_vec(node: HeapNode<T>, elements: &mut Vec<T>) {
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 iterative_consume_post_order_vec(node: HeapNode<T>) -> Vec<T> {
// let mut root = node;
// let mut elements = Vec::new(); // let mut elements = Vec::new();
// let mut stack = Vec::new(); // let mut stack = Vec::new();
// //
// while !stack.is_empty() || root.is_some() { // loop {
// if root.is_some() { // while let Some(current) = root {
// if current.right.is_some() {
// stack.push(current.right.as_ref().unwrap());
// }
// stack.push(root.as_ref().unwrap()); // stack.push(root.as_ref().unwrap());
// root = root.unwrap().left; // root = current.left;
// }
//
// if stack.is_empty() {
// break;
// }
//
// if let Some(current) = stack.pop() {
// if !stack.is_empty()
// && current.right.is_some()
// && stack.last().unwrap().value == current.right.as_ref().unwrap().value
// {
// stack.pop();
// stack.push(current);
// root = current.right;
// } else {
// elements.push(current.value);
// root = None;
// }
// } else { // } else {
// let node = stack.pop(); // break;
// elements.push(node.as_ref().unwrap().value);
// root = node.as_ref().unwrap().right;
// } // }
// } // }
// //
@ -565,14 +613,6 @@ impl<T: Ord> Node<T> {
unimplemented!() unimplemented!()
} }
fn recursive_consume_in_order_vec(node: HeapNode<T>, elements: &mut Vec<T>) {
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 recursive_consume_post_order_vec(node: HeapNode<T>, elements: &mut Vec<T>) { fn recursive_consume_post_order_vec(node: HeapNode<T>, elements: &mut Vec<T>) {
if let Some(node) = node { if let Some(node) = node {
Node::recursive_consume_post_order_vec(node.left, elements); Node::recursive_consume_post_order_vec(node.left, elements);
@ -651,9 +691,7 @@ impl<T: Ord> BinarySearchTree<T> for IterativeBST<T> {
} }
fn into_sorted_vec(self) -> Vec<T> { fn into_sorted_vec(self) -> Vec<T> {
let mut elements = Vec::new(); Node::iterative_consume_in_order_vec(self.root)
Node::recursive_consume_in_order_vec(self.root, &mut elements);
elements
} }
fn pre_order_vec(&self) -> Vec<&T> { fn pre_order_vec(&self) -> Vec<&T> {
@ -685,9 +723,7 @@ impl<T: Ord> BinarySearchTree<T> for IterativeBST<T> {
} }
fn into_in_order_iter(self) -> IntoIter<T> { fn into_in_order_iter(self) -> IntoIter<T> {
let mut elements = Vec::new(); Node::iterative_consume_in_order_vec(self.root).into_iter()
Node::recursive_consume_in_order_vec(self.root, &mut elements);
elements.into_iter()
} }
fn into_post_order_iter(self) -> IntoIter<T> { fn into_post_order_iter(self) -> IntoIter<T> {

@ -365,6 +365,10 @@ fn into_pre_order_iter() {
#[test] #[test]
fn into_in_order_iter() { fn into_in_order_iter() {
let another_bst: IterativeBST<i32> = IterativeBST::new();
let mut iter = another_bst.into_in_order_iter();
assert_eq!(iter.next(), None);
let mut bst = IterativeBST::new(); let mut bst = IterativeBST::new();
bst.insert(3); bst.insert(3);
bst.insert(4); bst.insert(4);

Loading…
Cancel
Save