verifying a Binary Search Tree
If anyone can tell me where I am going wrong with this method, that would be reaallly awesome, because I have no idea how to make hte method work properly. It consists of two methods actually, a driver method for the recursive method, and the recursive method itself:
public boolean isBST(tNode r) // r is the root of the tree passed in on calling this method
{
return isBST(r,r.left.key, r.right.key);
}
public boolean isBST(tNode t, int min, int max)
{
if(t == null)
{
return true;
}
if(t.key < min || t.key > max)
{
return false;
}
boolean leftIs = isBST(t.left,min,t.key);
if(!leftIs)
{
return false;
}
return isBST(t.right,t.key+1,max);
}
Please help. I don't know if my min's and max's are right in the calls, and if they are right in the original driver call.
Code tags added. -Narue
kharri5
Junior Poster in Training
56 posts since Jan 2005
Reputation Points: 13
Solved Threads: 0
It looks fine, though what happens if you have a one or two node tree here?
return isBST(r,r.left.key, r.right.key);
It would be better to come up with some sentinel min and max value to pass to isBST rather than requiring the tree to have at least three nodes.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401