java - BST 不是抽象的,不会重写 TreeGT 中的抽象方法 height()

标签 java binary-search-tree

我收到错误: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/

相关文章:

java - 从具有列表的实体创建数据库表

Java Jersey 使用 GET 返回 JSON,仅返回部分字段而不是全部

java - 不使用集合类的二叉搜索树迭代器实现

c++ - 在 BST 中搜索值代码

java - 不能 ssh 吗?可能是我 Mac 上 bash_profile 的问题

java - 将 json 的特定值传递给另一个 Activity

java - Tomcat 不提供简单的 HTML 页面 (HTTP 404)

c++ - 构建二叉搜索树时出现段错误

data-structures - 为什么只在平衡的二分查找树的叶节点中存储数据?

java - 编写适当的二叉树高度函数?