1 / 20

Iterator

Iterator. 5. 4. iter 1. 3. 2. 6. 5. 4. 1. 9. 2. while (iter.hasNext()) { ..... ..... iter.next()..... }. 5. iter 2. 3. 8. 1. 2. 7. 1. iter 3. In what order?. The internal information and structures are protected. 6. 2. 7. 4. 1. 3. 9. 8.

Télécharger la présentation

Iterator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterator 5 4 iter 1 3 2 6 5 4 1 9 2 while (iter.hasNext()) { ..... ..... iter.next()..... } 5 iter 2 3 8 1 2 7 1 iter 3 In what order? The internal information and structures are protected. 6 2 7 4 1 3 9 8

  2. import java.util.Iterator; publicclass BinarySearchTree<E extends Comparable<E>> implements ITKBST<E>, Iterable { ..... ..... ..... public Iterator iterator() { returnnew myIterator(); } ..... } Iterable An inner class that defines our iterator. Or, we can make it Anonymous

  3. Return path: 19 10 25 1 17 23 30 5 14 18 22 4 7 11 21 3 13 20

  4. Return path 19 10 25 1 17 23 30 5 14 18 22 4 7 1 11 21 10 3 13 20 19

  5. Return path 19 10 25 1 17 23 30 3 5 14 18 22 4 4 7 11 5 21 10 3 13 20 19

  6. Return path 19 10 25 1 17 23 30 5 14 18 22 4 7 11 7 21 10 3 13 20 19

  7. Return path 19 10 25 1 17 23 30 5 14 18 22 11 4 7 11 14 21 17 3 13 20 19

  8. Return path 19 10 25 1 17 23 30 5 14 18 22 13 4 7 11 14 21 17 3 13 20 19

  9. /**********ReturnanAnonymousclassasaniterator***********/ public Iterator iterator() { returnnew Iterator() { privatebooleanfirstTime=true; private LinkedListStack<TNode<E>> st = new LinkedListStack<TNode<E>>(); publicboolean hasNext() { if (root == null) returnfalse; if (firstTime) { firstTime = false; pushLeft(root); } return (!st.empty()); } privatevoid pushLeft(TNode<E> n) { st.push(n); while (st.peek().left != null) st.push(st.peek().left); } .... .... } }

  10. public Object next() { if (st.empty()) thrownew NoSuchElementException(); TNode<E> x = st.pop(); if (x.right != null) pushLeft(x.right); return x.data; } privatevoid pushLeft(TNode<E> n) { st.push(n); while (st.peek().left != null) st.push(st.peek().left); } Iterator (countined) 19 10 17 11 14 18 14 11 10 17 19 19 13

  11. Breath-first Search 19 10 25 1 17 23 30 5 14 18 19

  12. Breath-first Search 19 10 25 1 17 23 30 5 14 18 10 25 19

  13. Breath-first Search 19 10 25 1 17 23 30 5 14 18 25 1 17 19 10

  14. Breath-first Search 19 10 25 1 17 23 30 5 14 18 1 17 23 30 19 10 25

  15. Breath-first Search 19 10 25 1 17 23 30 5 14 18 17 23 30 5 19 10 25 1

  16. Breath-first Search 19 10 25 1 17 23 30 5 14 18 23 30 5 14 18 19 10 25 1 17

  17. /**********Returnabreath-firstiterator***********/ public Iterator Biterator() {returnnew BreathIterator(); } publicclass BreathIterator implements Iterator { private LinkedListQueue<TNode<E>> q = new LinkedListQueue<TNode<E>>(); public BreathIterator() { if (root!= null) q.offer(root); } publicboolean hasNext() { if (q.empty()) returnfalse; returntrue; } public Object next() { if (q.empty()) thrownew NoSuchElementException(); TNode<E> x = q.poll(); if (x.left != null) q.offer(x.left); if (x.right != null) q.offer(x.right); return x.data; } publicvoid remove() {} } /**********Endoftheabreath-firstiterator******/

  18. Depth-first Search 19 10 25 1 17 23 30 5 14 18 22 4 7 7 11 21 17 3 13 20 25

  19. /**********Returnadepth-firstniterator***********/ public Iterator Diterator() {returnnew DepthIterator(); } publicclass DepthIterator implements Iterator { private TNode<E> currentN = root; private LinkedListStack<TNode<E>> st = new LinkedListStack<TNode<E>>(); publicboolean hasNext() { if (currentN == null) returnfalse; returntrue; } .... .... .... } /**********Endofthedepth-firstniterator**********/

  20. /**********Returnadepth-firstniterator***********/ ..... ..... public Object next() { if (currentN == null) thrownew NoSuchElementException(); TNode<E> x = currentN; if (currentN.left != null && currentN.right!= null) { st.push(currentN.right); currentN = currentN.left; } else if (currentN.left == null && currentN.right!= null) { currentN = currentN.right; } else if (currentN.left != null && currentN.right == null) { currentN = currentN.left; } else if (!st.empty()) currentN = st.pop(); elsecurrentN = null; return x.data; } ........ /**********Endofthedepth-firstniterator**********/

More Related