【问题标题】:Binary Search tree, inorder method iterative not working二叉搜索树,中序方法迭代不起作用
【发布时间】:2015-12-08 07:55:48
【问题描述】:

我目前正在大学学习数据结构课程,我们正在学习使用链表的二叉搜索树。我们已经递归地检查了 inOrder 方法,但我想尝试迭代地执行该方法。经过一番研究,我意识到我必须在遍历树时使用堆栈。我能够到达树最左侧的尽头,但是向后遍历树是我遇到麻烦的地方。我已经尝试了各种版本的代码,但最后还是出现了一个 nil 指针异常,或者打印出了问题。

public void inOrder(){                
    // implement this method using non-recursive solution
   if(m_root==null){
      return;
   }
   Stack<BSTNode> myStack= new Stack<BSTNode>();
   BSTNode current=m_root;
   while (current!= null){
      myStack.push(current);
      current=current.getLeft();
   }
   while (current!=null&& !myStack.isEmpty()){
      current=myStack.peek();
      System.out.print(current.getInfo()+" ");
      myStack.pop();
      if(current.getRight()!=null){
          myStack.push(current);
      }

   }

}

【问题讨论】:

    标签: java tree binary-search-tree


    【解决方案1】:

    据我所知,您的代码在第二个 while 循环中存在一些问题。您的想法是正确的方向,但是存在一些逻辑错误。你所拥有的条件是正确的,但不应该在一起,它们应该分开。下面的代码实现了您正在寻找的东西。

    public void inOrder(){                
        // implement this method using non-recursive solution
       if(m_root==null){
          return;
       }
       Stack<BSTNode> myStack= new Stack<BSTNode>();
       BSTNode current=m_root;
       while (current!= null){
          myStack.push(current);
          current=current.getLeft();
       }
       while (!myStack.isEmpty()){
          current=(BSTNode)myStack.pop();
          System.out.print(current.getInfo()+" ");
          if(current.getRight() != null){
             current=current.getRight();
             while (current!= null){
                myStack.push(current);
                current=current.getLeft();
             }
          }
       }
    
    }
    

    【讨论】:

      【解决方案2】:

      首先,您的代码中有两个主要缺陷:您将第一个 while 循环的 current 指向 null,因此您永远不会进入第二个 while 循环。而且,我觉得这个

      if(current.getRight()!=null){
            myStack.push(current);
        }
      

      应该使用myStack.push(current.getRight()); 进行更正,否则您会尝试一遍又一遍地推动弹出的元素,您将进入无限循环。但即便如此,探索的逻辑也是错误的,因为您永远不会走到从正确链接到达的节点的左侧。 我尝试从您的代码开始并创建一个有效的中序遍历:

      public void inOrder(){                
      
         Stack<BSTNode> myStack= new Stack<BSTNode>();
         Set<BSTNode> visited = new HashSet<BSTNode>();
         BSTNode current = m_root;
         if(current!= null)
             myStack.push(current);
         while (!myStack.isEmpty()){
            current = myStack.peek();
            if(current.getLeft()!= null && !visited.contains(current.getLeft()))
                myStack.push(current.getLeft());
            else{
                //here you explore the parent node
                System.out.print(current.getInfo()+" ");
                visited.add(current);
                myStack.pop();
                //and then you see if it has children on the right
                if(current.getRight()!=null && !visited.contains(current.getRight))
                    myStack.push(current.getRight());
      
            }
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多