-1

我正在使用 BinaryNode 实现一个二叉搜索树来存储数据。我在我的 add 中使用了 CompareTo 方法,并包含确定该项目所属的子树的方法。我在使用 compare 的各个地方不断收到此错误:

BST.java:50: error: cannot find symbol
            if (item.CompareTo(root.data) > 0)
                    ^
  symbol:   method CompareTo(T)
  location: variable item of type T
  where T is a type-variable:
    T extends Comparable<? super T> declared in class BST

这是我的代码,我做错了什么?

import java.util.List;
import java.util.ArrayList;
import java.util.*;
import java.io.*;

public class BST<T extends Comparable<? super T>> implements BSTInterface<T>
{
    private BinaryNode<T> root;
    private int numberOfItems;
    public List<T> preOrder = new ArrayList<T>();
    public List<T> inOrder = new ArrayList<T>();
    public List<T> postOrder = new ArrayList<T>();

    public BST()
    {
        root = null;
        numberOfItems = 0;
    }

    public BST(T rootData)
    {
        root = new BinaryNode<T>(rootData);
        numberOfItems = 1;
    }

    public BST(T rootData, BST<T> leftTree, BST<T> rightTree)
    {
        root = new BinaryNode<T>(rootData);
        numberOfItems = 1;
        root.left = leftTree.root;
        root.right = rightTree.root;
    }

    public void setTree(T rootData)
    {
        root = new BinaryNode<T>(rootData);
    }

    public boolean contains(T item)
    {
        if (root.data.equals(item))
            return true;

        else
        {
            if (item.CompareTo(root.data) > 0)
            {
                root = root.left;
                return contains(item);
            }

            else if (item.CompareTo(root.data) < 0)
            {
                root = root.right;
                return contains(item);
            }

            else
                return false;
        }
    }

    public void add(T newItem)
    {
        if (root == null)
        {
            root = new BinaryNode<T>(newItem);
            numberOfItems++;
        }

        if (newItem.equals(root.data))
            return;

        if (newItem.CompareTo(root.data) < 0)
        {
            root = root.left;
            add(newItem);
        }

        if (newItem.CompareTo(root.data) > 0)
        {
            root = root.right;
            add(newItem);
        }   
    }
4

4 回答 4

2

这不是 c# :p

item.CompareTo()

change to:

item.compareTo()

compareTo来自Comparable界面。

于 2013-11-06T06:54:36.667 回答
0

如果您正在实施java.lang.Comparable,那么应该调用该方法compareTo(),而不是CompareTo()

为避免此类错误,建议@Override在方法前使用注释。这样 Eclipse 会告诉您您没有覆盖现有方法。

于 2013-11-06T06:55:44.690 回答
0

[Comparable<T>][1]有以下签名

int     compareTo(T o)
Compares this object with the specified object for order.

但是您正在使用CompareTo()请更正大小写,然后尝试

于 2013-11-06T06:55:46.787 回答
0

compareTo()不是CompareTo() // you are using capital C which is wrong

int java.lang.Comparable.compareTo(? super T o):-

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.

Parameters:
o - the object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
ClassCastException - if the specified object's type prevents it from being compared to this object.
于 2013-11-06T06:55:58.197 回答