Java 具有可比性的铸件
我试图找到树中某个节点的最小值,为了检测某个节点是否具有较小的值,我使用Java 具有可比性的铸件,java,casting,tree,binary-tree,comparable,Java,Casting,Tree,Binary Tree,Comparable,我试图找到树中某个节点的最小值,为了检测某个节点是否具有较小的值,我使用compareTo()函数,如下所示: @SuppressWarnings("unchecked") public static Object min(TreeNode t) { if(t == null) { return null; } Comparable<TreeNode> min = (Compar
compareTo()
函数,如下所示:
@SuppressWarnings("unchecked")
public static Object min(TreeNode t)
{
if(t == null) {
return null;
}
Comparable<TreeNode> min = (Comparable<TreeNode>) t;
if(t.getLeft() != null) {
Comparable<TreeNode> leftMin = (Comparable<TreeNode>) min(t.getLeft());
if( ((Comparable<TreeNode>)leftMin).compareTo( (Comparable<TreeNode>)min) < 0) {
min = leftMin;
}
}
if(t.getRight() != null) {
Comparable<TreeNode> rightMin = (Comparable<TreeNode>) min(t.getRight());
if( ((Comparable<TreeNode>)rightMin).compareTo( (Comparable<TreeNode>)min) < 0) {
min = rightMin;
}
}
return min;
}
我也尝试过:if(leftMin.compareTo(min)<0)
,但是这会产生相同的错误
您知道如何正确地强制转换和转换以下类吗?类
TreeNode
必须实现可比接口:
public class TreeNode implements Comparable<TreeNode> {
...
@Override
public int compareTo(TreeNode other) {
... // maybe compare 'initValue' here
}
}
这要求Value
中的实例实现Comparable
正如其他人建议的那样,您可以使用Comparable接口,这将要求您实现compareTo
方法
可在java se文档中找到与实现的比较详细信息:
将此对象与订单的指定对象进行比较。返回一个
负整数、零或正整数,因为此对象小于
大于、等于或大于指定对象
因此,我们可以将您的类更改为如下所示(注意:我建议将值转换为int或任何其他基本类型):
与其执行Compariable(TreeNode)(括号由于某种原因不起作用),不如将其转换为泛型Compariable对象,尤其是对于返回数据类型(object)
这是我认为应该在下面的内容
@SuppressWarnings("unchecked")
public static Object min(TreeNode t)
{
if(t == null) {
return null;
}
Comparable min = (Comparable)t.getValue();
if(t.getLeft() != null) {
Comparable leftMin = (Comparable) min(t.getLeft());
if(leftMin.compareTo(min) < 0) {
min = leftMin;
}
}
if(t.getRight() != null) {
Comparable rightMin = (Comparable) min(t.getRight());
if(rightMin.compareTo(min) < 0) {
min = rightMin;
}
}
return min;
}
@SuppressWarnings(“未选中”)
公共静态对象最小值(TreeNode t)
{
如果(t==null){
返回null;
}
可比最小值=(可比)t.getValue();
如果(t.getLeft()!=null){
可比较的leftMin=(可比较的)min(t.getLeft());
如果(左最小值与(最小值)<0){
min=leftMin;
}
}
如果(t.getRight()!=null){
可比rightMin=(可比)min(t.getRight());
如果(右最小值与(最小值)<0){
min=rightMin;
}
}
返回最小值;
}
如果正确使用泛型,则根本不需要任何强制转换。我无法更改classIn。在这种情况下,请尝试扩展TreeNode
类(使用extends
关键字对其进行子类化)或者使用TreeNode
作为成员变量创建一个新类,然后为该类实现comparable
接口。写下你的最小发现逻辑,基于新的类。@安德列回答编辑,以反映问题提出的变化,但也考虑。
public static TreeNode min(TreeNode node) {
...
TreeNode minNode = node;
Comparable<?> minValue = (Comparable<?>) minNode.getValue(); // guessed method name
if (node.getLeft() != null) {
TreeNode leftMin = min(t.getLeft());
Comparable<?> leftValue = (Comparable<?>) leftMin.getValue();
if (leftValue.compareTo(minValue) < 0) {
minNode = leftNode;
minValue = leftValue;
}
...
class TreeNode implements Comparable<TreeNode> {
// recommend to convert value to int or any other primitive type
private Object value;
private TreeNode left, right;
public TreeNode(Object initValue) {
value = initValue;
left = null;
right = null;
}
// if value is int, just use ==, < and >
// i.e. this.value == o.value, this.value < o.value and so on ...
@Override
public int compareTo(TreeNode o) {
if (this.value.equals(o.value)) return 0;
else if (this.value.hashCode() < o.value.hashCode()) return -1;
else return 1;
}
/*methods*/
}
// This method is not actually correct (i.e. won't actually find the min),
// but showing how it would change after using the comparable interface
// on the tree node class.
public TreeNode min(TreeNode t) {
if(t == null) {
return null;
}
TreeNode min = t;
if(t.getLeft() != null) {
TreeNode leftMin = min.getLeft();
if(leftMin.compareTo(min) < 0) {
min = leftMin;
}
}
if(t.getRight() != null) {
TreeNode rightMin = min.getRight();
if( rightMin.compareTo(min) < 0) {
min = rightMin;
}
}
return min;
}
@SuppressWarnings("unchecked")
public static Object min(TreeNode t)
{
if(t == null) {
return null;
}
Comparable min = (Comparable)t.getValue();
if(t.getLeft() != null) {
Comparable leftMin = (Comparable) min(t.getLeft());
if(leftMin.compareTo(min) < 0) {
min = leftMin;
}
}
if(t.getRight() != null) {
Comparable rightMin = (Comparable) min(t.getRight());
if(rightMin.compareTo(min) < 0) {
min = rightMin;
}
}
return min;
}