Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assigment5;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class BinaryTreeInOrderIterator implements Iterator{
- private Stack stack;
- public BinaryTreeInOrderIterator(BinaryNode root)
- {
- stack = new StackAsDynamicArray();
- fillTheleft(root);
- }
- public boolean hasNext()
- {
- return (!(stack.isEmpty()));
- }
- public void fillTheleft(BinaryNode current)
- {
- while (current != null )
- {
- stack.push(current);
- current = current.left;
- }
- }
- public Object next()
- {
- if(!hasNext())
- throw new NoSuchElementException();
- BinaryNode current = (BinaryNode) stack.pop();
- Object result = current.data;
- current = current.right;
- while(current != null) {
- stack.push(current);
- current = current.left;
- }
- return result;
- }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement