【问题标题】:Iterator for a Binary Search Tree do not go down the tree二叉搜索树的迭代器不会沿着树向下
【发布时间】:2019-07-22 06:46:16
【问题描述】:

我一直在努力使用迭代器方法实现二叉搜索树。我一直在 WikiPedia 上查看这个算法:

def search_recursively(key, node):
    if node is None or node.key == key:
        return node
    if key < node.key:
        return search_recursively(key, node.left)
    # key > node.key
    return search_recursively(key, node.right)

我将它翻译成 Java:

public Iterator<T> iterator()
    {
        return new Iterator<T>()
        {
            private int count = 0;

            @Override
            public boolean hasNext()
            {
                return count++ < size;
            }

            @Override
            public T next()
            {
                return search(root, root.word);
            }

            public T search(BST root, T word)
            {
                if (root == null || root.word.compareTo(word) == 0)
                {
                    return root.word;
                }

                if (root.word.compareTo(word) < 0)
                {
                    return search(root.left, word);
                }

                return search(root.right, word);
            }
        };

在尝试运行程序时,我只得到了 BST 的根元素:

MyWordSet bst = new MyWordSet();

T bst = new T("one");
T bst = new T("two");
T bst = new T("three");
T bst = new T("four");
T bst = new T("five");
T bst = new T("six");

bst.add(w1);
bst.add(w2);
bst.add(w3);
bst.add(w4);
bst.add(w5);
bst.add(w6);

Iterator<T> it = bst.iterator();

while (it.hasNext())
{
    System.out.println(it.next());
}

所以输出是:

one
one
one
one
one
one

那么为什么我的迭代器中的这个方法不能让我到达整棵树?我真的无法弄清楚这里出了什么问题以及为什么它只在它应该下树时才打印出一个。

【问题讨论】:

  • 您的next() 方法启动,根并搜索root.word (search(root, root.word)),所以它当然总是返回根元素。
  • 我没有深入研究你的算法,但是很怀疑你没有在search方法的任何地方使用count变量。而且我还希望它在next() 中增加,而不是在hasNext() 中。
  • 顺便说一句,算法是迭代的,这意味着没有递归调用,但您的实现是递归的。而且我不确定您为什么认为该算法需要实现Iterator 接口。
  • @Eran 我看到了,我换了正确的,我的错!它是一个赋值,所以我必须使用 Iterator 方法。
  • 迭代器方法不应该接受你要搜索的值吗?

标签: java algorithm iterator


【解决方案1】:

您根本不更新current_node

缺少current_node = node 的等效项。


嗯,改了代码之后,这里修改答案:

import java.util.Iterator;
import java.util.Stack;

/**
 *
 * @author jk
 */
public class BSTIterator<T> implements Iterator<T> {

    public static final class BST<T> {

        private BST<T> left;
        private BST<T> right;
        private T word;

        private BST(T word) {
            this.word = word;

        }

    }
    private final Stack<BST<T>> stackBST = new Stack<>();

    public BSTIterator(final BST<T> root) {
        // push all most left entries of the tree to the stack
        BST<T> currBST = root;
        while (currBST != null) {
            stackBST.push(currBST);
            currBST = currBST.left;
        }
    }

    @Override
    public boolean hasNext() {
        return !stackBST.isEmpty();
    }

    @Override
    public T next() {
        BST<T> currBST = stackBST.pop();

        // check if we are on the most right entry
        final boolean notMostRightEntry = currBST.right != null;
        if (notMostRightEntry) {
            // take next right entry 
            BST<T> nextBST = currBST.right;
            while (nextBST != null) {
                // push this next right entry on the stack
                stackBST.push(nextBST);
                nextBST = nextBST.left;
            }
        }
        return currBST.word;
    }

    public static void main(String[] args) {
        BST<Integer> root = new BST<>(20);
        root.left = new BST<>(5);
        root.right = new BST<>(30);
        root.left.right = new BST<>(10);
        root.right.left = new BST<>(25);
        root.right.right = new BST<>(40);
        root.right.left = new BST<>(35);
        root.right.left.left = new BST<>(32);
        for (Iterator<Integer> bstIt = new BSTIterator<>(root); bstIt.hasNext();) {
            System.out.println("val: " + bstIt.next());

        }
    }

}

【讨论】:

  • 我应该在哪里更新等效的 current_node = node ?在搜索方法中的每个 if 表达式中?
猜你喜欢
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多