我收到错误:BST 不是抽象的,并且不会覆盖 TreeGT 中的抽象方法 height()。我需要一些帮助来理解它的含义以及如何解决它。
在文件 TreeGT.java 中,我有以下代码:
public interface TreeGT<E> {
public boolean insert(E item);
public boolean delete(E item);
public boolean find(E item);
public int height();
}
在文件 BST.java 中,我有以下代码:
import java.util.*;
public class BST<E extends Comparable<E>> implements TreeGT<E> {
private Node root; //Only root by itself
public BST() {
root = null;
}
private static class Node {
Comparable data;
int height; //Height of node
int size; //Number of nodes in tree
private Node left; //Left subtree
private Node right; //Right subtree
Node (Comparable data) { //Constructor for tree
this.data = data;
this.height = 0; //Height zero
this.size = 1; //Root counts
this.left = null; //No left leafs
this.right = null; //No right leafs
}
}
public void makeEmpty() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
public int size() {
if (isEmpty())
throw new Exception("Tree is empty");
return root == null ? 0 : root.size;
}
public boolean insert(E item) {
return false;
}
public boolean delete(E item) {
return false;
}
public boolean find(E item) {
return false;
}
}
附注我才用java编程几天,所以请你说得尽可能简单/直接。另外,我正在尝试创建 BST。我认为我走在正确的道路上,不是吗?
最佳答案
接口(interface)是未定义(即抽象)方法的集合。当您实现一个接口(interface)(在 implements treeGT
中实现)时,您必须为所有这些方法提供定义。因此,由于您的类实现了treeGT,并且height()是treeGT中定义的抽象方法之一,因此您必须在您的类中定义该方法。
即您需要添加
public int height(){
//do something
}
关于java - BST 不是抽象的,不会重写 TreeGT 中的抽象方法 height(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15181575/