-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeTrim.java
More file actions
379 lines (349 loc) · 13.2 KB
/
TreeTrim.java
File metadata and controls
379 lines (349 loc) · 13.2 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import java.util.Hashtable;
import java.util.LinkedList;
//////////////
//This is the treeTrim algorithm
///////////////
////////////////
public class TreeTrim {
Node head;
Node on;
LinkedList<Person> contestants;
LinkedList<Person> toMatch;
LinkedList<Person> selected;
Hashtable<Integer, LinkedList<Person>> ruledOut;
LinkedList<LinkedList<Person>> incompleateInfo;
LinkedList<LinkedList<Integer>> depthIndex;
LinkedList<Integer> numUnkown;
LinkedList<Integer> confirmedAtdepth;
int TBdepth = -1;
Node TBNode;
Node TBhead;
boolean isQueer;
public TreeTrim(LinkedList<Person> contestantsMale, LinkedList<Person> contestantsFemale) {
create(contestantsMale, contestantsFemale);
isQueer =false;
}
public TreeTrim(LinkedList<Person> contestants) {
LinkedList<Person> contestants2 = new LinkedList<>();
contestants2.addAll(contestants);
create(contestants, contestants2);
this.isQueer = true;
//ruleOutDuplicates();
}
private void create(LinkedList<Person> contestantsMale, LinkedList<Person> contestantsFemale) {
this.contestants = contestantsMale;
head = new Node(new LinkedList<>(),null,contestants, null, -1);
on = head;
TBhead = new Node(new LinkedList<>(),null,contestants, null, -1);
TBNode = TBhead;
toMatch = contestantsFemale;
selected = new LinkedList<>();
ruledOut = new Hashtable<>();
incompleateInfo = new LinkedList<>();
numUnkown = new LinkedList<>();
depthIndex = new LinkedList<>();
confirmedAtdepth = new LinkedList<>();
}
public Picks getCeremony(){
traverseUp();
traverseDown();
return createSelection();
}
public void traverseUp(){
//boolean onChopped = false;
//if(ruledOut.containsKey(on.depth)){
// onChopped = ruledOut.get(on.depth).contains(on.val);
//}
while ((on.children.isEmpty() || isOnChopped()) && on.parent !=null) { // && !(on.parent == null)){
//System.out.println("val e: " + on.val);
/// System.out.println("above e: " + on.above);
//System.out.println("children e: " + on.children);
//System.out.println("------------");
if (on.parent == null) {
//System.out.println("on val" + on.val);
} else {
Person nameprev = on.val;
on = on.parent;
on.children.remove(nameprev);
}
//if(ruledOut.containsKey(on.depth)){
// onChopped = ruledOut.get(on.depth).contains(on.val);
//}
}
traverseDown();
}
public void traverseDown(){
while (!on.children.isEmpty() && on.depth<toMatch.size()) {
//traverse
LinkedList<Person> toskip = new LinkedList<>();
//LinkedList<Person> available = new LinkedList<>();
//available.addAll(contestants);
//System.out.println("children " + on.children.toString());
if(ruledOut.containsKey(on.depth+1)) {
toskip.addAll(ruledOut.get(on.depth + 1));
}
LinkedList<Person> newabove = on.getNewAbove();
//System.out.println("contestants1: " + contestants );
boolean noRepeat = !confirmedAtdepth.contains(on.depth +1);
on.children = on.getAvailChildren(contestants, toskip, newabove, true);
//System.out.println("toskip: " + toskip.toString() );
//.out.println("val t: " + on.val + " depth:" + on.depth);
//System.out.println("above t: " + on.above + " newabove " +newabove);
//System.out.println("children t: " + on.children);
//System.out.println("------------");
if(newabove.size()< toMatch.size()){
Person newVal = queerNewChild(newabove);
if(on.children.isEmpty() && newVal==null) {
traverseUp();
}
else{
//System.out.println("ruledout "+ ruledOut.toString());
//System.out.println("data" + incompleateInfo.toString());
//System.out.println("depth -1:" + on.depth + "remove: " + toskip.toString());
if(isQueer){
//Person newVal = queerNewChild(newabove);
if(newVal==null){
on = on.addChildNode(on.children, toskip, newabove);
}else{
on = on.addChildNode(on.children, toskip, newabove, newVal);
}
}else{
on = on.addChildNode(on.children, toskip, newabove);
}
}
}
}
}
public Picks createSelection(){
Picks p;
if(isQueer){
p = createSelectionQueer();
}
else{
p = createSelectionStraight();
}
return p;
}
// returns a list of matches from the node you are on
public Picks createSelectionStraight(){
LinkedList<Person> selection = new LinkedList<>();
selection.addAll(on.above);
selection.add(on.val);
selection.addAll(on.children);
if(toMatch.size() != selection.size()){
System.out.println("Contestants and contestants to match are nor the same size!" + toMatch.size() +" " + selection.size());
System.out.println(selection);
}
LinkedList<Match> matches = new LinkedList<>();
selected = selection;
for (int i=0; i<toMatch.size(); i++){
Match m = new Match(toMatch.get(i), selection.get(i));
matches.add(m);
}
return new Picks(matches);
}
// returns a list of matches from the node you are on, removing duplicates
public Picks createSelectionQueer(){
LinkedList<Person> selection = new LinkedList<>();
selection.addAll(on.above);
selection.add(on.val);
selection.addAll(on.children);
if(toMatch.size() != selection.size()){
System.out.println("Contestants and contestants to match are nor the same size!" + toMatch.size() +" " + selection.size());
System.out.println(selection);
}
LinkedList<Match> matches = new LinkedList<>();
LinkedList<Person> in = new LinkedList<>();
selected = selection;
for (int i=0; i<toMatch.size(); i++){
if(!in.contains(toMatch.get(i))) {
Match m = new Match(toMatch.get(i), selection.get(i));
matches.add(m);
//in.add(toMatch.get(i));
in.add(selection.get(i));
}
}
return new Picks(matches);
}
public void recordCeremony(int numCorrect){
//System.out.println("hi");
if(numCorrect == toMatch.size()){
System.out.println("//////////////////");
System.out.println("//////////////////");
System.out.println("YOU WON");
System.out.println("//////////////////");
System.out.println("//////////////////");
}
else {
//System.out.println("hi2");
if (numCorrect == 0) {
//then all selected are wrong
for (int i = 0; i < selected.size(); i++) {
setRuledOut(i,selected.get(i));
}
}
else{
//System.out.println("hi4");
//add row of incompleate data
LinkedList<Person> r = new LinkedList<>();
r.addAll(selected);
incompleateInfo.add(r);
LinkedList<Integer> rowDepth = new LinkedList<>();
for(int x=0; x<selected.size(); x++){
rowDepth.add(x);
}
depthIndex.add(rowDepth);
numUnkown.add(numCorrect);
}
//System.out.println("hi5");
updateIncomplete();
//System.out.println("hi6");
}
//("ruledout "+ ruledOut.toString());
//System.out.println("data" + incompleateInfo.toString());
//System.out.println("data index" + depthIndex.toString());
//System.out.println("data beams" + numUnkown.toString());
}
//update table of incomplete information to try and narrow down wich are the correct matches from the group
public void updateIncomplete(){
for(int i=0; i<incompleateInfo.size(); i++){
//System.out.println("hi i" + i);
LinkedList<Person> data = incompleateInfo.get(i);
LinkedList<Integer> rowIndexes = depthIndex.get(i);
for(int j=0; j<data.size(); j++){ //for each row of data
int atDepth = rowIndexes.get(j);
if(ruledOut.containsKey(atDepth)){
if(ruledOut.get(atDepth).contains(data.get(j))){ //if can be ruled out
data.remove(j);
rowIndexes.remove(j);
}
}
}
if(data.size() == numUnkown.get(i)){
for(int k=0; k<data.size(); k++){
setFound(rowIndexes.get(k), data.get(k));
}
//i--;
}
}
}
//for queer season make sure not person can be paired with themselves
public void ruleOutDuplicates(){
for(int i=0; i< toMatch.size(); i++){
setRuledOut(i, toMatch.get(i));
}
}
//rule out matches
public void setRuledOut(int depth, Person p){
setRuledOutHelper(depth, p);
if(isQueer){
setRuledOutHelper(toMatch.indexOf(p), contestants.get(depth));
}
}
public void setRuledOutHelper(int depth, Person p){
LinkedList<Person> l = new LinkedList();
if (ruledOut.containsKey(depth)) {
l.addAll(ruledOut.get(depth));
}
if(!l.contains(p)){
l.add(p);
}
ruledOut.put(depth, l);
if(l.size() == (toMatch.size() -1)){ //if only one option left
Person person = null;
int i=0;
while(person == null){
if(!l.contains(contestants.get(i))){
person=contestants.get(i);
}
i++;
}
setFound(depth,person);
}
}
//found a confirmed match
public void setFound(int depth, Person p){
confirmedAtdepth.add(depth);
for(int i=0; i<toMatch.size(); i++){
if(i != depth){
//get ruled out for depth into l
LinkedList<Person> l = new LinkedList<>();
if(ruledOut.containsKey(i)){
l.addAll(ruledOut.get(i));
}
//add found to confirmed to to all other depths
if(!l.contains(p)){
l.add(p);
ruledOut.put(i,l);
}
}
}
//rule out all other than it found for found depth
LinkedList<Person> toRuleOut = new LinkedList<>();
toRuleOut.addAll(contestants);
toRuleOut.remove(p);
ruledOut.put(depth,toRuleOut);
}
//gets pair for truth both
public Match getTruthBooth(){
updateIncomplete();
Node tb = TBhead;
Node n = breadthFirstSearch(tb);
TBdepth = n.depth;
TBNode = n;
return new Match(toMatch.get(n.depth), n.val);
}
//for truthbooth traversal
public Node breadthFirstSearch(Node tb){
//get children
LinkedList<Person> toskip = new LinkedList<>();
if(ruledOut.containsKey(tb.depth+1)) {
toskip.addAll(ruledOut.get(tb.depth + 1));
}
LinkedList<Person> newabove = tb.getNewAbove();
tb.children = tb.getAvailChildren(contestants, toskip, newabove, false);
//get validChild
tb = tb.addChildNode(tb.children, toskip, newabove);
if(confirmedAtdepth.contains(tb.depth)){
tb = breadthFirstSearch(tb);
}
return tb;
}
//takes into account result from truthbooth
public void recordTruthBooth(boolean isMatch){
if(isMatch){
setFound(TBdepth, TBNode.val);
TBhead = TBNode;
}else{
setRuledOut(TBdepth, TBNode.val);
}
updateIncomplete();
}
//used to find out if the branch has been chopped off
public boolean isOnChopped(){
boolean onChopped = false;
LinkedList<Person> branch = new LinkedList<>();
branch.addAll(on.above);
branch.add(on.val);
for(int i = 0; i< branch.size(); i++){
if(ruledOut.containsKey(i)){
onChopped = ruledOut.get(i).contains(branch.get(i));
if(onChopped){
i= branch.size();
}
}
}
return onChopped;
}
//for queer season
//if p1 already matched with p2 then match p2 with p1
public Person queerNewChild(LinkedList<Person> newabove){
Person p = null;
for (int i = 0; i < newabove.size(); i++) {
if (toMatch.get(on.depth + 1).equals(newabove.get(i))) {
p = toMatch.get(i);
}
}
return p;
}
}