I am trying to figure out if the binary tree I have is a BST or not.
So, first I looked at what the conditions have to be for a BST.
1) The root of the tree/ each branch can have at most 2 leaves or branches.
2) The right must be greater than the root and the left must be smaller than the root.
The code I tried is below. I am trying to use recursion. Everytime I try to compile, it says that the operator can not be applied to the given node (the error starts at the first if condition inside the while loop).
I thought the logic was correct, I just don't kknow why it keeps giving me that error message or how to fix it. Is there some way I can change the variable to an equivalent that I can use a boolean operator on? Or is my logic the problem?
Thanks for any help.
public boolean isBST (AnyType x, BinaryNode<AnyType>t)
{
//Base case, if both the left and the right are null, then the tree is a BST
if (t.left ==null&&t.right==null)
{
return true;
}
while(t.left!=null&&t.right!=null) //until you have reached the bottom of the tree
{
if(t.left < t.element) // if the left of the tree(t.element) < t.element then part of BST condition satisfied
{
t.left = isBST(t.left);
}
if(tRight > t.element)// if the right of the tree(t.element) > t.element then part of BST condition satisfied
{
t.right = isBST (t.right);
}
}
// if not true return false
return false;
}

New Topic/Question
Reply



MultiQuote






|