使用通用类比< T>在Java中实现二叉树数据? [英] Implementing Binary Tree in Java with Generic Comparable<T> data?
问题描述
if( data.compareTo(this.data)< = 0)
,
产生
错误:不兼容的类型:java.lang.Comparable< T>无法转换为T
?
this.data
的类型是 Comparable< T>
,并且应该能够使用或是compareTo()方法的参数... right ?好吧,显然不是。但我真的不明白为什么。泛型仍然让我困惑。
public class MyBinaryTreeNodeG< T> {
Comparable< T>数据;
MyBinaryTreeNodeG< T>父母;
MyBinaryTreeNodeG< T>剩下;
MyBinaryTreeNodeG< T>对;
public MyBinaryTreeNodeG(比较< T>数据){
this.data = data;
}
public MyBinaryTreeNodeG< T> addChild(Comparable< T> data){
if(data.compareTo(this.data)< = 0){//这是编译器插入的行
//检查是否存在左树节点为空。如果是这样,请添加。否则,递归。
} else {
//相同的树节点
返回null;
$ / code>
下面是一个更加标准的二叉树实现的剪辑。这编译好。但我仍然不明白为什么这是一个更好(根据编译器)实施比我上面。
public class MyBinaryTreeNodeG< ; T扩展了Comparable< T>> {
T data;
MyBinaryTreeNodeG< T>父母;
MyBinaryTreeNodeG< T>剩下;
MyBinaryTreeNodeG< T>对;
public MyBinaryTreeNodeG(T data){
this.data = data;
}
public MyBinaryTreeNodeG< T> addChild(T data){
if(data.compareTo(this.data)< = 0){
// left node stuff
} else {
// right node东西
返回null;
=https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T) =nofollow>可比较的的javadoc: compareTo(T)
由 Comparable
接口提供。该方法允许将实现此接口的 T
类的对象实例与另一个 T
实例进行比较。在第一个示例中,您将比较 Comparable< T>
与 Comparable< T>
T
)
这应该起作用:
public class MyBinaryTreeNodeG< T> {
比较< T>数据;
// ...
public MyBinaryTreeNodeG< T> (this.data.compareTo(data)<= 0){
} else {
}
// ...
Q: In my implementation of a binary tree below, why does the compiler choke at
if (data.compareTo(this.data) <= 0)
,
producing
Error: incompatible types: java.lang.Comparable<T> cannot be converted to T
?
Both data
and this.data
are of type Comparable<T>
and should be able to use or be an argument to the compareTo() method...right? Well, clearly not. But I really don't understand why. Generics are still baffling me.
public class MyBinaryTreeNodeG<T>{
Comparable<T> data;
MyBinaryTreeNodeG<T> parent;
MyBinaryTreeNodeG<T> left;
MyBinaryTreeNodeG<T> right;
public MyBinaryTreeNodeG(Comparable<T> data){
this.data = data;
}
public MyBinaryTreeNodeG<T> addChild(Comparable<T> data){
if (data.compareTo(this.data) <= 0) { //this is the line on which the compiler chockes
//check if left tree node is null. If so, add. Otherwise, recurse.
} else {
//same for the right tree node
return null;
}
The following is a clip from a more standard implementation of a binary tree. This compiles fine. But I still fail to see why this is a "better" (according to compiler) implementation than mine above.
public class MyBinaryTreeNodeG<T extends Comparable<T>>{
T data;
MyBinaryTreeNodeG<T> parent;
MyBinaryTreeNodeG<T> left;
MyBinaryTreeNodeG<T> right;
public MyBinaryTreeNodeG(T data){
this.data = data;
}
public MyBinaryTreeNodeG<T> addChild(T data){
if (data.compareTo(this.data) <= 0) {
//left node stuff
} else {
//right node stuff
return null;
}
Look at the javadoc of Comparable : compareTo(T)
is provided by the Comparable
interface. This method allows to compare a object instance of T
class implementing this interface with another T
instance. In your first example, you're comparing a Comparable<T>
with a Comparable<T>
(and not with T
)
This should work :
public class MyBinaryTreeNodeG<T> {
Comparable<T> data;
// ...
public MyBinaryTreeNodeG<T> addChild(final T data) {
if (this.data.compareTo(data) <= 0) {
} else {
}
// ...
}
}
这篇关于使用通用类比< T>在Java中实现二叉树数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!