Repository navigation
Expand file tree
/
Copy pathPartialTreeList.java
More file actions
174 lines (150 loc) · 3.72 KB
/
Copy pathPartialTreeList.java
File metadata and controls
174 lines (150 loc) · 3.72 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
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
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 {
if (rear != null){
if (rear.next == rear){
Node treeNode = rear;
rear = null;
this.size--;
return treeNode.tree;
}
else if (rear.next != null){
Node treeNode = rear.next;
rear.next = rear.next.next;
this.size--;
return treeNode.tree;
}
}
else {
throw new NoSuchElementException("Your List is empty");
}
return null;
}
/**
* 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 {
if (rear == rear.next){
Vertex ver = rear.tree.getRoot();
if(ver.equals(vertex.getRoot())){
size = 0;
PartialTree part = rear.tree;
rear = null;
return part;
}
}
return null;
}
/**
* 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();
}
}
}