使用并行堆栈的NonRecursive PostOrder二叉树遍历

时间:2012-04-19 23:12:06

标签: java traversal postorder

我想采用这种非递归后序二叉树遍历的pseucode-ish算法并实际将其实现为代码。本质上我假设创建两个并行堆栈,一个用于保存对节点的引用,另一个用于保存整数值1或2,以判断是否已访问左子树或右子树。我根据他们给我的算法创建了算法,但由于某种原因它只打印了一些数字而不是正确的顺序,我觉得我解释它应该是它应该是但它刚刚工作,任何帮助都会很好。

他们希望我做什么:

1.create stack(s)
2. current = root;
3.v = 0;
4. if (current is null)
    the binary tree is empty
5. if (current is not null)
    a. push current onto stack;
    b. push 1 onto stack;
    c. current = current.lLink;
6. while (stack is not empty)
      if (current is not null and v is 0)
      {
          push current and 1 onto stack;
          current = current.lLink
      }
      else
      {
          pop stack into current and v;
          if ( v == 1)
          {
              push current and 2 onto stack;
              current = current.rLink;
              v = 0;
          }
          else
              visit current;
      }

以下是我对它的实现:

public void nonRecursivePostTraversal()
{
    LinkedStackClass<BinaryTreeNode<T> > stack
    = new LinkedStackClass<BinaryTreeNode<T> >();//create stack

    //create parallel stack of integers
    LinkedStackClass<Integer> stackInt = new LinkedStackClass<Integer>();

    BinaryTreeNode<T> current;
    current = root;

    Integer v = 0;

    if (current == null)
        stack = null;

    if   (current != null)
    {
        stack.push(current);
        v = 1;
        stackInt.push(v);
        current = current.lLink;
    }


    while (!stack.isEmptyStack())
        if (current != null && v == 0)
        {
            stack.push(current);
            v = 1;
            stackInt.push(v);
            current = current.lLink;

        }
        else
        {
            current = (BinaryTreeNode<T>) stack.peek();
            stack.pop();
            v = (Integer) stackInt.peek();
            stackInt.pop();
            if (v == 1)
            {
                stack.push(current);
                v = 2;
                stackInt.push(v);
                current = current.rLink;
                v = 0;
            }
            else
                System.out.print(current.info + " ");
        }

}//end alg

1 个答案:

答案 0 :(得分:0)

public static void nonRecursivePostOrder(BinaryTreeNode root){


    Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
    List<BinaryTreeNode> returnNodes = new LinkedList<BinaryTreeNode>();


      while (true) {
          if (root != null) {

              if (returnNodes.contains(root)) {
                  returnNodes.add(stack.pop());
                  root = null;
              } else {
                  stack.push(root);
                  root = root.getLeft();
              }

          } else {
              if (stack.isEmpty()) {
                  break;
              } else if (stack.peek().getRight() == null) {
                  root = stack.pop();
                  returnNodes.add(root);
                  if (root == stack.peek().getRight()) {
                      returnNodes.add(stack.pop());
                  }
              }

              if (!stack.isEmpty()) {
                  root = stack.peek().getRight();
              } else {
                  root = null;
              }
          }
      }

      for(BinaryTreeNode node : returnNodes)
          System.out.print(node.getData()+" ");

}