/** * */ package dataStructures1112; /** * @author Daniel * */ public class BSTInOrderIterator implements Iterator> { /** * */ private static final long serialVersionUID = 1L; private Stack> st; private BSTNode root; public BSTInOrderIterator(BSTNode node){ this.st = new StackInList>(); this.root = node; this.rewind(); } @Override public boolean hasNext() { return !(st.isEmpty()); } @Override public Entry next() throws NoSuchElementException { if(!this.hasNext()) throw new NoSuchElementException(); BSTNode next = st.pop(); this.pushPathToMinimum(next.getRight()); return next.getEntry(); } @Override public void rewind() { this.pushPathToMinimum(root); } private void pushPathToMinimum(BSTNode node) { while(node != null) { st.push(node); node = node.getLeft(); } } }