[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Binary Search Tree

Posted on 2004-11-30
11
Medium Priority
?
284 Views
Last Modified: 2010-03-31
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);
  }
}
0
Comment
Question by:pete420
  • 6
  • 5
11 Comments
 

Author Comment

by:pete420
ID: 12708648
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;
  }
}
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12708831
where is the implementation of functions nodeCount(root), insert(e,root), .......?
0
 

Author Comment

by:pete420
ID: 12708842
sorry i do not unserstand

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:petmagdy
ID: 12708872
example statment like:
>>    root = insert(e,root);
where is the function implementation of insert(e, root)?
0
 

Author Comment

by:pete420
ID: 12708897
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
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 300 total points
ID: 12708945
always the function is coded in the class who has member fields modified by those functions
0
 

Author Comment

by:pete420
ID: 12708969
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
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12708973
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)
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12709005
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
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12709017
unless u intend in the future to use:

 private BinaryTreeNode insert(int e, BinaryTreeNode n)

on BinaryTreeNode other than the root variable
0
 

Author Comment

by:pete420
ID: 12709050
sorry im so confused.
i will re read over everythig u have sed.

thanks
pete
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question