|
|
@ -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());
|
|
|
|
None => {
|
|
|
|
root = ¤t.left;
|
|
|
|
let mut node = Node::right_node(&stack);
|
|
|
|
}
|
|
|
|
match node {
|
|
|
|
|
|
|
|
Some(_) => root = node,
|
|
|
|
if stack.is_empty() {
|
|
|
|
None => {
|
|
|
|
break;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
{
|
|
|
|
node = stack.pop().unwrap();
|
|
|
|
stack.pop();
|
|
|
|
elements.push(&node.as_ref().unwrap().value);
|
|
|
|
stack.push(current);
|
|
|
|
}
|
|
|
|
root = ¤t.right;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
elements.push(¤t.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());
|
|
|
|
// None => {
|
|
|
|
// root = current.left;
|
|
|
|
// 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() {
|
|
|
|
// break;
|
|
|
|
// let something_2 = stack.pop().unwrap().take().unwrap();
|
|
|
|
// }
|
|
|
|
// if something.value == something_2.value {
|
|
|
|
|
|
|
|
// 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
|
|
|
|
elements
|
|
|
|
unimplemented!()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|