Advertisement
dcndrd

TreeIterator

Oct 27th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package br.uefs.ecomp.treeStock.util;
  7.  
  8. import java.util.Iterator;
  9.  
  10. /**
  11. *
  12. * @author dcandrade
  13. */
  14. public class inOrderIterator implements Iterator {
  15.  
  16. LinkedList stack;
  17.  
  18. public inOrderIterator(Node root) {
  19. stack = new LinkedList();
  20. this.inOrder(root);
  21. }
  22.  
  23. public void inOrder(Node root) {
  24. if (root != null) {
  25. this.inOrder(root.getLeftChild());
  26. stack.insereFinal(root.getData());
  27. this.inOrder(root.getRightChild());
  28. }
  29. }
  30.  
  31. @Override
  32. public boolean hasNext() {
  33. return !this.stack.estaVazia();
  34. }
  35.  
  36. @Override
  37. public Object next() {
  38. if (this.hasNext()) {
  39. try {
  40. return this.stack.removeInicio();
  41. } catch (DadoNaoEncontradoException ex) {
  42. return null;
  43. }
  44. } else {
  45. return null;
  46. }
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement