[英]Iterate through a avl tree
I currently write on an AVL-tree
and write an iterator-method, to iterate through the whole tree in preorder. 我目前在
AVL-tree
编写并编写迭代器方法,以按顺序遍历整个树。 And I get a NullPointer
if I process the line 'stack.empty()' and don't have a clue why. 如果我处理'stack.empty()'行,但不知道为什么,则会得到一个
NullPointer
。 And 4 eyes do see it better than two ;). 有四只眼睛比两只有更好的感觉;)。 Thank you in beforehand for help in advance.
在此先感谢您的帮助。
Code: 码:
@Override
public Iterator<E> iterator() {
return new Iterator<E>(){
Node start;
Node current;
// int counter;
Stack<Node> stack;
// int border = count(root);
public void iterator() {
stack = new Stack<Node>();
current = start;
stack.add(root);
}
@Override
public boolean hasNext() {
if(stack.empty()) return false;
else return true;
}
@Override
public E next() {
if(!hasNext()){
throw new NoSuchElementException();
}
Node n = stack.pop();
if(n.left.value != null) stack.push(n.left);
if(n.right.value != null) stack.push(n.right);
return n.value;
}
};
}
你可能是说n.left!= null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.