In order recursion in binary trees

做~自己de王妃 提交于 2019-12-23 06:12:12

问题


Can someone please explain to me how recursion works in in Order traversal. here's my inOrder() method.

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);  

The method inOrder() prints the elements correctly,but I don't understand how it works.
When I call t.inOrder(t.root); since root has value 5 it would be similar to inOrder(5); and that has a left node so if(p.left!=null){ inOrder(p.left); }

would get executed.There the recursion call would be inOrder(4);
Since 4's left points to null, then visit(4) is the line that executed printing the value 4.
But then after that how does 5 get printed.Although at first when the method was called by t.inOrder(t.root); the local variable p was assigned with BinaryNode of value 5, now p is 4. Then after printing out 4, the next line that can get executed is

if(p.right!=null){ inOrder(p.right); }
But since p.right now refers to right in BinaryNode with element 4 and 4's right is null, this also won't get executed.
Then how does the recursion is maintained?
How does it print out 5 and the rest of the nodes?


回答1:


This is hard to say.. It depend on your implementation..

I added implementation with in order traverse.. Hope that helps

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



回答2:


I've explained the best i could without images. do print(i) means printing i and do inorder(i) means inorder(i) is expanded to inorder(left of i) > print (i) > inorder(right of i)

inorder(5) called

todo: inorder(4) > print 5 > inorder(6)

do inorder(4)

todo: inorder(left of 4)=nothing > print(4) > inorder(right of 4)=nothing > print(5)

inorder 6

do print 4

do print 5

do inorder 6

todo: inorder(left of 6)=nothing > print 6 > inorder(60)

do print 6

do inorder 60

todo: inorder(25) > print 60 > inorder(right of 60)=nothing

do inorder 25

todo: inorder(10) > print 25 > inorder(right of 25)=nothing > print 60

do inorder 10

todo: inorder(left of 10)=nothing > print 10 >inorder(right of 10)=nothing >print 25>print 60

do print 10

do print 25

do print 60

So if you see the order of printing its 4 5 6 10 25 60



来源:https://stackoverflow.com/questions/23852798/in-order-recursion-in-binary-trees

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!