Java实现二叉搜索树,仅对键使用迭代器

Java实现二叉搜索树,仅对键使用迭代器,java,Java,我在Java编程方面遇到了一个问题。我想实现一个类(二进制搜索树),如下所示 然后我写了下面的代码 public Iterable<Key> iterator() { return new ABCKeyIterator(); } private class ABCKeyIterator implements Iterator<Key> { public boolean hasNext() {} public void remove() {}

我在Java编程方面遇到了一个问题。我想实现一个类(二进制搜索树),如下所示

然后我写了下面的代码

public Iterable<Key> iterator()
{
    return new ABCKeyIterator();
}

private class ABCKeyIterator implements Iterator<Key>
{
    public boolean hasNext() {}
    public void remove() {}
    public Key next() {}   
}
public Iterable迭代器()
{
返回新的ABCKeyIterator();
}
私有类ABCKeyIterator实现迭代器
{
公共布尔值hasNext(){}
public void remove(){}
公钥next(){}
}
该类包括键值对。现在我想实现一个迭代器函数来获取所有键。迭代器函数出错

iterator() in ABC cannot implement iterator() in Iterable return type Iterable<Key> is not compatible with Iterator<Key> where Key,T are type-variables: Key extends Comparable<Key> declared in class ABC T extends Object declared in interface Iterable
ABC中的迭代器()无法在Iterable返回类型Iterable中实现迭代器(),Iterable与迭代器不兼容,其中Key,T是类型变量:Key扩展类ABC中声明的可比性T扩展接口Iterable中声明的对象
有人能帮忙看看如何从头开始实现它吗?我认为Java已经有了这样的东西。但我想自己实现它来学习它。谢谢。

该错误消息抱怨此行:

public Iterable<Key> iterator()
public Iterable迭代器()

您已将
iterator()
声明为返回
Iterable
而不是
iterator
。这两个接口虽然相关,但却完全不同。

您的ABC类只是一个键值对类吗?然后迭代器将迭代单个值?我在ABC中有链表。我叫它ABCNode,它是Key,Value,ABCNode left,ABCNode right。你为什么不能直接返回链表的迭代器呢?谢谢你,鲁克,我想我应该为函数iterator()返回迭代器。
iterator() in ABC cannot implement iterator() in Iterable return type Iterable<Key> is not compatible with Iterator<Key> where Key,T are type-variables: Key extends Comparable<Key> declared in class ABC T extends Object declared in interface Iterable
public Iterable<Key> iterator()