-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABTreeSet.java
625 lines (572 loc) · 13.3 KB
/
ABTreeSet.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Stack;
/**
* @author Stephanie Engelhardt
*/
public class ABTreeSet<E extends Comparable<? super E>> extends AbstractSet<E> {
final class ABTreeIterator implements Iterator<E> {
private Node current;
private Node pending;
ABTreeIterator() {
current=root;
if(current==null)
return;
while(current.left!=null)
current=current.left;
}
@Override
public boolean hasNext() {
return current!=null;
}
@Override
public E next() {
if(!hasNext())
throw new NoSuchElementException();
pending=current;
if(current.right!=null){
current=current.right;
while(current.left!=null)
current=current.left;
return pending.data;
}
else{
while(true){
if(current.parent==null){
current=null;
return pending.data;
}
if(current.parent.left==current){
current=current.parent;
return pending.data;
}
current=current.parent;
}
}
}
@Override
public void remove() {
if(pending==null)
throw new IllegalStateException();
if (pending.left!=null && pending.right!=null)
current=pending;
unlinkNode(pending);
pending=null;
}
}
final class Node implements BSTNode<E> {
private E data;
private Node left;
private Node right;
private Node parent;
private int count;
Node(E data) {
this.data=data;
left=null;
right=null;
parent=null;
count=1;
}
/**
* Returns the number of nodes in that subtree, including the node itself
*/
@Override
public int count() {
return count;
}
@Override
public E data() {
return data;
}
@Override
public BSTNode<E> left() {
return left;
}
@Override
public BSTNode<E> parent() {
return parent;
}
@Override
public BSTNode<E> right() {
return right;
}
@Override
public String toString() {
return data.toString();
}
}
private boolean selfbalance;
private int top;
private int bottom;
private int size;
private Node root;
private List<BSTNode<E>> inorder;
private List<BSTNode<E>> preorder;
/**
* Default constructor. Builds a non-self-balancing tree.
*/
public ABTreeSet() {
selfbalance=false;
size=0;
root=null;
}
/**
* If <code>isSelfBalancing</code> is <code>true</code> <br>
* builds a self-balancing tree with the default value alpha = 2/3.
* <p>
* If <code>isSelfBalancing</code> is <code>false</code> <br>
* builds a non-self-balancing tree.
*
* @param isSelfBalancing
*/
public ABTreeSet(boolean isSelfBalancing) {
selfbalance=isSelfBalancing;
if(selfbalance){
top=2;
bottom=3;
}
root=null;
size=0;
}
/**
* If <code>isSelfBalancing</code> is <code>true</code> <br>
* builds a self-balancing tree with alpha = top / bottom.
* <p>
* If <code>isSelfBalancing</code> is <code>false</code> <br>
* builds a non-self-balancing tree (top and bottom are ignored).
*
* @param isSelfBalancing
* @param top
* @param bottom
* @throws IllegalArgumentException
* if (1/2 < alpha < 1) is false
*/
public ABTreeSet(boolean isSelfBalancing, int top, int bottom) {
selfbalance=isSelfBalancing;
if(selfbalance){
this.top=top;
this.bottom=bottom;
}
root=null;
size=0;
}
/**
* {@inheritDoc}
*
* @throws NullPointerException
* if e is null.
*/
@Override
public boolean add(E e) {
if(e==null)
throw new NullPointerException();
if(root==null){
root=new Node(e);
root.count=1;
size++;
return true;
}
Node current=root;
while(true){
int comp=current.data.compareTo(e);
if(comp==0){
return false;
}
else if(comp>0){
if(current.left!=null)
current=current.left;
else{
Node node=new Node(e);
node.parent=current;
current.left=node;
size++;
updateCount(node);
Node tester=node;
Boolean unbalanced=false;
Node highestUnbalanced=null;
while(tester.parent()!=null){
tester=tester.parent;
tester.count=tester.count+1;
//Test for inequality
if(selfbalance){
if(tester.left!=null && tester.right!=null){
if(((tester.left.count()*bottom>=size*top)||(tester.right.count()*bottom>=size*top))){
unbalanced=true;
highestUnbalanced=tester;
}
}
else if (tester.left!=null && tester.right==null){
if(tester.left.count()*bottom>=size*top){
unbalanced=true;
highestUnbalanced=tester;
}
}
else if (tester.left==null && tester.right!=null){
if(tester.right.count()*bottom>=size*top){
unbalanced=true;
highestUnbalanced=tester;
}
}
}
}
if(unbalanced)
rebalance(highestUnbalanced);
return true;
}
}
else{
if(current.right!=null)
current=current.right;
else{
Node node=new Node(e);
node.parent=current;
current.right=node;
size++;
updateCount(node);
Node tester=node;
Boolean unbalanced=false;
Node highestUnbalanced=null;
while(tester.parent()!=null){
tester=tester.parent;
tester.count=tester.count+1;
if(selfbalance){
if(tester.left!=null && tester.right!=null){
if(((tester.left.count()*bottom>=size*top)||(tester.right.count()*bottom>=size*top))){
unbalanced=true;
highestUnbalanced=tester;
}
}
else if (tester.left!=null && tester.right==null){
if(tester.left.count()*bottom>=size*top){
unbalanced=true;
highestUnbalanced=tester;
}
}
else if (tester.left==null && tester.right!=null){
if(tester.right.count()*bottom>=size*top){
unbalanced=true;
highestUnbalanced=tester;
}
}
}
}
if(unbalanced)
rebalance(highestUnbalanced);
return true;
}
}
}
}
/**
* Looks for a given object within
* the tree and returns if it exists
* within the tree or not
* @return boolean of if the given object exists within the tree
*/
@Override
public boolean contains(Object o) {
E key= (E) o;
return getBSTNode(key)!=null;
}
/**
* @param e
* @return BSTNode that contains e, null if e does not exist
*/
public BSTNode<E> getBSTNode(E e) {
Node current=root;
Node compare=new Node(e);
while(current!=null){
int comp=current.data.compareTo(compare.data());
if(comp ==0)
return current;
else if (comp>0)
current=current.left;
else
current=current.right;
}
return null;
}
/**
* Returns an in-order list of all nodes in the given sub-tree.
*
* @param root
* @return an in-order list of all nodes in the given sub-tree.
*/
public List<BSTNode<E>> inorderList(BSTNode<E> root) {
inorder=new ArrayList<BSTNode<E>>();
inorderHelper(root);
return inorder;
}
/**
* Helper method that recursively sorts subtrees of the overall tree in order
* @param root of the subtree
*/
private void inorderHelper(BSTNode<E> root){
if(root==null)
return;
if(root.left()!=null)
inorderHelper(root.left());
inorder.add(root);
if(root.right()!=null)
inorderHelper(root.right());
}
/**
* Creates an iterator that goes through the
* tree in order
* @return iterator<E> that can iteratate through the tree
*/
@Override
public Iterator<E> iterator() {
return new ABTreeIterator();
}
/**
* Returns an pre-order list of all nodes in the given sub-tree.
* @param root
* @return an pre-order list of all nodes in the given sub-tree.
*/
public List<BSTNode<E>> preorderList(BSTNode<E> root) {
preorder=new ArrayList<BSTNode<E>>();
preorderHelp(root);
return preorder;
}
/**
* Helper method that recursively sorts subtrees of the
* overall tree in preorder
* @param root
*/
private void preorderHelp(BSTNode<E> root){
preorder.add(root);
if(root.left()!=null)
preorderHelp(root.left());
if(root.right()!=null)
preorderHelp(root.right());
}
/**
* Performs a re-balance operation on the subtree rooted at the given node.
* @param bstNode
*/
public void rebalance(BSTNode<E> bstNode) {
inorderList(bstNode);
boolean left=false;
if(bstNode.parent()!=null)
if(bstNode.parent().left()==bstNode)
left=true;
root=null;
rebalanceHelper(0,bstNode.count()-1);
if(bstNode.parent()!=null){
root.parent=(ABTreeSet<E>.Node) bstNode.parent();
if(left)
root.parent.left=root;
else
root.parent.right=root;
}
}
/**
* Helper method to rebalance a given subtree of the overall tree
*/
private void rebalanceHelper(int start, int end){
int middle=(start+end)/2;
add((inorder.get(middle).data()));
if(middle!=start)
rebalanceHelper(start, middle-1);
if(middle!=end)
rebalanceHelper(middle+1,end);
}
/**
* Removes a node from the tree, and if the tree is selfbalancing,
* checks to see whether any nodes along the path of the node removed
* has become unbalanced. The node highest to the root that is unbalanced
* it the node that has rebalanced call on it
* @return boolean if it was able to remove the object from the tree or not
*/
@Override
public boolean remove(Object o) {
E key= (E) o;
Node n=(ABTreeSet<E>.Node) getBSTNode(key);
if(n==null)
return false;
Node parent=n.parent;
unlinkNode(n);
Boolean unbalanced=false;
Node highestUnbalanced = null;
while(parent!=null){
if(selfbalance){
if(parent.left!=null && parent.right!=null){
if(((parent.left.count()*bottom>=size*top)||(parent.right.count()*bottom>=size*top))){
unbalanced=true;
highestUnbalanced=parent;
}
}
else if (parent.left!=null &&parent.right==null){
if(parent.left.count()*bottom>=size*top){
unbalanced=true;
highestUnbalanced=parent;
}
}
else if (parent.left==null && parent.right!=null){
if(parent.right.count()*bottom>=size*top){
unbalanced=true;
highestUnbalanced=parent;
}
}
}
parent=parent.parent;
}
if(unbalanced)
rebalance(highestUnbalanced);
return true;
}
/**
* Returns the root of the tree.
*
* @return the root of the tree.
*/
public BSTNode<E> root() {
return root;
}
/**
* Allows the user to change the functionality of the tree by
* making it self balancing
* @param isSelfBalance
*/
public void setSelfBalance(boolean isSelfBalance) {
this.selfbalance=isSelfBalance;
}
/**
* Returns the number of nodes in the tree
*/
@Override
public int size() {
return size;
}
/**
* Finds the successor of a node within the tree
* @param node
* @return successor of given node
*/
public BSTNode<E> successor(BSTNode<E> node) {
if(node.right()!=null){
Node current=(ABTreeSet<E>.Node) node.right();
while(current.left()!=null){
current=(ABTreeSet<E>.Node) current.left();
}
return current;
}
Node n=(ABTreeSet<E>.Node) node.parent();
while(n!=null && node==n.right()){
node=n;
n=(ABTreeSet<E>.Node) n.parent();
}
return n;
}
/**
* Makes a string of a given tree
* @return sting of the tree
*/
@Override
public String toString() {
StringBuilder build= new StringBuilder();
level(root,build,0);
return build.toString();
}
/**
* Helper method to recursively make a string of the tree
* @param root
* @param height
* @return
*/
private void level(Node root, StringBuilder build, int height){
for(int i=0; i<height; i++){
build.append(" ");
}
if(root==null){
build.append("null" + "\n");
return;
}
build.append(root.data.toString() + "\n");
if(root.left!=null || root.right!=null){
level(root.left, build, height+1);
level(root.right, build, height+1);
}
}
/**
* Helper method that unlinks the
* given node from the tree
* @param n node that is going to be unlinked from the tree
*/
protected void unlinkNode(Node n){
Boolean leftChild=false;
if(n.parent!=null)
if(n.parent.left()!=null)
leftChild=(n.parent.left.data.compareTo(n.data)==0);
if(n.right()==null&& n.left()==null){
if(leftChild)
n.parent.left=null;
else
n.parent.right=null;
Node parent=n.parent;
while(parent!=null){
updateCount(parent);
parent=parent.parent;
}
n.parent=null;
}
else if(n.right()==null && n.left()!=null){
if(leftChild){
n.parent.left=n.left;
}
else
n.parent.right=n.right;
n.left.parent=n.parent;
Node parent=n.parent;
while(parent!=null){
updateCount(parent);
parent=parent.parent;
}
n.parent=null;
n.left=null;
}
else if(n.right()!=null && n.left()==null){
if(leftChild)
n.parent.left=n.right;
else
n.right.parent=n.parent;
Node parent=n.parent;
while(parent!=null){
updateCount(parent);
parent=parent.parent;
}
n.right=null;
n.parent=null;
}
else{
Node s = (ABTreeSet<E>.Node) successor(n);
n.data = s.data;
unlinkNode(s);
}
}
/**
* Helper method that updates the count for
* a given node- called during the add or remove
* or rebalance method when the subtrees of nodes
* have been changed
* @param n
*/
private void updateCount(Node n){
n.count=1;
Node left=n;
Node right=n;
while(left.left()!=null){
n.count=n.count+1;
left=(ABTreeSet<E>.Node) left.left();
}
while(right.right()!=null){
n.count=n.count+1;
right=(ABTreeSet<E>.Node) right.right();
}
}
}