0

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

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Narue
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.