java - Iterate through an AVL tree -
i have implemented avl tree , wanted write iterator, searches in pre order. have peace of code , nullpointer in "stack.push(current)" , 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 :)
your root
object never declared or instantiated. setting current = root;
in current context, root
nothing.
public void iterator() { counter++; stack = new stack<node>(); current = root; stack.empty(); }
Comments
Post a Comment