2014-05-25 80 views
-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); }

会得到executed.There递归调用将inOrder(4);
自4的左点为null,则visit(4)是执行打印数值4的行。
但是之后又是如何打印的。虽然最初当方法被t.inOrder(t.root);调用时,局部变量p被分配了值为5的BinaryNode,现在p是4。然后在打印出4之后,可以执行的下一行是

if(p.right!=null){ inOrder(p.right); }
但是因为现在p.right在BinaryNode中引用了元素4的权利,而4的权利是null,所以这也不会被执行。
那么如何保持递归?
它如何打印5和其余节点?

+0

这是很难不图纸解释...也许找到一个在线视频。 ..但让我试试。我认为你的问题是你将节点与价值混淆在一起;序(根)是不太一样的序(5)...根是具有5也左右 – okaram

+0

至于它是如何实现的,通常有一个堆栈中的节点;当你调用一个函数时,参数会被推入栈中,也是返回的地方;当返回时,结果被放入堆栈,调用者的地址被取出,然后我们返回到那个地方。 – okaram

+0

@okaram:我明白inOrder(root)是一个BinaryNode作为参数。我写在order(5)中,以便我可以很容易地解释它 –

回答

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; 
} 
} 
0

我已经解释尽我所能,而不图片。 做打印(I)指印刷我 和做序(I)指序(i)中膨胀以序(左的i)>打印(I)>序(I的右侧)

序(5)称为

TODO:序(4)>打印5>序(6)

做序(4)

TODO:序(左4)=无>打印(4)>序(右的4)=无>打印(5)

序6

做打印4

做打印5

做序6

TODO:序(左的6)=无>打印6>序(60)

办打印6

办60号

TODO:序(25)>打印60>序(右的60)=没什么

做序25

TODO:序(10)>打印25>序(25右侧)=无>打印60

做序10

TODO:序(左的10)=无>打印10>序(右10)=无>打印25>打印60

不要打印10

做打印25

能打印60

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