@@ -44,7 +44,6 @@ public TournamentTree(double t, ArrayList<P> leaves, TournamentTreeWinner<P> win
44
44
public boolean audit (double t ) {
45
45
ArrayList <Double > distances = new ArrayList <>();
46
46
for (TournamentNode <P > leaf : leaves ) {
47
- leaf .getWinner ().updatePosition (t );
48
47
distances .add (winnerFunction .computeValue (t , leaf .getWinner ()));
49
48
}
50
49
Collections .sort (distances );
@@ -59,6 +58,10 @@ public boolean audit(double t) {
59
58
valid = false ;
60
59
LOGGER .info ("The tree itself is not valid :(" );
61
60
}
61
+ if (!root .getParent ().isNull ()) {
62
+ valid = false ;
63
+ LOGGER .info ("The root is not the root :(" );
64
+ }
62
65
return valid ;
63
66
}
64
67
@@ -72,10 +75,10 @@ public boolean validate(TournamentNode<P> node) {
72
75
if (!valid ) return false ; // no need to check, TODO print out offenders
73
76
74
77
if (!node .getLeftChild ().isNull ()) {
75
- valid = node .getLeftChild ().getKey () < node .getKey () && isBalanced (node .getLeftChild (), node );
78
+ valid = node .getLeftChild ().getKey () <= node .getKey () && isBalanced (node .getLeftChild (), node );
76
79
}
77
80
if (!node .getRightChild ().isNull ()) {
78
- valid = valid && node .getKey () <= node .getRightChild ().getKey () && isBalanced (node .getRightChild (), node );
81
+ valid = valid && node .getKey () < node .getRightChild ().getKey () && isBalanced (node .getRightChild (), node );
79
82
}
80
83
return valid ;
81
84
}
@@ -105,7 +108,7 @@ public void process(TournamentEvent<P> event) {
105
108
event .getNode ().setWinner (event .getNode ().getLeftChild ().getWinner ());
106
109
}
107
110
108
- event .getNode ().createEvent (solver , event .getFailureTime (), winnerFunction , true );
111
+ eq . add ( event .getNode ().createEvent (solver , event .getFailureTime (), winnerFunction , true ) );
109
112
110
113
// percolate
111
114
updateWinners (event .getFailureTime (), event .getNode ().getParent ());
@@ -143,17 +146,17 @@ public void setWinner(TournamentNode<P> winner) {
143
146
public TournamentNode <P > insert (double t , int key , P winner ) {
144
147
if (root == null ) {
145
148
root = new TournamentNode <>(key , winner );
146
- root .setRightChild (new TournamentNode <>(key , winner ));
149
+ root .setLeftChild (new TournamentNode <>(key , winner ));
147
150
return root ;
148
151
}
149
152
150
153
// else -- NOTE: I do not use a stack to keep track of the traversed path. It can be done by keeping a parent
151
154
// pointer in every node. Requires more space (obviously), but it makes the code nicer imo.
152
155
TournamentNode <P > leaf = root ;
153
156
154
- // find the location to insert, which is always a leaf
157
+ // find the location to insert
155
158
while (!leaf .isNull ()) {
156
- if (key < leaf .getKey ()) leaf = leaf .getLeftChild ();
159
+ if (key <= leaf .getKey ()) leaf = leaf .getLeftChild ();
157
160
else leaf = leaf .getRightChild ();
158
161
}
159
162
@@ -167,22 +170,23 @@ public TournamentNode<P> insert(double t, int key, P winner) {
167
170
TournamentNode <P > new_node = new TournamentNode <>(key , winner );
168
171
// attach the new node to the leaf node and alter the old leaf node such that it becomes an internal node
169
172
// which means the old leaf must be moved down a level and become the new leaf's sibling
170
- if (key < leaf .getKey ()) {
171
- // a leaf must always be a right child!
172
- TournamentNode <P > new_inner = new TournamentNode <>(key , winner );
173
- new_inner .setRightChild (new_node );
174
- leaf .setLeftChild (new_inner );
175
- leaf .setRightChild (new TournamentNode <>(leaf .getKey (), leaf .getWinner ()));
173
+ // that is if it's even a leaf, no guarantee
174
+ if (key <= leaf .getKey ()) {
175
+ if (leaf .isLeaf ()) {
176
+ // have to create a new inner node since a leaf cannot have any children
177
+ TournamentNode <P > new_leaf = new TournamentNode <>(leaf .getKey (), leaf .getWinner ());
178
+ leaf .setKey (key ); // have to replace its key since
179
+ leaf .setRightChild (new_leaf );
180
+ }
181
+ leaf .setLeftChild (new_node );
176
182
}
177
183
else {
178
- // here we have to swap the keys as we always let inner nodes have the key of its right leaf
179
- // it is safe to do as everything in the left subtree is less than the new key
184
+ if (leaf .isLeaf ()) {
185
+ // have to create a new inner node since a leaf cannot have any children
186
+ TournamentNode <P > new_leaf = new TournamentNode <>(leaf .getKey (), leaf .getWinner ());
187
+ leaf .setLeftChild (new_leaf );
188
+ }
180
189
leaf .setRightChild (new_node );
181
- // a leaf must always be a right child!
182
- TournamentNode <P > new_inner = new TournamentNode <>(leaf .getKey (), leaf .getWinner ());
183
- new_inner .setRightChild (new TournamentNode <>(leaf .getKey (), leaf .getWinner ()));
184
- leaf .setLeftChild (new_inner );
185
- leaf .setKey (new_node .getKey ());
186
190
}
187
191
// time to rebalance the tree
188
192
rebalance (t , leaf );
@@ -233,7 +237,7 @@ private void updateWinner(double t, TournamentNode<P> node) {
233
237
P leftWinner = node .getLeftChild ().getWinner ();
234
238
P rightWinner = node .getRightChild ().getWinner ();
235
239
236
- node .setWinner (winnerFunction .findWinner (leftWinner , rightWinner ));
240
+ node .setWinner (winnerFunction .findWinner (t , leftWinner , rightWinner ));
237
241
}
238
242
239
243
if (node .getEvent () != null ) eq .remove (node .getEvent ());
@@ -246,7 +250,7 @@ public TournamentNode<P> delete(double t, int key) {
246
250
247
251
if (node == null ) return null ;
248
252
249
- // should be safe to assume that the doubles match, otherwise I suck
253
+ // should be safe to assume that the keys match, otherwise I suck
250
254
assert node .getKey () == key ;
251
255
252
256
return delete (t , node );
@@ -318,7 +322,9 @@ private void rebalance(double t, TournamentNode<P> tmp_node) {
318
322
rotate_right (t , tmp_node .getRightChild ());
319
323
rotate_left (t , tmp_node .getLeftChild ());
320
324
}
321
- } else if (!tmp_node .isLeaf ()) {
325
+ }
326
+
327
+ if (!tmp_node .isLeaf ()) {
322
328
// even if we don't rotate, we have to update the winner certificate
323
329
updateWinner (t , tmp_node );
324
330
}
@@ -340,8 +346,8 @@ public TournamentNode<P> find(double key) {
340
346
else tmp_node = root ;
341
347
342
348
while (!tmp_node .isNull ()) {
343
- if (key < tmp_node .getKey ()) tmp_node = tmp_node .getLeftChild ();
344
- else if (key >= tmp_node .getKey ()) tmp_node = tmp_node .getRightChild ();
349
+ if (key <= tmp_node .getKey ()) tmp_node = tmp_node .getLeftChild ();
350
+ else if (key > tmp_node .getKey ()) tmp_node = tmp_node .getRightChild ();
345
351
else break ;
346
352
}
347
353
@@ -383,7 +389,7 @@ public void rotate_left(double t, TournamentNode<P> node) {
383
389
// reset its parent to avoid a weird cycle
384
390
newRoot .setParent (null );
385
391
// update root
386
- root = newRoot ;
392
+ this . root = newRoot ;
387
393
}
388
394
newRoot .setLeftChild (node );
389
395
// update b's weights and then a's
@@ -392,7 +398,7 @@ public void rotate_left(double t, TournamentNode<P> node) {
392
398
393
399
// update the winners, we don't let it percolate though as there may be more rotations
394
400
if (!node .isLeaf ()) updateWinner (t , node );
395
- // updateWinner(t, newRoot);
401
+ updateWinner (t , newRoot );
396
402
}
397
403
398
404
public void rotate_right (double t , TournamentNode <P > node ) {
@@ -437,7 +443,7 @@ public void rotate_right(double t, TournamentNode<P> node) {
437
443
// this means that 'node' is the root and since 'newRoot' is a child of 'node', we have to manually
438
444
// reset its parent to avoid a weird cycle
439
445
newRoot .setParent (null );
440
- root = newRoot ;
446
+ this . root = newRoot ;
441
447
}
442
448
newRoot .setRightChild (node );
443
449
// process a's weights and then b's
@@ -446,7 +452,7 @@ public void rotate_right(double t, TournamentNode<P> node) {
446
452
447
453
// process the winners, we don't let it percolate though as there may be more rotations
448
454
if (!node .isLeaf ()) updateWinner (t , node );
449
- // updateWinner(t, newRoot);
455
+ updateWinner (t , newRoot );
450
456
}
451
457
452
458
public TournamentNode <P > predecessor (TournamentNode <P > node ) {
0 commit comments