-1

有人可以向我解释一下递归在顺序遍历中是如何工作的。这是我的 inOrder() 方法。

public void inOrder(BinaryNode p){
        if(p.left!=null){
            inOrder(p.left);
        }

        visit(p);

        if(p.right!=null){
            inOrder(p.right);
        }


    }
    public void visit(BinaryNode p){
        System.out.println(p.element);
    }

BinaryTree t=new BinaryTree();
        t.insert(5);
        t.insert(t.root,4);
        t.insert(t.root,6);
        t.insert(t.root,60);
        t.insert(t.root,25);
        t.insert(t.root,10);
        t.inOrder(t.root);  

inOrder() 方法可以正确打印元素,但我不明白它是如何工作的。
当我打电话时,t.inOrder(t.root);因为 root 的值为 5 它会类似于inOrder(5); 并且有一个左节点所以if(p.left!=null){ inOrder(p.left); }

将被执行。那里的递归调用将是inOrder(4);
因为 4 的左边指向 null,然后visit(4)是执行打印值 4 的行。
但之后如何打印 5。虽然起初当方法被t.inOrder(t.root);局部变量调用时p 被赋值为 5 的 BinaryNode,现在 p 为 4。然后在打印出 4 之后,可以执行的下一行是

if(p.right!=null){ inOrder(p.right); }
但是由于 p.right 现在引用 BinaryNode 中元素 4 的 right 并且 4 的 right 为空,所以这也不会被执行。
那么递归是如何维护的呢?
它如何打印出 5 和其余节点?

4

2 回答 2

0

这很难说..这取决于您的实施..

我添加了按顺序遍历的实现。希望对您有所帮助

class BinaryTreeSearch{
 public enum State{
 Visited, Unvisited,Visiting;

}
//this is the Node used in the tree
  static class Node{
    private int data;
    private Node left;
    private Node right;
    public Node(int data){
        this.data = data;
        left = null;
        right = null;
    }
    public void setLeft(Node left){
        this.left = left;
    }
    public void setRight(Node right){
        this.right = right;
    }
    public Node getLeft(){
        return this.left;
    }       
    public Node getRight(){
        return this.right;
    }
    public int getData(){
        return this.data;
    }
    public boolean equals(Node n){
        if(this.data ==(int) n.getData()) return true;
        else
            return false;
    }
}
public static void main(String[] args){
    BinaryTreeSearch bts = new BinaryTreeSearch();
    bts.run();
}
//execute the test case
public void run(){
    Node root = new Node(10);
    insert(root,new Node(20));
    insert(root,new Node(5));
    insert(root,new Node(4));
    insert(root,new Node(5));
    insert(root,new Node(15));

    inOrderTraverse(root);
    System.out.println("\n" + binarySearch(root,new Node(10)));
}

// insert a node to the binary search tree
public void insert(Node root, Node n){
    if(root == null|| n == null) return;

    if(root.getData() > n.getData()){
        if(root.getLeft() == null){
            root.setLeft(n);
             System.out.println("Added node to left of "+root.getData()+" of value "+n.getData());           
        }else{
            insert(root.getLeft(),n);
        }

    }else if(root.getData() < n.getData()){
        if(root.getRight() == null){
            root.setRight(n);
            System.out.println("Added node to Right of "+root.getData()+" of value "+n.getData());     
        }else{
            insert(root.getRight(),n);
        }

    }
}
//in-order Traversal
public void inOrderTraverse(Node root){
    if(root != null){
        inOrderTraverse(root.getLeft());
        System.out.print("  "+root.getData());
        inOrderTraverse(root.getRight());
    }

}
//binary search
public boolean binarySearch(Node root,Node n){
    if(root == null || n == null) {
        return false;
    }
    System.out.println("  Testing out "+root.getData()+" for value "+n.getData());
    if(root.getData() > n.getData()){
       return  binarySearch(root.getLeft(),n);
    }else if(root.getData() < n.getData()){
       return  binarySearch(root.getRight(),n);
    }
    return true;
}
} 
于 2014-05-25T07:05:19.033 回答
0

我已经解释了没有图像我能做到的最好的事情。do print(i) 意味着打印 i 并且 do inorder(i) 意味着 inorder(i) 被扩展为 inorder(left of i) > print (i) > inorder(right of i)

inorder(5) 调用

待办事项:有序(4)>打印5>有序(6)

按顺序做(4)

待办事项:有序(4 的左侧)= 无 > 打印(4) > 有序(4 的右侧)= 无 > 打印(5)

顺序 6

打印 4

打印 5

按顺序做 6

待办事项:有序(6 的左边)= 无 > 打印 6 > 有序(60)

打印 6

按顺序做 60

待办事项:inorder(25) > print 60 > inorder(60 的右边)=nothing

按顺序执行 25

待办事项:inorder(10) > print 25 > inorder(25 右边)=nothing > print 60

按顺序做 10

待办事项:有序(10 的左边)= 无 > 打印 10 > 有序(10 的右边)= 无 > 打印 25> 打印 60

打印 10

打印 25

打印 60

所以如果你看到打印它的顺序 4 5 6 10 25 60

于 2014-05-29T15:45:55.757 回答