I am trying to write a recursive method inside a searchTree class, and for some reason, the methods are looking outside the class in the interface for the definition of the method rather than looking at themselves. Here are some snippets of the code:
in Main class:
public class BinarySearchTree<T extends Comparable<T>> implements BinarySearchTreeInterface1<T>, BinarySearchTreeInterface2<T>{`
recursive method within Main:
public int getHeight(){
if (isEmpty()){
return 0;
}else{
int height = 1 + Math.max(this.getLeftSubTree().getHeight(),
this.getRightSubTree().getHeight());
return height;
}
}`
As you can see I have two interface classes. The error underlines getHeight() and states:
cannot find symbol
symbol: method getHeight()
location: interface BinarySearchTreeInterface1<T>
where T is a type-variable:
T extends Comparable<T> declared in class BinarySearchTree
I have 5 recursive methods that were shown in the Interface2 file and all of them are getting this same erorr. Thanks for any assistance.
Update: I am hesitant to post the Interface classes as they are from a university class and I don’t wish to upset the instructor. The ironic thing is, the non-recursive methods work just fine. Here is a small part of what the Interface2 file shows:
public interface BinarySearchTreeInterface2<T>{
public int getNumberOfNodes();
public int getHeight();
It seems that
getLeftSubTree()returns aBinarySearchTreeInterface1<T>. But getHeight() is defined inBinarySearchTreeInterface2<T>, so the method getHeight() can’t be called on the result ofgetLeftSubTree().You might be able to get the program compile by overriding the
getLeftSubTree()method like this:Of course, the same must be done with
getRightSubTree().