-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartialTreeList.java
245 lines (195 loc) · 4.99 KB
/
PartialTreeList.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package apps;
import java.util.Iterator;
import java.util.NoSuchElementException;
import structures.Vertex;
public class PartialTreeList implements Iterable<PartialTree> {
/**
* Inner class - to build the partial tree circular linked list
*
*/
public static class Node {
/**
* Partial tree
*/
public PartialTree tree;
/**
* Next node in linked list
*/
public Node next;
/**
* Initializes this node by setting the tree part to the given tree,
* and setting next part to null
*
* @param tree Partial tree
*/
public Node(PartialTree tree) {
this.tree = tree;
next = null;
}
}
/**
* Pointer to last node of the circular linked list
*/
private Node rear;
/**
* Number of nodes in the CLL
*/
private int size;
/**
* Initializes this list to empty
*/
public PartialTreeList() {
rear = null;
size = 0;
}
/**
* Adds a new tree to the end of the list
*
* @param tree Tree to be added to the end of the list
*/
public void append(PartialTree tree) {
Node ptr = new Node(tree);
if (rear == null) {
ptr.next = ptr;
} else {
ptr.next = rear.next;
rear.next = ptr;
}
rear = ptr;
size++;
}
/**
* Removes the tree that is at the front of the list.
*
* @return The tree that is removed from the front
* @throws NoSuchElementException If the list is empty
*/
public PartialTree remove()
throws NoSuchElementException {
/* COMPLETE THIS METHOD */
if(size <= 0){
throw new NoSuchElementException();
}
if(size != 1){
Node firstNode = rear.next;
rear.next = firstNode.next;
size--;
firstNode.next = null;
return firstNode.tree;
} else {
Node retNode = rear;
rear = null;
size = 0;
retNode.next = null;
return retNode.tree;
}
}
/**
* Removes the tree in this list that contains a given vertex.
*
* @param vertex Vertex whose tree is to be removed
* @return The tree that is removed
* @throws NoSuchElementException If there is no matching tree
*/
public PartialTree removeTreeContaining(Vertex vertex)
throws NoSuchElementException {
/* COMPLETE THIS METHOD */
PartialTree ptRemove = null;
if(rear == null){
throw new NoSuchElementException();
}
if (rear.next == rear){
if(rear.tree.getRoot().name.equals(vertex.getRoot().name)){
PartialTree returnedTree = rear.tree;
rear = null;
size --;
return returnedTree;
} else {
throw new NoSuchElementException();
}
}
Node pt = rear.next;
Node prev = rear;
PartialTree treeLL = null;
/* if(vertexFound){
ptRemove = tmptree;
removeNode(tmp);
break;
}
tmp = tmp.next;
} while (tmp != rear);
if (ptRemove == null){
return null;
} else {
return ptRemove;
}
} */
do {
Vertex tmp = pt.tree.getRoot();
Vertex check = vertex.getRoot();
if(tmp.name.equals(check.name)){
treeLL = pt.tree;
if(pt == rear){
rear = prev;
}
prev.next = pt.next;
size--;
return treeLL;
}
prev = pt;
pt = pt.next;
} while(pt != rear.next);
throw new NoSuchElementException();
}
/* boolean vertexFound = vertexCheck(tmptree, vertex);
private boolean vertexCheck(PartialTree partialTree, Vertex vertex){
Vertex pTree = vertex;
while(pTree.parent != pTree){
pTree = pTree.parent;
}
return pTree == partialTree.getRoot();
}
*/
/**
* Gives the number of trees in this list
*
* @return Number of trees
*/
public int size() {
return size;
}
/**
* Returns an Iterator that can be used to step through the trees in this list.
* The iterator does NOT support remove.
*
* @return Iterator for this list
*/
public Iterator<PartialTree> iterator() {
return new PartialTreeListIterator(this);
}
private class PartialTreeListIterator implements Iterator<PartialTree> {
private PartialTreeList.Node ptr;
private int rest;
public PartialTreeListIterator(PartialTreeList target) {
rest = target.size;
ptr = rest > 0 ? target.rear.next : null;
}
public PartialTree next()
throws NoSuchElementException {
if (rest <= 0) {
throw new NoSuchElementException();
}
PartialTree ret = ptr.tree;
ptr = ptr.next;
rest--;
return ret;
}
public boolean hasNext() {
return rest != 0;
}
public void remove()
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
}