-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMyBSTIterator.java
127 lines (114 loc) · 3.41 KB
/
MyBSTIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyBSTIterator<K extends Comparable<K>, V> extends MyBST<K, V> {
abstract class MyBSTNodeIterator<T> implements Iterator<T> {
MyBSTNode<K, V> next;
MyBSTNode<K, V> lastVisited;
/**
* Constructor that initializes the node iterator
*
* @param first The initial node that next points
*/
MyBSTNodeIterator(MyBSTNode<K, V> first) {
next = first;
lastVisited = null;
}
/**
* This method is used for determining if the next pointer in the
* iterator points to null.
*
* @return If next is null based on the current position of iterator
*/
public boolean hasNext() {
return next != null;
}
MyBSTNode<K, V> nextNode() {
// TODO
return null;
}
/**
* TODO: add inline comments for this method to demonstrate your
* understanding of this method.
*
* This method removes the last visited node from the tree.
*/
public void remove() {
if (lastVisited == null) {
throw new IllegalStateException();
}
if (lastVisited.getRight() != null &&
lastVisited.getLeft() != null) {
next = lastVisited;
}
MyBSTIterator.this.remove(lastVisited.getKey());
lastVisited = null;
}
}
/**
* BST Key iterator class that extends the node iterator.
*/
class MyBSTKeyIterator extends MyBSTNodeIterator<K> {
MyBSTKeyIterator(MyBSTNode<K, V> first) {
super(first);
}
/**
* This method advance the iterator and returns a node key
*
* @return K the next key
*/
public K next() {
return super.nextNode().getKey();
}
}
/**
* BST value iterator class that extends the node iterator.
*/
class MyBSTValueIterator extends MyBSTNodeIterator<V> {
/**
* Call the constructor method from node iterator
*
* @param first The initial value that next points
*/
MyBSTValueIterator(MyBSTNode<K, V> first) {
super(first);
}
/**
* This method advance the iterator and returns a node value
*
* @return V the next value
*/
public V next() {
return super.nextNode().getValue();
}
}
/**
* This method is used to obtain an iterator that iterates through the
* value of BST.
*
* @return The value iterator of BST.
*/
public MyBSTKeyIterator getKeyIterator() {
MyBSTNode<K, V> curr = root;
if (curr != null) {
while (curr.getLeft() != null) {
curr = curr.getLeft();
}
}
return new MyBSTKeyIterator(curr);
}
/**
* This method is used to obtain an iterator that iterates through the
* value of BST.
*
* @return The value iterator of BST.
*/
public MyBSTValueIterator getValueIterator() {
MyBSTNode<K, V> curr = root;
if (curr != null) {
while (curr.getLeft() != null) {
curr = curr.getLeft();
}
}
return new MyBSTValueIterator(curr);
}
}