I have implemented an AVL tree and I wanted to write an Iterator, that searches in pre order. I have this peace of code and I get always a NullPointer in "stack.push(current)" and dont know why
@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;
}
};
}
Thanks in beforehand :)