我正在尝试用java编写我自己的二叉搜索树。我已经编写了所有方法,现在我正在尝试编写一个程序来测试这些方法。
但是,当我尝试实现我的“插入”方法时,它不会编译,我也不知道为什么。
public class lab05driver {
public static void main(String[] args) {
BST q = new BST();
int a = 5;
String b = "jed";
double c = 1.8;
char d = 'r';
boolean e = false;
int f = 35;
String g = "yay";
double h = 2.1;
char i = 'i';
boolean j = true;
Integer k = 5;
q.insert(k);
}}
我的 BST 课程如下所示:
public class BST implements myBST {
private myTreeNode root;
public BST() {
}
public void insert(Comparable x) {
if(root == null) {
root = new myTreeNode();
root.data = x;
} else if ( !lookup(x) ) {
root.insert(x);
}
}
...more code...
}
而且,myBST 看起来像:
public interface myBST {
public void insert(Comparable x);
public void delete(Comparable x);
public boolean lookup(Comparable x);
public void printPreOrder();
public void printInOrder();
public void printPostOrder();
}
最后,myTreeNode 看起来像:
public class myTreeNode {
public myTreeNode() {
}
public Comparable data ;
public myTreeNode leftchild;
public myTreeNode rightchild;
public myTreeNode parent;
public void insert(Comparable d) {
//if less than
//does left exist? if it doesnt, make it, give it d
//if it exists call insertrecursive on rightchild
if(d.compareTo(data) <= 0) {
if(leftchild != null) {
leftchild.insert(d);
} else {
leftchild = new myTreeNode();
leftchild.data = d;
leftchild.parent = this;
}
} else {
if(rightchild != null) {
rightchild.insert(d);
} else {
rightchild = new myTreeNode();
rightchild.data = d;
rightchild.parent = this;
}
}
}
...more code...
}
它在 lab05driver 中的“q.insert(k)”处引发错误。任何帮助/建议将不胜感激......
~~~~~ 编辑:对不起,我只是复制了那个错误......有一个主要方法,整数k是一个整数......我得到命令行的错误是:警告:[unchecked] unchecked call to compareTo(T ) 作为原始类型 java.lang.Comparable 的成员