-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
646 lines (516 loc) · 16 KB
/
AVLTree.java
File metadata and controls
646 lines (516 loc) · 16 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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import java.util.*;
/**
* AVLTree class contains tree structure that is used to store words
* found in the dictionary as AVL tree structure. The tree balances itself
* when the balance factor becomes either -2 or 2. This makes sure that
* remove, add and contains methods performed on the tree has O(logN) efficiency.
* @author Adisa Narula
* @param <T>
*/
public class AVLTree <T extends Comparable <T>> implements Iterable <T> {
// instantiate attributes for BST class
protected AVLNode <T> root;
/**
* Constructor for AVL tree creates an empty tree object
*/
AVLTree() {
this.root = null;
}
/**
* Add method that can be accessed by the user in order
* to add elements to AVL tree. Add method calls recursion add so that user
* cannot access nodes and do not have to know the underlying structure
* of how data is stored in the tree.
* @param data that is inserted into the tree
*/
public void add(T data) {
// if data not null
if (data != null) {
this.root = recAdd(root, data);
}
}
/**
* Recursion add method compares data with code and recursively goes through the tree structure
* until the correct position is found where the new data gets added. The method automatically
* update height of nodes in the tree after every time a new node is added and calls balance factor
* to check whether tree has to be re balanced.
* @param node tree that new node is added to
* @param data that is inserted to the tree
* @return
*/
private AVLNode<T> recAdd(AVLNode<T> node, T data) {
if (node == null) {
// where node is being added
node = new AVLNode<T> (data);
updateHeight(node);
return node;
}
// if data is less than node data
else if (data.compareTo(node.getData()) <= 0) {
node.setLeft(recAdd(node.getLeft(), data));
}
// data greater than node
else {
node.setRight(recAdd(node.getRight(), data));
}
// update height
updateHeight(node);
int factor = balanceFactor(node);
// check balance here
if (factor == 2 || factor == -2) {
node = fixTree(node, factor);
}
//System.out.println("Balance factor: " + factor);
return node;
}
// remove method goes here
/**
* Remove method accessed by user in order to remove specified data from the tree
* This makes sure that user does not have access to node or have to understand
* underlying structure of the tree. Remove method calls recursion remove.
* @param data that is removed from the tree.
*/
public void remove(T data) {
// if data is not null
if (data != null) {
this.root = recRemove(this.root, data);
}
// reset root to reference new tree
}
/**
* Recursively searchers through the tree to find node that needs
* to be removed and calls remove method when data is found.
* @param node that data is being removed from.
* @param data that is removed from the tree structure.
* @return The new tree structure after data has been removed.
*/
private AVLNode<T> recRemove(AVLNode<T> node, T data) {
if (node == null) {
; // do nothing
}
else if (data.compareTo(node.getData()) < 0) {
// search left subtree
node.setLeft(recRemove(node.getLeft(), data));
}
else if (data.compareTo(node.getData()) > 0) {
// search right subtree
node.setRight(recRemove(node.getRight(), data));
}
else {
// reassign node
node = remove(node);
// update height
updateHeight(node);
int factor = balanceFactor(node);
// check balance here
if (factor == 2 || factor == -2) {
node = fixTree(node, factor);
}
}
// return node
return node;
}
/**
* Performs remove operation on tree after node that is going to be removed
* has been found. Method checks whether node being removed has one child
* or two children and performs remove method accordingly. Remove method automatically
* calls update height method after a node has been removed and re balances the tree
* when necessary.
* @param node that data is being removed from.
* @return node after data has been removed.
*/
private AVLNode<T> remove(AVLNode<T> node) {
// conditions for node with one child
if (node.getLeft() == null) {
return node.getRight();
}
// no right child return left child
else if (node.getRight() == null) {
return node.getLeft();
}
else {
// special case for node with two children
// get data stored in node that is going to replace removed node
T data = getPredecessor(node);
node.setData(data);
node.setLeft(recRemove(node.getLeft(), data));
// implement AVL
return node;
}
}
/**
* Retrieves the node that will replaced removed node. Gets the rightmost
* node in the left subtree of the given node.
* @param node that data is being removed from
* @return data of rightmost node in the left subtree.
*/
private T getPredecessor(AVLNode<T> node) {
if (node.getLeft() == null) {
// there should be a left child
throw new NoSuchElementException("Error: there should be a left chlid");
}
else {
// keep current node
AVLNode<T> current = node.getLeft();
// while there is still more right children
while(current.getRight() != null) {
current = current.getRight();
}
return current.getData();
}
}
/**
* Fix tree method is called when the tree needs to be balanced (when balanced factor
* is either -2 or 2). Calls specific re-balance method depending on which node
* has caused the imbalance.
* @param node tree that needs to be re-balanced.
* @param bf balanced factor that triggered rotation.
* @return tree after re-balancing has been performed
*/
private AVLNode<T> fixTree(AVLNode<T> node, int bf) {
// imbalance on the left side
if (bf == -2) {
// get left child
AVLNode<T> leftChild = node.getLeft();
// left left node caused imbalance
if (balanceFactor(leftChild) == -1) {
return balanceLL(node);
}
else if (balanceFactor(leftChild) == 1) {
return balanceLR(node);
}
}
// imbalance on the right side
else if (bf == 2) {
//get right child
AVLNode<T> rightChild = node.getRight();
// imbalance left subtree
if (balanceFactor(rightChild) == -1) {
return balanceRL(node);
}
else if(balanceFactor(rightChild) == 1) {
return balanceRR(node);
}
}
return node;
}
/**
* Balance method for left-left imbalance. Automatically
* update height of each node that is rotated.
* @param A tree that needs to be re-balanced.
* @return re-balanced tree
*/
private AVLNode<T> balanceLL (AVLNode<T> A) {
// store left child of node
AVLNode<T> B = A.getLeft();
// reset left child to right subtree of B
A.setLeft(B.getRight());
// set right subtree of B to be the original node
B.setRight(A);
// update height
updateHeight(A);
updateHeight(B);
return B;
}
/**
* Balance method for left right imbalance when balance factor
* is -2 and 1. Updates height automatically for nodes that
* undergoes a rotation.
* @param A tree that needs to be re-balanced.
* @return re-balanced tree
*/
private AVLNode<T> balanceLR (AVLNode<T> A) {
// store left subtree of A
AVLNode<T> B = A.getLeft();
// store right subtree of B
AVLNode<T> C = B.getRight();
// reset left child of node A
A.setLeft(C.getRight());
// reset right child of node B
B.setRight(C.getLeft());
// set left and right for C to get the final re-balanced tree
C.setLeft(B);
C.setRight(A);
// update height
updateHeight(A);
updateHeight(B);
updateHeight(C);
// balanced tree
return C;
}
/**
* Balance method for right-left imbalance. Automatically
* update height of each node that is rotated.
* @param A tree that needs to be re-balanced.
* @return re-balanced tree
*/
private AVLNode<T> balanceRL (AVLNode<T> A) {
// store right subtree of A
AVLNode<T> B = A.getRight();
// store left subtree of B
AVLNode<T> C = B.getLeft();
// reset right subtree of A
A.setRight(C.getLeft());
// reset left subtree of B
B.setLeft(C.getRight());
C.setRight(B);
C.setLeft(A);
// update height
updateHeight(A);
updateHeight(B);
updateHeight(C);
return C;
}
/**
* Balance method for right right imbalance. Automatically
* update height of each node that is rotated.
* @param A tree that needs to be re-balanced.
* @return re-balanced tree
*/
private AVLNode<T> balanceRR (AVLNode<T> A) {
// store right subtree of A
AVLNode<T> B = A.getRight();
// reset right subtree of A to left subtree of B
A.setRight(B.getLeft());
// reset left subtree of B
B.setLeft(A);
// update height
updateHeight(A);
updateHeight(B);
// re-balanced tree
return B;
}
/**
* Contains method accessed by user in order to check whether
* a piece of data exists in the tree structure. Calls recursive
* contains method.
* @param data that is searched for in the tree.
* @return true if data is found in the tree.
*/
public boolean contains(T data) {
// tree if found
return recContains(root, data);
}
/**
* Recursive contains method recursively searchers through the tree structure
* in order to find whether data exists in the tree. Uses compareTo method to
* check whether the method should go left or right to find the data.
* @param node tree that is being searched.
* @param data that is searched for in the tree.
* @return true if data is found.
*/
private boolean recContains (AVLNode<T> node, T data) {
// if tree is empty
if (node == null) {
return false;
}
// if data is less than node data go left
else if (data.compareTo(node.getData()) < 0) {
return recContains(node.getLeft(), data);
}
// greater than node data; go right
else if (data.compareTo(node.getData()) > 0) {
return recContains(node.getRight(), data);
}
else {
// true if found
return true;
}
}
/**
* Calculates balance factor of node to determine whether
* the tree needs to be re-balanced. If balance factor returns -2 or
* 2, the tree automatically triggers re-balancing.
* @param node that is being searched.
* @return -2 or 2 if re-balancing is necessary.
*/
private int balanceFactor(AVLNode<T> node) {
// no right element return negative height
if (node.getRight() == null) {
return - (node.getHeight());
}
// no left element return height
else if (node.getLeft() == null) {
return node.getHeight();
}
// store left and right height as variables
int rightHeight = node.getRight().getHeight();
int leftHeight = node.getLeft().getHeight();
// return balance factor
return rightHeight - leftHeight;
}
/**
* Resets height value for each node when a node
* is either added or removed from the tree. This allows
* balance factor method to determine whether a rotation needs
* to occur in order to keep the tree balanced.
* @param node tree that has had a node either removed or added
*/
private void updateHeight(AVLNode<T> node) {
// if node is a leaf
if (node.getLeft() == null && node.getRight() == null) {
node.setHeight(0);
}
// no left child
else if (node.getLeft() == null) {
// set height
node.setHeight(node.getRight().getHeight() + 1);
}
else if (node.getRight() == null) {
// set height
node.setHeight(node.getLeft().getHeight() + 1);
}
else {
// find maximum between height of subtrees
int maximum = max(node.getRight().getHeight(), node.getLeft().getHeight());
node.setHeight(maximum + 1);
}
}
/**
* Determines the maximum height between left and right subtree
* @param right height of right subtree
* @param left height of left subtree
* @return maximum integer
*/
private int max(int right, int left) {
// if right is greater return right
if (right >= left) {
return right;
}
else {
return left;
}
}
/**
* Converts the tree into string post order traversal of the tree.
* Calls post traversal print that prints the data in post
* traversal manner.
* Credits: Joanna Klukowska
* @return string representation of the tree.
*/
public String toString() {
StringBuilder s = new StringBuilder();
//inOrderPrint(root, s);
postOrderPrint(root, 0, s);
return s.toString();
}
/**
* Prints the data in the tree in a post traversal order.
* Shows the structure of the root and its subtree.
* Credits: Joanna Klukowska
* @param tree that is being printed.
* @param level (depth) of the current recursive call in the tree
* @param output string that concatenates data from the tree.
*/
private void postOrderPrint(AVLNode<T> tree, int level, StringBuilder output )
{
// if tree is not null
if (tree != null) {
String spaces = "\n";
if (level > 0) {
for (int i = 0; i < level - 1; i++)
spaces += " ";
spaces += "|--";
}
// append spaces and data to the tree
output.append(spaces);
output.append("Data: " + tree.getData());
postOrderPrint(tree.getLeft(), level + 1, output);
postOrderPrint(tree.getRight(), level + 1, output);
}
}
/**
* Iterator method allows user to traverse and print out
* elements in the AVL tree without having to know
* its underlying structure.
* @return iterator<T> object
*/
@Override
public Iterator<T> iterator() {
// return new custom tree iterator object
return (new TreeIterator());
}
/**
* Private iterator class that implements methods hasNext() and next()
* that is required in order for user to traverse through the AVl tree
* in an in order traversal manner. The inner iterator class allows it to access
* the tree and maintain object oriented principles.
* @author Adisa Narula
*/
private class TreeIterator implements Iterator<T> {
// instantiate data feel or iterator
private AVLNode<T> tree;
private ArrayList<T> inOrderArray;
private T dataReturned;
// keep at counter
int counter;
int lastElement;
/**
* Constructor sets tree to root of the AVL tree.
* It also calls inOrder method to add data to the array list
* in an in order traversal way.
*/
public TreeIterator() {
this.tree = root;
inOrderArray = new ArrayList<T>();
// add elements to array
inOrder(tree);
// set counter to zero
counter = 0;
lastElement = inOrderArray.size();
}
/**
* Allows user to check whether the tree contains another element.
* The method must be called before next method is called.
* @return true if there is another element in the tree.
*/
@Override
public boolean hasNext() {
// check size of array to see whether there are any more elements
return (counter != lastElement);
}
/**
* The next method accesses and returns the next data found in the tree.
* The method must be called after the hasNext() method every time
* it is used.
* @return the next data in the tree.
*/
@Override
public T next() {
// if no more element throw exception
if (!hasNext()) {
throw new NoSuchElementException("No more elements");
}
// keep data
dataReturned = inOrderArray.get(counter);
counter++;
// return the next data
return dataReturned;
}
/**
* Optional remove method not implemented for AVL iterator.
* @throws UnsupportedOperationException remove method is not supported in tree iterator.
*/
@Override
public void remove() {
throw new UnsupportedOperationException("remove() is not supported.");
}
// in order traversal of data in BST
/**
* Recursively performs in order traversal of data in the AVL tree.
* Adds data to array list in order.
* @param node tree that the method traverse through.
*/
private void inOrder(AVLNode<T> node) {
// if the node is not empty
if (node != null) {
// go all the way to the left
inOrder(node.getLeft());
// add to array
inOrderArray.add(node.getData());
// go right
inOrder(node.getRight());
}
}
}
}