Java-二进制搜索节点如何检查对象?
我怎样才能做测试而不出错 因为该对象不能在此运算符中使用 我有错误(在插入函数中)为什么 错误: 未为参数类型java.lang.Object、java.lang.Object定义运算符<Java-二进制搜索节点如何检查对象?,java,binary-tree,Java,Binary Tree,我怎样才能做测试而不出错 因为该对象不能在此运算符中使用 我有错误(在插入函数中)为什么 错误: 未为参数类型java.lang.Object、java.lang.Object定义运算符
public class BinarySearchNode {
protected Object data;
protected BinarySearchNode left;
protected BinarySearchNode right;
public BinarySearchNode(Object data) {
if (data == null)
throw new RuntimeException("we are too lazy to deal with null data");
this.data = data;
this.left = null;
this.right = null;
}
public void insert(Object toAdd) {
if **(toAdd < data)** { <<<<<<<<<<<<<<<<<<< here
if (left == null)
left = new BinarySearchNode(toAdd);
else
left.insert(toAdd);
} else {
if (right == null)
right = new BinarySearchNode(toAdd);
else
right.insert(toAdd);
}
}
public void InOrderPrint() {
if (left != null)
left.InOrderPrint();
System.out.println(this.data);
if (right != null)
right.InOrderPrint();
}
}
公共类二进制搜索节点{
受保护对象数据;
左侧受保护的二进制搜索节点;
受保护的二进制搜索节点权限;
公共二进制搜索节点(对象数据){
如果(数据==null)
抛出新的RuntimeException(“我们懒得处理空数据”);
这个数据=数据;
this.left=null;
this.right=null;
}
公共空白插入(要添加的对象){
如果**(toAdd对象
s的顺序:
- 将
替换为——这样可以将比较逻辑放入对象本身,或者对象
- 要求调用者提供的实现-这将允许调用者在使用API时直接提供逻辑
public class BinarySearchNode<T extends Comparable<T>> {
protected T data;
...
public void insert(T toAdd) {
if (toAdd.compareTo(data) < 0) {
...
} else {
...
}
}
}
公共类二进制搜索节点{
受保护的T数据;
...
公共无效插入(T至添加){
如果(添加比较到(数据)<0){
...
}否则{
...
}
}
}