check if a tree is a binary search tree

Posted by TimeToCodeTheRoad on Stack Overflow See other posts from Stack Overflow or by TimeToCodeTheRoad
Published on 2011-01-01T19:34:01Z Indexed on 2011/01/01 19:54 UTC
Read the original article Hit count: 287

I have written the following code to check if a tree is a Binary search tree. Please help me check the code:

Pair p{ 
    boolean isTrue;
    int min;
    int max;
}

public boo lean isBst(BNode v){ 
    return isBST1(v).isTrue;  
}



public Pair  isBST1(BNode v){

    if(v==null)
    return new Pair(true, INTEGER.MIN,INTEGER.MAX);

    if(v.left==null && v.right==null)
    return new Pair(true, v.data, v.data);

    Pair pLeft=isBST1(v.left);
    Pair pRight=isBST1(v.right);

    boolean check=pLeft.max<v.data && v.data<= pRight.min;

    Pair p=new Pair();
    p.isTrue=check&&pLeft.isTrue&&pRight.isTrue;

    p.min=pLeft.min;
    p.max=pRight.max;
    return p;

}

Note: This function checks if a tree is a binary search tree

© Stack Overflow or respective owner

Related posts about java

Related posts about data-structures