Title: Iterator
1Iterator
5
iter 1
4
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
4
7
1
3
8
9
2Iterable
import java.util.Iterator public class
BinarySearchTreeltE extends ComparableltEgtgt
implements ITKBSTltEgt, Iterable
..... ..... ..... public Iterator
iterator() return new myIterator()
.....
An inner class that defines our iterator. Or, we
can make it Anonymous
3 Return path
19
25
10
1
23
17
30
5
18
14
22
7
4
11
21
3
13
20
4 Return path
19
25
10
1
23
17
30
5
18
14
22
7
4
1
11
21
10
3
13
20
19
5 Return path
19
25
10
1
23
17
30
3
5
18
14
22
4
7
4
11
5
21
10
3
13
20
19
6 Return path
19
25
10
1
23
17
30
5
18
14
22
7
4
11
7
21
10
3
13
20
19
7 Return path
19
25
10
1
23
17
30
5
18
14
22
11
7
4
11
14
21
17
3
13
20
19
8 Return path
19
25
10
1
23
17
30
5
18
14
22
13
7
4
11
14
21
17
3
13
20
19
9/ Return an Anonymous class as an
iterator / public Iterator iterator()
return new Iterator() private boolean
firstTimetrue private LinkedListStackltTNodeltEgt
gt st new LinkedListStackltTNodeltEgtgt()
public boolean hasNext() if (root
null) return false if (firstTime)
firstTime false pushLeft(root)
return (!st.empty()) private void
pushLeft(TNodeltEgt n) st.push(n) while
(st.peek().left ! null) st.push(st.peek().le
ft) .... ....
10Iterator (countined)
public Object next() if (st.empty()) throw
new NoSuchElementException() TNodeltEgt x
st.pop() if (x.right ! null)
pushLeft(x.right) return x.data
private void pushLeft(TNodeltEgt n)
st.push(n) while (st.peek().left !
null) st.push(st.peek().left)
19
10
17
11
18
14
14
11
17
10
19
19
13
11 Breath-first Search
19
25
10
23
17
30
1
18
5
14
19
12 Breath-first Search
19
25
10
23
17
30
1
18
5
14
10
25
19
13 Breath-first Search
19
25
10
23
17
30
1
18
5
14
25
1
17
19
10
14 Breath-first Search
19
25
10
23
17
30
1
18
5
14
1
17
23
30
19
10
25
15 Breath-first Search
19
25
10
23
17
30
1
18
5
14
17
23
30
5
19
10
25
1
16 Breath-first Search
19
25
10
23
17
30
1
18
5
14
23
30
5
14
18
19
10
25
1
17
17/ Return a breath-first iterator
/ public Iterator Biterator() return
new BreathIterator() public class
BreathIterator implements Iterator private
LinkedListQueueltTNodeltEgtgt q new
LinkedListQueueltTNodeltEgtgt()
public BreathIterator() if
(root! null) q.offer(root) public
boolean hasNext() if (q.empty()) return false
return true public Object next()
if (q.empty()) throw new NoSuchElementException
() TNodeltEgt x q.poll() if (x.left ! null)
q.offer(x.left) if (x.right ! null)
q.offer(x.right) return x.data public
void remove() / End of the a
breath-first iterator /
18 Depth-first Search
19
25
10
1
23
17
30
5
18
14
22
7
4
7
11
21
17
3
13
20
25
19/ Return a depth-first n iterator
/ public Iterator Diterator() return
new DepthIterator() public class
DepthIterator implements Iterator private
TNodeltEgt currentN root private
LinkedListStackltTNodeltEgtgt st new
LinkedListStackltTNodeltEgtgt() public
boolean hasNext() if (currentN null) return
false return true ....
.... .... / End of the
depth-first n iterator /
20/ Return a depth-first n iterator
/ ..... ..... public Object next()
if (currentN null) throw new
NoSuchElementException() TNodeltEgt 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() else
currentN null return x.data ........ /
End of the depth-first n iterator
/