0

我想采用这种非递归后序二叉树遍历的伪代码算法,并将其实际实现到代码中。本质上,我假设创建两个并行堆栈,一个用于保存对节点的引用,另一个用于保存整数值 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
4

1 回答 1

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()+" ");

}
于 2014-11-08T15:38:47.687 回答