200 likes | 427 Views
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.
E N D
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
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
Return path: 19 10 25 1 17 23 30 5 14 18 22 4 7 11 21 3 13 20
Return path 19 10 25 1 17 23 30 5 14 18 22 4 7 1 11 21 10 3 13 20 19
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
Return path 19 10 25 1 17 23 30 5 14 18 22 4 7 11 7 21 10 3 13 20 19
Return path 19 10 25 1 17 23 30 5 14 18 22 11 4 7 11 14 21 17 3 13 20 19
Return path 19 10 25 1 17 23 30 5 14 18 22 13 4 7 11 14 21 17 3 13 20 19
/**********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); } .... .... } }
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
Breath-first Search 19 10 25 1 17 23 30 5 14 18 19
Breath-first Search 19 10 25 1 17 23 30 5 14 18 10 25 19
Breath-first Search 19 10 25 1 17 23 30 5 14 18 25 1 17 19 10
Breath-first Search 19 10 25 1 17 23 30 5 14 18 1 17 23 30 19 10 25
Breath-first Search 19 10 25 1 17 23 30 5 14 18 17 23 30 5 19 10 25 1
Breath-first Search 19 10 25 1 17 23 30 5 14 18 23 30 5 14 18 19 10 25 1 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******/
Depth-first Search 19 10 25 1 17 23 30 5 14 18 22 4 7 7 11 21 17 3 13 20 25
/**********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**********/
/**********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**********/