WIP iterative_consume_post_order_vec()

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

@ -474,44 +474,44 @@ impl<T: Ord> Node<T> {
} }
} }
fn iterative_post_order_vec(node: &HeapNode<T>) -> Vec<&T> { fn iterative_post_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();
loop { while !stack.is_empty() || root.is_some() {
while let Some(current) = root { match root {
if current.right.is_some() { Some(node) => {
stack.push(current.right.as_ref().unwrap()); stack.push(root);
} root = &node.left
stack.push(root.as_ref().unwrap());
root = &current.left;
}
if stack.is_empty() {
break;
} }
None => {
let mut node = Node::right_node(&stack);
match node {
Some(_) => root = node,
None => {
node = stack.pop().unwrap();
elements.push(&node.as_ref().unwrap().value);
if let Some(current) = stack.pop() { while !stack.is_empty()
if !stack.is_empty() && node.as_ref().unwrap().value
&& current.right.is_some() == Node::right_node(&stack).as_ref().unwrap().value
&& stack.last().unwrap().value == current.right.as_ref().unwrap().value
{ {
stack.pop(); node = stack.pop().unwrap();
stack.push(current); elements.push(&node.as_ref().unwrap().value);
root = &current.right; }
} else { }
elements.push(&current.value); }
root = &None;
} }
} else {
break;
} }
} }
elements elements
} }
fn right_node<'a>(stack: &[&'a HeapNode<T>]) -> &'a HeapNode<T> {
&stack.last().unwrap().as_ref().unwrap().right
}
fn recursive_post_order_vec<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) { fn recursive_post_order_vec<'a>(node: &'a HeapNode<T>, elements: &mut Vec<&'a T>) {
if let Some(ref node) = node { if let Some(ref node) = node {
Node::recursive_post_order_vec(&node.left, elements); Node::recursive_post_order_vec(&node.left, elements);
@ -574,43 +574,44 @@ impl<T: Ord> Node<T> {
} }
} }
fn iterative_consume_post_order_vec(node: HeapNode<T>) -> Vec<T> { fn iterative_consume_post_order_vec(root: HeapNode<T>) -> Vec<T> {
// let mut root = node; let mut elements = Vec::new();
// let mut elements = Vec::new(); let mut stack = vec![root];
// let mut stack = Vec::new();
// // while !stack.is_empty() {
// loop { // match stack.pop().unwrap() {
// while let Some(current) = root { // Some(mut node) => {
// if current.right.is_some() { // let left_node = node.left.take();
// stack.push(current.right.as_ref().unwrap()); // stack.push(Some(node));
// } // stack.push(left_node);
// stack.push(root.as_ref().unwrap());
// root = current.left;
// } // }
// None => {
// let mut node = stack.pop().unwrap();
// match node {
// Some(mut node) => {
// let option = node.right.take();
// stack.push(Some(node));
// stack.push(option)
// },
// None => {
// let something = node.take().unwrap();
// //
// if stack.is_empty() { // while !stack.is_empty() {
// let something_2 = stack.pop().unwrap().take().unwrap();
// if something.value == something_2.value {
// break; // break;
// } // }
// elements.push(something_2.value);
// }
// //
// if let Some(current) = stack.pop() { // elements.push(something.value);
// 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 {
// break;
// } // }
// } // }
// // }
// elements // }
unimplemented!()
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>) {
@ -727,9 +728,7 @@ impl<T: Ord> BinarySearchTree<T> for IterativeBST<T> {
} }
fn into_post_order_iter(self) -> IntoIter<T> { fn into_post_order_iter(self) -> IntoIter<T> {
let mut elements = Vec::new(); Node::iterative_consume_post_order_vec(self.root).into_iter()
Node::recursive_consume_post_order_vec(self.root, &mut elements);
elements.into_iter()
} }
} }

@ -388,6 +388,10 @@ fn into_in_order_iter() {
#[test] #[test]
fn into_post_order_iter() { fn into_post_order_iter() {
let another_bst: IterativeBST<i32> = IterativeBST::new();
let mut iter = another_bst.into_post_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