Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BST不是抽象的,并且不重写TreeGT中的抽象方法height()_Java_Binary Search Tree - Fatal编程技术网

Java BST不是抽象的,并且不重写TreeGT中的抽象方法height()

Java BST不是抽象的,并且不重写TreeGT中的抽象方法height(),java,binary-search-tree,Java,Binary Search Tree,我收到一个错误:BST不是抽象的,并且不会覆盖TreeGT中的抽象方法高度。我需要一些帮助来理解它的含义,以及如何解决它 在文件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不是抽象的,并且不会覆盖TreeGT中的抽象方法高度。我需要一些帮助来理解它的含义,以及如何解决它

在文件TreeGT.java中,我有以下代码:

public interface TreeGT<E> {
    public boolean insert(E item);
    public boolean delete(E item);
    public boolean find(E item);
    public int height();
}
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;
}

}
在文件BST.java中,我有以下代码:

public interface TreeGT<E> {
    public boolean insert(E item);
    public boolean delete(E item);
    public boolean find(E item);
    public int height();
}
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。我认为我走的是正确的道路,不是吗?

接口是一个未定义的集合,即抽象方法。当您在implements treeGT中实现一个接口时,您必须为所有这些方法提供一个定义。因此,由于类实现了treeGT,而height是treeGT中定义的抽象方法之一,因此必须在类中定义该方法

i、 e.您需要添加

public int height(){
    //do something
}

接口是未定义方法的集合,即抽象方法。当您在implements treeGT中实现一个接口时,您必须为所有这些方法提供一个定义。因此,由于类实现了treeGT,而height是treeGT中定义的抽象方法之一,因此必须在类中定义该方法

i、 e.您需要添加

public int height(){
    //do something
}