postorder using tail recursion
i find this link, http://www.experts-exchange.com/Programming/Algorithms/Q_25205171.html, which suggests a way to do postorder tail recursion. however, it uses 2 stacks, is there a way to do this with only one stack. thanks!
below is the java code pasted from the link above:
public static final <T> void postorder(Tree<T> root) {
Stack<Tree<T>> stack = new Stack<Tree<T>>();
S开发者_如何转开发tack<Tree<T>> traversal = new Stack<Tree<T>>();
stack.push(root);
while (!stack.isEmpty()) {
Tree<T> node = stack.pop();
if (node.right != null)
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
traversal.push(node);
}
while (!traversal.isEmpty()) {
System.out.println(traversal.pop().value.toString());
}
}
Yes, but the code needs to be structured differently. A proper stack-based simulation of a recursive algorithm should keep a node on the stack until the node and its children have been completely traversed. The stack should contain instances of a class that contains information about how many children have been traversed, say:
public class StackElement<T> {
public Tree<T> node;
public int numTraversedChildren;
}
(using public fields for simplicity). Whenever you push a node onto the stack, push a StackElement
that refers to that node and where numTraversedChildren
is 0. In the top of the loop, peek (don't pop) the stack to find the top element. If and only if numTraversedChildren == 2
, you know that all children of this node have been traversed. In that case, you can process (in your case, print) that node and then pop it. Otherwise, keep the node on the stack, increment numTraversedChildren
, and push either its left child (if the old value of numTraversedChildren
was 0) or its right child (if the old value of numTraversedChildren
was 1).
Note that when this approach is followed, the while
loop and the push/pop operations on the stack are effectively simulating function calls: a push is a call, a pop is a return, and the stack maintains all parameters and local variables for each function invocation. The element at the top of the stack always represents the function that is currently executing.
精彩评论