-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree.java
139 lines (114 loc) · 3.42 KB
/
AVLTree.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
package project_AVL;
public class AVLTree<T extends Comparable<? super T>> extends BST<T> {
protected int height;
public AVLTree() {
super();
height = -1;
}
public AVLTree(BSTNode<T> root) {
super(root);
height = -1;
}
public int getHeight() {
return getHeight(root);
}
private int getHeight(BSTNode<T> node) {
if(node == null)
return -1;
else
return 1 + Math.max(getHeight(node.left), getHeight(node.right));
}
private AVLTree<T> getLeftAVL() {
AVLTree<T> leftsubtree = new AVLTree<T>(root.left);
return leftsubtree;
}
private AVLTree<T> getRightAVL() {
AVLTree<T> rightsubtree = new AVLTree<T>(root.right);
return rightsubtree;
}
protected int getBalanceFactor() {
if(isEmpty())
return 0;
else
return getRightAVL().getHeight() - getLeftAVL().getHeight();
}
public void insertAVL(T el) {
super.insert(el);
this.balance();
}
public void deleteAVL(T el) {
deleteByCopying(el);
balance();
}
protected void balance() {
if(!isEmpty())
{
getLeftAVL().balance();
getRightAVL().balance();
adjustHeight();
int balanceFactor = getBalanceFactor();
if(balanceFactor == -2) {
// System.out.println("Balancing node with el: "+root.el);
if(getLeftAVL().getBalanceFactor() < 0)
rotateRight();
else
rotateLeftRight();
}
else if(balanceFactor == 2) {
// System.out.println("Balancing node with el: "+root.el);
if(getRightAVL().getBalanceFactor() > 0)
rotateLeft();
else
rotateRightLeft();
}
}
}
protected void adjustHeight()
{
if(isEmpty())
height = -1;
else
height = 1 + Math.max(getLeftAVL().getHeight(), getRightAVL().getHeight());
}
protected void rotateRight() {
// System.out.println("RIGHT ROTATION");
// if (isEmpty()) throw new Exception("Invalid Operation");
BSTNode<T> tempNode = root.right;
root.right = root.left;
root.left = root.right.left;
root.right.left = root.right.right;
root.right.right = tempNode;
T val = (T) root.el;
root.el = root.right.el;
root.right.el = val;
getRightAVL().adjustHeight();
adjustHeight();
}
protected void rotateLeft() {
// System.out.println("LEFT ROTATION");
BSTNode<T> tempNode = root.left;
root.left = root.right;
root.right = root.left.right;
root.left.right = root.left.left;
root.left.left = tempNode;
T val = (T) root.el;
root.el = root.left.el;
root.left.el = val;
getLeftAVL().adjustHeight();
adjustHeight();
}
protected void rotateLeftRight() {
// System.out.println("Double Rotation...");
getLeftAVL().rotateLeft();
getLeftAVL().adjustHeight();
this.rotateRight();
this.adjustHeight();
}
protected void rotateRightLeft() {
// System.out.println("Double Rotation...");
getRightAVL().rotateRight();
getRightAVL().adjustHeight();
this.rotateLeft();
this.adjustHeight();
}
}