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

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

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

四年前关门了。
改进这个问题
对于如何为这个特定的通用bst实现迭代器,我有点困惑。下面是一些代码:

  1. public class BinSearchTree<E extends Comparable<E>> extends AbstractSet<E> {
  2. protected Entry<E> root;
  3. protected int size;
  4. public BinSearchTree() {
  5. root = null;
  6. size = 0;
  7. }
  8. public BinSearchTree(BinSearchTree<E> otherTree) {
  9. LinkedList<Entry <E>> elements= new LinkedList<Entry<E>>();
  10. elements.add(otherTree.root);
  11. while(!elements.isEmpty()){
  12. BinSearchTree.add(elements.remove());
  13. }
  14. }

下面是我要实现的大致内容

  1. protected class TreeIterator implements Iterator<E> {
  2. /**
  3. * Positions this TreeIterator to the smallest
  4. * element, according to the Comparable interface,
  5. * in the BST object. The worstTime(n) is O(n)
  6. * and averageTime(n) is O(log n).
  7. */
  8. protected TreeIterator() {
  9. }
  10. /**
  11. * Determines if there are still some elements,
  12. * in the BST object this TreeIterator object is
  13. * iterating over, that have not been accessed by
  14. * this TreeIterator object.
  15. *
  16. * @return true - if there are still some elements
  17. * that have not been accessed by this
  18. * TreeIterator object; otherwise, return
  19. * false.
  20. */
  21. public boolean hasNext() {
  22. return false;
  23. }
  24. /**
  25. * Returns the element in the Entry this
  26. * TreeIterator object was positioned at
  27. * before this call, and advances this
  28. * TreeIterator object. The worstTime(n) is O(n)
  29. * and averageTime(n) is constant.
  30. *
  31. * @return the element this TreeIterator object
  32. * was positioned at before this call.
  33. *
  34. * @throws NoSuchElementException - if this
  35. * TreeIterator object was not positioned
  36. * at an Entry before this call.
  37. */
  38. public E next() {
  39. return null;
  40. }
  41. /**
  42. * Removes the element returned by the most recent
  43. * call to this TreeIterator object’s next() method.
  44. * The worstTime(n) is O(n) and averageTime(n) is
  45. * constant.
  46. *
  47. * @throws IllegalStateException - if this
  48. * TreeIterator’s next() method was not
  49. * called before this call, or if this
  50. * TreeIterator’s remove() method was called
  51. * between the call to the next() method and
  52. * this call.
  53. */
  54. public void remove() {
  55. }
  56. }

欢迎任何帮助,谢谢!

暂无答案!

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

相关问题