我对如何为这个特定的通用bst实现迭代器感到有些困惑。以下是一些代码:
public class BinSearchTree<E extends Comparable<E>> extends AbstractSet<E> {
protected Entry<E> root;
protected int size;
public BinSearchTree() {
root = null;
size = 0;
}
public BinSearchTree(BinSearchTree<E> otherTree) {
LinkedList<Entry <E>> elements= new LinkedList<Entry<E>>();
elements.add(otherTree.root);
while(!elements.isEmpty()){
BinSearchTree.add(elements.remove());
}
}
这里大致是我要实施的内容
protected class TreeIterator implements Iterator<E> {
/**
* Positions this TreeIterator to the smallest
* element, according to the Comparable interface,
* in the BST object. The worstTime(n) is O(n)
* and averageTime(n) is O(log n).
*/
protected TreeIterator() {
}
/**
* Determines if there are still some elements,
* in the BST object this TreeIterator object is
* iterating over, that have not been accessed by
* this TreeIterator object.
*
* @return true - if there are still some elements
* that have not been accessed by this
* TreeIterator object; otherwise, return
* false.
*/
public boolean hasNext() {
return false;
}
/**
* Returns the element in the Entry this
* TreeIterator object was positioned at
* before this call, and advances this
* TreeIterator object. The worstTime(n) is O(n)
* and averageTime(n) is constant.
*
* @return the element this TreeIterator object
* was positioned at before this call.
*
* @throws NoSuchElementException - if this
* TreeIterator object was not positioned
* at an Entry before this call.
*/
public E next() {
return null;
}
/**
* Removes the element returned by the most recent
* call to this TreeIterator object’s next() method.
* The worstTime(n) is O(n) and averageTime(n) is
* constant.
*
* @throws IllegalStateException - if this
* TreeIterator’s next() method was not
* called before this call, or if this
* TreeIterator’s remove() method was called
* between the call to the next() method and
* this call.
*/
public void remove() {
}
}
欢迎任何帮助,谢谢!