> Binary Search Tree Problem(Java)?

Binary Search Tree Problem(Java)?

Posted at: 2014-12-18 
The Search.insert() method does not return a value, so you can't assign that void result to a node.

For some baffling reason, the Search.find() method returns a boolean and not a Node. I don't see how that is useful in the least.

Because of both of those facts, if you want recursive implementations of insert() and find(), you will need to define helper functions. Here's a sample for the find() method to use:

private static Node findNode(Node root, int data) {

.... if (root == null) return null;

.... if (data > root.data) return findNode(root.right, data);

.... if (data < root.data) return findNode(root.left, data);

.... return root;

}

That returns a found Node object, or null if the value wasn't in the tree.

Using that, your find() method can simply return null != findNode(this.root, b) as a boolean.

Use the same idea on insert().

A description of the assignment is attached.

I'm trying to create a class BinarySearchTree which implemets:

-----------------------------------------------------------

interface Search{

void insert(int n);

boolean find(int n);

int getCost();

void resetCost();

}

-------------------------------------------------------------

Here's the class:

-----------------------------------------------------------

public class BinarySearchTree implements Search{



private Node root;

int cost;



public void insert(int n){

cost++;

if(root==null) root = new Node(n);

if(n < root.data) root.left=insert(n);

if(n > root.data) root.right=insert(n);

}



public boolean find(int n){

cost++;

if(root == null) return false;

if(root.data==n) return true;

if(n
if(n>root.data) return find(root.right, n);

root = insert(root, n);

}



public int getCost(){

return cost;



}

public void resetCost();{

cost = 0;

}

}

I keep getting errors on the if statements it says incompatible types Node and int, how can I fix this?