通用二叉搜索树的java迭代器实现

ix0qys7i  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(197)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

四年前关门了。
改进这个问题
对于如何为这个特定的通用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() {

    }
}

欢迎任何帮助,谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题