Binary Search Tree

getting a load of errors in my code,

public class BinaryTree
{
  // reference to root node
  private BinaryTreeNode root;
 
  // default constructor
  public BinaryTree()
  {
    root = null;
  }
 
  // test if the tree is empty
  public boolean isEmpty()
  {
     return root==null;
  }
 
  // empty the tree
  public void clear()
  {
     root = null;
  }

// number of nodes in the tree
  public int nodeCount()
  {
    return nodeCount(root);
  }

  // insert an element into the tree  
  public void insert(int e)
  {
    root = insert(e,root);
  }
 
  // delete an element from the tree
  public void delete(int e)
  {
    root = delete(e,root);
  }
 
  // test if an element is in the tree
  public boolean find(int e)
  {
    return find(e,root);
  }

  // print all elements in the tree in order
  public void displayInOrder()
  {
     displayInOrder(root);
  }
}
pete420Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pete420Author Commented:
thought u mite also need:

public class BinaryTreeNode
{
  private int data;      
  private BinaryTreeNode left;
  private BinaryTreeNode right;
 
  public BinaryTreeNode() {
    this(0, null, null);
  }
 
  public BinaryTreeNode(int e, BinaryTreeNode ln, BinaryTreeNode rn) {
    data = e;
    left   = ln;
    right = rn;
  }
 
  public void setData(int newData) {
        data = newData;
  }
  public void setLeft(BinaryTreeNode nLeft) {
    left = nLeft;
  }
 
  public void setRight(BinaryTreeNode nRight) {
    right = nRight;
  }
 
  public int getData() {
    return data;
  }

  public BinaryTreeNode getLeft() {
    return left;
  }
 
  public BinaryTreeNode getRight() {
    return right;
  }
}
petmagdyCommented:
where is the implementation of functions nodeCount(root), insert(e,root), .......?
pete420Author Commented:
sorry i do not unserstand

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

petmagdyCommented:
example statment like:
>>    root = insert(e,root);
where is the function implementation of insert(e, root)?
pete420Author Commented:
i have not coded that,
i am unsure as to where i put the insert function, does it go in that class or does it go in the driver class file?

my understanding of organisation of java is sketchy

pete
petmagdyCommented:
always the function is coded in the class who has member fields modified by those functions

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pete420Author Commented:
ok thanks, i will give you the points now,

perhaps you could answer another general question here if you have the time:

there is a function:

public void insert(int e)
  {
    root = insert(e,root);
  }

then i am coding up:

      private BinaryTreeNode insert(int e, BinaryTreeNode n)
      {
         if (n == null)
         {
            n = new BinaryTreeNode(e,null,null);
         }
         else if  (e < n.getData())
         {
            // set the new left subtree after e is inserted into the the left subtree
                  n.setLeft(insert(e, n.getLeft()));
         }        
         else if (e > n.getData())
         {
           // set the new right subtree after e is inserted into the right subtree
                         n.setRight(insert(e, n.getRight()));
         }
         return n;
      }


why have to insert functions, whty not just have the code for insert in one function,
why have one insert function call another, thenr return the value??

thanks

pete
petmagdyCommented:
hence in ur case u don't need those functions, implment the methods like find(e), nodeCount(), this is the basics of object oriented, u need to read about OOP (object oriented programming)
petmagdyCommented:
u function need to be like this:

public void insert(int e)
  {
        if (root == null)
        {
           root = new BinaryTreeNode(e,null,null);
        }
        else if  (e < root.getData())
        {
           // set the new left subtree after e is inserted into the the left subtree
                 root.setLeft(insert(e, root.getLeft()));
        }        
        else if (e > root.getData())
        {
          // set the new right subtree after e is inserted into the right subtree
                        root.setRight(insert(e, root.getRight()));
        }

  }

please note
petmagdyCommented:
unless u intend in the future to use:

 private BinaryTreeNode insert(int e, BinaryTreeNode n)

on BinaryTreeNode other than the root variable
pete420Author Commented:
sorry im so confused.
i will re read over everythig u have sed.

thanks
pete
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.