Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二叉搜索树的迭代器不向下搜索树_Java_Algorithm_Iterator - Fatal编程技术网

Java 二叉搜索树的迭代器不向下搜索树

Java 二叉搜索树的迭代器不向下搜索树,java,algorithm,iterator,Java,Algorithm,Iterator,我一直在努力用迭代器方法实现二叉搜索树。我一直在维基百科上查看这个算法: 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_

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

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)

那么,为什么迭代器中的这个方法不适用于我访问整个树呢?我真的搞不懂这里到底出了什么问题,为什么它应该掉到树下才打印出一个

您只需不更新
当前\u节点

缺少当前节点的等效项


那么在修改了代码之后,这里修改了答案:

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());

        }
    }

}
import java.util.Iterator;
导入java.util.Stack;
/**
*
*@author jk
*/
公共类bstierator实现了迭代器{
公共静态最终类BST{
私人BST左;
私人BST权利;
私人T字;
私人BST(T字){
这个单词=单词;
}
}
私有最终堆栈stackBST=新堆栈();
公共BST迭代器(最终BST根){
//将树中最左边的所有条目推送到堆栈中
BST currBST=根;
while(currBST!=null){
stackBST.push(电流);
currBST=currBST.left;
}
}
@凌驾
公共布尔hasNext(){
return!stackBST.isEmpty();
}
@凌驾
公共交通工具{
BST currBST=stackBST.pop();
//检查我们是否在最右边的入口
最终布尔值notmostriggentry=currBST.right!=null;
如果(不尝试){
//在下一个路口右转
BST nextBST=当前BST.right;
while(nextBST!=null){
//按下堆栈上的下一个右项
stackBST.push(下一个测试);
nextBST=nextBST.left;
}
}
返回currBST.word;
}
公共静态void main(字符串[]args){
BST根=新BST(20);
root.left=新的BST(5);
root.right=新的BST(30);
root.left.right=新的BST(10);
root.right.left=新的BST(25);
root.right.right=新的BST(40);
root.right.left=新的BST(35);
root.right.left.left=新的BST(32);
for(迭代器bstIt=new BSTIterator(根);bstIt.hasNext();){
System.out.println(“val:+bstIt.next());
}
}
}

您的
next()
方法启动,root并搜索root.word(
search(root,root.word)
),因此它当然会始终返回root元素。我没有深入研究您的算法,但非常怀疑您没有在
search
方法中的任何位置使用
count
变量。我还希望它在
next()
中递增,而不是在
hasNext()
中递增。顺便说一句,算法是迭代的,这意味着没有递归调用,但您的实现是递归的。我不知道你为什么认为这个算法需要实现迭代器接口。@Eran我看到了,我改成了正确的,我错了!它用于赋值,所以我必须使用迭代器方法。迭代器方法不应该接受您希望搜索的值吗?我应该在哪里更新等效的current_node=node?在搜索方法中的每个if表达式中?
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
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());

        }
    }

}