-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTIterator.java
More file actions
146 lines (130 loc) · 4.08 KB
/
Copy pathBSTIterator.java
File metadata and controls
146 lines (130 loc) · 4.08 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Student ID: V00816592
* Date: 07/31/15
* Program Name: BSTIterator.java
* Program Description: Iterator used to traverse a BSTRefBased.java binary search tree
* @author Mike Zastre, UVic
* @author Isaac Sahle, UVic
* Part of CSC 115, Summer 2015, Assignment #5
*/
import java.util.LinkedList;
public class BSTIterator implements java.util.Iterator<WordRefs> {
private BSTRefBased t;
private WordRefs currentItem;
private LinkedList<WordRefs> list;
public BSTIterator(BSTRefBased t) {
this.t = t;
currentItem = null;
list = new LinkedList<>();
setInorder();
}
/**
* Computes if list is not empty
* @return true if the list is not empty, false otherwise
*/
public boolean hasNext() {
return !(list.isEmpty());
}
/**
* Retrieves next item in list, exception thrown if there is no item to retrieve
* @return Wordrefs item from list
*/
public WordRefs next() throws java.util.NoSuchElementException {
return (list.remove());
}
/**
* Throws exception, removal not allowed for this implementation
*/
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* Clears list and sets list items to the preorder traversal
*/
public void setPreorder() {
list.clear();
preorder(t.getRoot());
}
/**
* Clears list and sets list items to the inorder traversal
*/
public void setInorder() {
list.clear();
inorder(t.getRoot());
}
/**
* Clears list and sets list items to the postorder traversal
*/
public void setPostorder() {
list.clear();
postorder(t.getRoot());
}
/**
* Preorder method has the actual access to data and
* adds items to list in preorder.
* @param node the node which is the starting point of traversal
*/
private void preorder(TreeNode node) {
if(node == null){
return;
}else{
list.add(node.item);
preorder(node.left);
preorder(node.right);
}
}
/**
* Inorder method has the actual access to data and
* adds items to list inorder.
* @param node the node which is the starting point of traversal
*/
private void inorder(TreeNode node) {
if(node == null){
return;
}else{
inorder(node.left);
list.add(node.item);
inorder(node.right);
}
}
/**
* Postorder method has the actual access to data and
* adds items to list in postorder.
* @param node the node which is the starting point of traversal
*/
private void postorder(TreeNode node) {
if(node == null){
return;
}else{
postorder(node.left);
postorder(node.right);
list.add(node.item);
}
}
public static void main(String[] args) {
BSTRefBased t;
BSTIterator it;
boolean result;
String message;
message = "Test 1: set Inorder -- ";
t = new BSTRefBased();
t.insert("humpty");
t.insert("dumpty");
it = new BSTIterator(t);
result = it.next().getWord().equals("dumpty") && it.next().getWord().equals("humpty");
System.out.println(message + (result ? "passed" : "FAILED"));
message = "Test 2: set Preorder -- ";
it.setPreorder();
result = it.next().getWord().equals("humpty") && it.next().getWord().equals("dumpty");
System.out.println(message + (result ? "passed" : "FAILED"));
message = "Test 3: set Postorder -- ";
t = new BSTRefBased();
t.insert("humpty");
t.insert("dumpty");
t.insert("sat");
it = new BSTIterator(t);
it.setPostorder();
result = it.next().getWord().equals("dumpty") && it.next().getWord().equals("sat") && it.next().getWord().equals("humpty");
System.out.println(message + (result ? "passed" : "FAILED"));
}
}