我已经实现了一个AVL树,我想写一个Iterator,它按预先搜索。我有这种代码的安静,我总是在" stack.push(当前)"中得到一个NullPointer。并且不知道为什么
@Override
public Iterator<E> iterator() {
return new Iterator<E>(){
Node current;
int counter;
Stack<Node> stack;
public void iterator() {
counter++;
stack = new Stack<Node>();
current = root;
stack.empty();
}
@Override
public boolean hasNext() {
if(counter == count(root)) return false;
else return true;
}
@Override
public E next() {
stack.push(current);
counter++;
if(current.left.value != null) return current.left.value;
else return current.right.value;
}
};
}
事先谢谢:)
答案 0 :(得分:0)
您的root
对象从未声明或实例化过。您正在设置current = root;
,但在当前上下文中,root
无效。
public void iterator() {
counter++;
stack = new Stack<Node>();
current = root;
stack.empty();
}