Solved

# Binary Search Tree

Posted on 2004-11-30
279 Views
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
Question by:pete420

Author Comment

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

where is the implementation of functions nodeCount(root), insert(e,root), .......?
0

Author Comment

sorry i do not unserstand

0

LVL 13

Expert Comment

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

Author Comment

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

always the function is coded in the class who has member fields modified by those functions
0

Author Comment

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

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

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

}

0

LVL 13

Expert Comment

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

sorry im so confused.
i will re read over everythig u have sed.

thanks
pete
0

## Featured Post

### Suggested Solutions

wordsWithout 49 64
strCopies  challenge 17 60
word0 challenge 4 37
Eclipse Neon and jdk 1.8.0 11 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.