【问题标题】:What is the problem with this InOrder Traversal of Binary Tree?这个二叉树的InOrder遍历有什么问题?
【发布时间】:2020-09-01 17:11:07
【问题描述】:

我使用 java 实现了 BinaryTree 并尝试实现 InOrder Traversal。在这种情况下,我在副本上干运行代码它运行良好,但是当我在我的 IDE 上运行它时,我得到了无限循环。但为什么? 请帮忙。

 class BinaryTree{
    
    class Node{
    
            private int data;
            private Node left, right;
            public Node(int data)
            {
                this.data = data;
                left=null;
                right=null;}
}
    public void inOrderTraversal(Node root)
            {
                Stack<Node> stack = new Stack<>();
                Node temp = root;
                stack.push(root);
                while(!stack.isEmpty())
                {
                    temp = stack.peek();
                    if(temp.left!=null)
                    {
                        stack.push(temp.left);
                    }
                    else
                    {
                        temp = stack.pop();
                        System.out.print(temp.data+" ");
                        if(temp.right!=null)
                        {
                            stack.push(temp.right);
                        }
                    }
                }
            }
    
    public static void main(String[] args) {
    
            Node one = new Node(1);
            Node two = new Node (2);
            Node three = new Node(3);
            Node four = new Node(4);
            Node five = new Node(5);
            Node six = new Node(6);
            one.left = two;
            one.right = three;
            two.left = four;
            two.right = five;
            three.left = six
    
            BinaryTrees bn = new BinaryTrees();
            bn.inOrderTraversal(one);
        }
}

【问题讨论】:

  • 什么是Node?当你的函数被调用时,Node root 的值是多少?
  • 这里的节点是我为树和数据定义左、右指针的内部类,根是树的根(第一个节点)。
  • 请在您的问题中提供Node 的定义,因为它高度相关。此外,如果 Node root 在函数被调用时恰好指向自己,那么它将递归地继续将自己压入堆栈,您的代码将永远循环。
  • 太棒了!我会检查您的代码并回复您。
  • 您有机会查看我的回答吗?

标签: java data-structures binary-tree inorder


【解决方案1】:

您的代码以Node root 开头,等于oneone 左边是twotwo 左边是four。在采用else 条件之前,您的遍历将two 然后four 推入堆栈。然后你popfour,由于four 右侧没有任何内容,你的while 循环重新开始。但是现在堆栈的顶部是twotwo 的左侧仍然是 four,因此您将 four 压入堆栈,从而开始无限循环。

您需要一种方法来指示已访问过的节点。如果您确实必须使用堆栈,则应在 Node 类中添加一个新属性,例如 private boolean visited,并将其初始化为 false。在每个temp = stack.pop() 之后,您需要设置temp.visited = True,并且只将未访问的节点推送到堆栈上。比如这样:

class Node {
    private int data;
    private Node left, right;
    private boolean visited;
    public Node(int data) {
        this.data = data;
        left = null;
        right = null;
        visited = false;
    }
}

public void inOrderTraversal(Node root) {
    Stack<Node> stack = new Stack<>();
    Node temp = root;
    stack.push(root);
    while(!stack.isEmpty()) {
        temp = stack.peek();
        if(temp.left != null && !temp.left.visited) {
            stack.push(temp.left);
        } 
        else {
            temp = stack.pop();
            temp.visited = true;
            System.out.print(temp.data + " ");
            
            if(temp.right != null && !temp.right.visited) {
                stack.push(temp.right);
            }
        }
    }
}

一个更简单的解决方案是使用递归:

public void inOrderTraveralRecursive(Node node) {
    if (node == null) {
        return;
    }
    inOrderTraveralRecursive(node.left);
    System.out.print(node.data + " ");
    inOrderTraveralRecursive(node.right);
}

【讨论】:

  • 或者在压栈时,检查是否temp == temp.left?
  • temp == temp.left 仅在它们是同一个对象时才有效。在示例中,OP 提供的这种条件不会解决无限循环。适当的解决方案是使用递归。
  • @h0r53 先生,我的 Post OrderTraversal 也有同样的问题。如果我在一次遍历中标记访问过每个节点,那么在其他遍历中它们将不会被打印。能否请您给我一个具体的解决方案。但是,如果我只在程序中使用 InOrder,则此解决方案有效
【解决方案2】:

为了解决上述问题,我们可以在这里使用队列和堆栈来实现。

public void inOrderTraversal(Node root) {
        Stack<Node> stack = new Stack<>();
        Queue<Node> out = new LinkedList<>();
        Node temp = root;
        stack.push(root);
        while (!stack.isEmpty()) {
            temp = stack.peek();
            if (temp.left != null && !temp.left.visited) {
                stack.push(temp.left);
            } else {
                temp = stack.pop();
                temp.visited = true;
                out.offer(temp);

                if (temp.right != null && !temp.right.visited) {
                    stack.push(temp.right);
                }
            }
        }
        while(!out.isEmpty())
        {
            Node tempo = out.poll();
            System.out.print(tempo.data+" ");
            tempo.visited=false;
        }
    }

这是正确的解决方案。

【讨论】:

  • 在这里使用“正确”这个词要小心。最优化的解决方案是使用递归。我提供的递归示例可以通过简单地将 print 语句向上或向下移动一行来修改以实现 pre、post 和 in order 的遍历。
猜你喜欢
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2022-11-11
  • 2017-08-01
相关资源
最近更新 更多