/** * */ package dataStructures; /** * @author Ricardo Gaspar Nr. 35277 * @author Hugo Ant—nio Nr. 34334 Turno P2 Docente Vasco Amaral */ public class BSTInorderIterator, V> implements Iterator> { /** * */ private static final long serialVersionUID = 1L; protected BSTNode root; protected Stack> path; /** * */ public BSTInorderIterator(BSTNode root) { this.root = root; rewind(); } @Override public boolean hasNext() { return (!path.isEmpty()); } @Override public Entry next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException(); BSTNode node = path.pop(); Entry entryToReturn = node.getEntry(); // Se tem filho direito, achar o m’nimo desse filho e empilhar if (node.getRight() != null) minNode(node.getRight()); return entryToReturn; } @Override public void rewind() { path = new StackInList>(); minNode(root); } /** * Realiza o percurso atŽ ao n— cuja chave tem o valor m’nimo. Empilha os * n—s por onde vai passando atŽ chegar ao fim. * * @param node * N— de partida. * @return N— cujo valor da chave Ž m’nimo. */ private void minNode(BSTNode node) { while (node != null) { path.push(node); node = node.getLeft(); } } }