首页 > 解决方案 > 如何正确使用二叉搜索树的通用实现

问题描述

我写了一个二叉搜索树和二叉搜索节点类,我对泛型不太擅长,所以我尝试用泛型来做。现在,每当我创建我编写的类的二叉树时,将参数类型设置为 String 是完全可以的,但是如果我创建自己的类型,那么它不允许我使用它。我会解释一下,这是我的 BinaryNode 和 BinaryTree 类,以及我自己创建的类型 Name,它本质上是一个字符串,但我只是想测试它:

二进制节点:

public class BinaryNode<T extends Comparable<T>> {
T data;
BinaryNode<T> left;
BinaryNode<T> right;


public BinaryNode (T data){
    this.data = data;
}
public BinaryNode (){
    this(null);
}

//insert

public void insert(T data){
    // if data is less than node's data , then if left is null insert , if not do the same for left
    if(this.data.compareTo(data) > 0){
        if(this.left == null){
            left =  new BinaryNode<>(data);
        }
        else {
            left.insert(data);
        }
    }
    // if data is greater than node's data , then if right is null insert , if not do the same for right
    else if(this.data.compareTo(data) < 0) {
        if (this.right == null) {
            right =  new BinaryNode<>(data);;
        }
        else{
            right.insert(data);
        }
    }
    // if it's equal to node's data do nothing as we do not allow duplicate values.
}

public void traverseInOrder(){
    if(this.left != null){
        left.traverseInOrder();
    }
    System.out.println(this.data);
    if(this.right != null){
        right.traverseInOrder();
    }
}

public BinaryNode getMin(){
    if(left !=null) {
       return left.getMin();
    }
    return this;
}
public BinaryNode getMax(){
    if(right !=null){
       return right.getMax();
    }
    return this;
}
public BinaryNode get(T data){

    if(this.data.compareTo(data) > 0){
        if(this.left == null) {
            return null;
        }
        return left.get(data);
    }
    if(this.data.compareTo(data) < 0){
        if(this.right == null){
            return null;
        }
        return right.get(data);
    }
    return this;
}

@Override
public String toString() {
    return "data = " + data ;
}
}

二叉树:

public class BinaryTree<T extends Comparable<T>> {
BinaryNode<T> root;

public BinaryTree(T data){
    root = new BinaryNode<T>(data);
}
public BinaryTree(){
    root = null;
}

public void insert(T data){
    if(root == null){
        root = new BinaryNode<T>(data);
    }
    else{
        root.insert(data);
    }
}

public void traverseInOrder(){
    if(root == null){
        return;
    }
    root.traverseInOrder();
}

public BinaryNode getMin(){
    if(root == null){
        return null;
    }
    return root.getMin();
}

public BinaryNode getMax(){
    if(root == null){
        return null;
    }
    return root.getMax();
}

public BinaryNode get(T data){
    if(root == null){
        return null;
    }
    return root.get(data);

}


}

姓名:

public class Name implements Comparable {
String name;


public Name(String name){
    this.name = name;
}
public Name(){
    this("");
}


@Override
public int compareTo(Object o) {
    if(!(o instanceof Name)) throw new IllegalArgumentException();
    String n1 = this.name.toLowerCase();
    String n2 = ((Name) o).name.toLowerCase();
    for(int i = 0; i< n1.length() && i< n2.length() ; i++ ){
        if(n1.charAt(i) > n2.charAt(i)) return 1;
        if(n2.charAt(i) > n1.charAt(i)) return -1;
    }
    if(n1.length() > n2.length()) return -1;
    if(n2.length() > n1.length()) return 1;

    return 0;
}
public String toString(){
    return name;
}
}

现在我可以轻松地在 Main 中创建一个二叉树,如下所示:

BinaryTree<String> nametree = new BinaryTree<>();

但是,如果我想做类似的事情:

BinaryTree<Name> nametree = new BinaryTree<>();

然后我得到一个编译错误,除非我在 BinaryNode 和 BinaryTree 类中将 Comparable 更改为 Comparable ,而且我觉得我对泛型没有很好的掌握,所以如果有人能澄清我所做的事情有什么问题的话。以及任何其他可以帮助我更好、更正确地使用泛型的东西。提前致谢 !

标签: javagenericsbinary-search-tree

解决方案


问题可能是当您在 Name 中实现 Comparable 接口时需要一个类型。

尝试将 Name 的类定义设置为:public class Name implements Comparable<Name>

这个答案给出了一个很好的解释:Java-<T extends Comparable<T>> 的含义?


推荐阅读