1 Replies - 275 Views - Last Post: 09 October 2012 - 06:28 PM Rate Topic: -----

#1 JavaLilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-September 12

Is a BST or not?

Posted 09 October 2012 - 04:14 PM

This is a homework assignment.

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



Is This A Good Question/Topic? 0
  • +

Replies To: Is a BST or not?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,354
  • Joined: 27-December 08

Re: Is a BST or not?

Posted 09 October 2012 - 06:28 PM

If you are encountering errors, please post them exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1