-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSett.java
More file actions
270 lines (248 loc) · 11.1 KB
/
Sett.java
File metadata and controls
270 lines (248 loc) · 11.1 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//////////////////// ALL ASSIGNMENTS INCLUDE THIS SECTION /////////////////////
//
// Title: Access Control
// Files: User.Java, AccessControlTest.java, AccessControl.java
// Course: CS300, Fall, 2018
//
// Author: Daniel Chu
// Email: dchu22@wisc.edu
// Lecturer's Name: Gary Dahl
//
//////////////////// PAIR PROGRAMMERS COMPLETE THIS SECTION ///////////////////
//
// Partner Name: Tolga Beser
// Partner Email: tbeser@wisc.edu
// Partner Lecturer's Name: Gary Dahl
//
// VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
// _x_ Write-up states that pair programming is allowed for this assignment.
// _x_ We have both read and understand the course Pair Programming Policy.
// _x_ We have registered our team prior to the team registration deadline.
//
///////////////////////////// CREDIT OUTSIDE HELP /////////////////////////////
//
// Students who get help from sources other than their partner must fully
// acknowledge and credit those sources of help here. Instructors and TAs do
// not need to be credited here, but tutors, friends, relatives, room mates,
// strangers, and others do. If you received no outside help from either type
// of source, then please explicitly indicate NONE.
//
// Persons: N/A
// Online Sources: N/A
//
/////////////////////////////// 80 COLUMNS WIDE ///////////////////////////////
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
/**
* This class represents a Sett, where a group of Badgers live together. Each Sett is organized
* as a BST of Badger nodes.
*
*/
public class Sett {
private Badger topBadger; // top of the BST
/**
* Constructs an empty Sett.
*/
public Sett() {
topBadger = null;
}
/**
* Empties this Sett, to no longer contain any Badgers.
*/
public void clear() {
topBadger = null; // removing topBadger causes references to other badgers to be lost
}
/**
* Counts how many Badgers live in this Sett.
* @return The number of Badgers living in this Sett.
*/
public int countBadger() {
return countHelper(topBadger);
}
/**
* This recursive helper method is used to help count the number of Badgers in this Sett.
* @param current - The current Badger that is the root of a (sub) tree that we are counting the number of Badgers within.
* @return the number of Badgers living in the Sett rooted at the current Badger.
*/
private int countHelper(Badger current) {
if(current.getLeftLowerNeighbor()==null) { // if the left is empty
if(current.getRightLowerNeighbor()==null) { // if the right is also empty
return 1; // bottom of the branch. return 1
}
else { // if the right is not empty
return countHelper(current.getRightLowerNeighbor())+1; // increment value and recurse
}
} else { // if the left is not empty
if(current.getRightLowerNeighbor()!=null) {
return countHelper(current.getLeftLowerNeighbor())
+ countHelper(current.getRightLowerNeighbor());
} else {
return countHelper(current.getLeftLowerNeighbor())+1; // increment value and recurse
}
}
}
/**
* Finds a Badger of a specified size in this Sett.
* @param size - The size of the Badger object to search for and return.
* @return The Badger found with the specified size.
* @throws java.util.NoSuchElementException - When there is no Badger in this Sett with the
* specified size. The message within this exception must read "WARNING: failed to find a
* badger with size {size} in the sett", where {size} needs to be replaced with the specified
* size parameter.
*/
public Badger findBadger(int size) {
Badger found = findHelper(topBadger, size);
return found;
}
/**
* This recursive helper method is used to help find a Badger within this Sett.
* @param current - The current Badger that is the root of a (sub) tree that we are searching for a Badger with the specified size within.
* @param size - The size of the Badger object to search for and return.
* @return The Badger found with the specified size.
* @throws java.util.NoSuchElementException - When there is no Badger in this Sett with the
* specified size. The message within this exception must read "WARNING: failed to find a
* badger with size {size} in the sett", where {size} needs to be replaced with the specified
* size parameter.
*/
private Badger findHelper(Badger current, int size) {
if(current==null) { // if the topBadger is null, it throws
throw new NoSuchElementException("WARNING: failed to find a badger with size " + size
+ " in the sett");
}
if(current.getSize()<size) { // compares the current badger's size to the size
if(current.getRightLowerNeighbor()==null) { // if right tree null, no badger of size exists
throw new NoSuchElementException("WARNING: failed to find a badger with size " + size
+ " in the sett");
} else {
return findHelper(current.getRightLowerNeighbor(), size); // recursive call
}
} else if(current.getSize()>size){ // compares the current badger's size to the size
if(current.getLeftLowerNeighbor()==null) { // if left tree null, no badger of size exists
throw new NoSuchElementException("WARNING: failed to find a badger with size " + size
+ " in the sett");
} else {
return findHelper(current.getLeftLowerNeighbor(), size); // recursive call
}
} else {
return current; // returns the badger
}
}
/**
* Gets all Badgers living in the Sett as a list in ascending order of their size: smallest one
* in the front (at index zero), through the largest one at the end (at index size-1).
* @return A list of all Badgers living in the Sett in ascending order by size.
*/
public List<Badger> getAllBadgers(){
// locally creates a list to return
List<Badger> allBadgers = new ArrayList<Badger>(countBadger());
getAllHelper(topBadger, allBadgers);
return allBadgers;
}
/**
* This recursive helper method is used to help collect the Badgers within this Sett into a List.
* @param current - The current Badger that is the root of a (sub) tree that we are collecting all contained Badgers within, into the allBadgers List.
* @param allBadgers - The list of all Badgers living in the Sett that is rooted at the current Badger node. The contents of this list should be in ascending order by Badger size.
*/
public void getAllHelper(Badger current, List<Badger> allBadgers) {
// the method goes left > current > right
if(current.getLeftLowerNeighbor()!=null) { // if left is not null, recursive calls with left
getAllHelper(current.getLeftLowerNeighbor(), allBadgers);
}
allBadgers.add(current); // adds the current value
if(current.getRightLowerNeighbor()!=null) { // if right is not null, recursive calls with right
getAllHelper(current.getRightLowerNeighbor(), allBadgers);
}
}
/**
* Computes the height of the Sett, as the number of nodes from root to the deepest leaf Badger node.
* @return The depth of this Sett.
*/
public int getHeight() {
return getHeightHelper(topBadger);
}
/**
* This recursive helper method is used to help compute the height of this Sett.
* @param current - The current Badger that is the root of a (sub) tree that we are calculating the height of.
* @return The height of the (sub) tree that we are calculating.
*/
private int getHeightHelper(Badger current) {
if (current == null) { // if null the branch is ended
return 0; // adds 0
} else { // recursive calls both sides and checks for larger, then increments by one
return Math.max(getHeightHelper(current.getLeftLowerNeighbor()),
getHeightHelper(current.getRightLowerNeighbor())) + 1;
}
}
/**
* Retrieves the largest Badger living in this Sett.
* @return The largest Badger living in this Sett.
*/
public Badger getLargestBadger() {
Badger current = topBadger;
while(current.getRightLowerNeighbor()!=null) { // loops through rightmost badger
current = current.getRightLowerNeighbor();
}
return current; // returns rightmost badger at bottom
}
/**
* Retrieve the top Badger within this Sett (the one that was settled first).
* @return The Badger living on the top of the current Sett.
*/
public Badger getTopBadger() {
return topBadger;
}
/**
* Checks whether this Sett is empty.
* @return true if this Sett is empty, false otherwise.
*/
public boolean isEmpty() {
return topBadger == null;
}
/**
* Creates a new Badger object with the specified size, and inserts them into this Sett (BST).
* @param size - The size of the new Badger that will be settled.
* @throws java.lang.IllegalArgumentException - When a Badger with the specified size already
* exists within this Sett. The message in this exception must read: "WARNING: failed to
* settle the badger with size {size}, as there is already a badger with the same size in this
* sett", where {size} needs to be replaced with the specified size parameter.
*/
public void settleBadger(int size) {
settleHelper(topBadger, new Badger(size)); // starts recursive loop with new badger
}
/**
* This recursive helper method is used to help settle a new Badger within this Sett.
* @param current - The current Badger (previously settled within this Sett) that we are
* considering settling the newBadger beneath (either to its left or right).
* @param newBadger - The new Badger that needs to be settled within this Sett.
* @throws java.lang.IllegalArgumentException - When a Badger with the specified size already
* exists within this Sett. The message in this exception must read: "WARNING: failed to settle
* the badger with size {size}, as there is already a badger with the same size in this sett",
* where {size} needs to be replaced with the specified size parameter.
*/
private void settleHelper(Badger current, Badger newBadger) {
if (isEmpty()) { // checks if the list is empty
topBadger = newBadger; // sets top badger to the new badger
}
if (current != null && newBadger != null) { // check for no null values
if (newBadger.getSize() == current.getSize()) { // throws if size already exists
throw new java.lang.IllegalArgumentException(
"WARNING: failed to settle the badger with size " + newBadger.getSize()
+ ", as there is already a badger with the same size in this sett");
}
if (newBadger.getSize() > current.getSize()) { // if new badger greater than current
if (current.getRightLowerNeighbor() == null) { // if there is no further node
current.setRightLowerNeighbor(newBadger);
} else { // otherwise recursive call with lower node
settleHelper(current.getRightLowerNeighbor(), newBadger);
}
} else { // if new badger less than current node
if (current.getLeftLowerNeighbor() == null) { // if there is no further node
current.setLeftLowerNeighbor(newBadger);
} else { // otherwise recursive call with lower node
settleHelper(current.getLeftLowerNeighbor(), newBadger);
}
}
}
}
}