-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAVL.java
More file actions
302 lines (280 loc) · 10.5 KB
/
Copy pathAVL.java
File metadata and controls
302 lines (280 loc) · 10.5 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
public class AVL {
private AVLNode root;
// Constructor instancing the root as null
// the first word inserted will be the root
public AVL() {
this.root = null;
}
public AVLNode getRoot() {
return this.root;
}
public void insertElement(String word, String currentFile) {
this.root = insertRecursively(this.root, word, currentFile);
}
private AVLNode insertRecursively(AVLNode node, String word, String currentFile) {
if (node == null) {
return new AVLNode(word, currentFile);
}
int res = node.getWord().compareToIgnoreCase(word);
if (res > 0) {
node.setLeft(insertRecursively(node.getLeft(), word, currentFile));
}
else if (res < 0) {
node.setRight(insertRecursively(node.getRight(), word, currentFile));
}
else {
node.increaseFrequency(currentFile); // Word already exists, increase frequency
return node;
}
// Rebalance the node
return rebalance(node);
}
// mode parameters sets if the element will be shown
// 0: just the search
// 1: search and print of the values
public AVLNode elementExists(AVLNode node, String word, int mode) {
AVLNode curr = node;
AVLNode nodeWord = null;
while (curr != null) {
int res = curr.getWord().compareToIgnoreCase(word);
if (res == 0) {
if (mode == 1) curr.print();
nodeWord = curr;
}
curr = (res > 0) ? curr.getLeft() : curr.getRight();
}
return nodeWord;
}
public AVLNode findFather(AVLNode son) {
AVLNode father = getRoot();
AVLNode returnFather = null;
// Both father and son are the root
if (father == son) {
return returnFather;
}
while (father != null) {
if (father.getLeft() == son || father.getRight() == son) {
returnFather = father;
}
father = (father.getWord().compareTo(son.getWord()) > 0) ? father.getLeft() : father.getRight();
}
return returnFather;
}
public void removeElement(String word) {
if(elementExists(getRoot(), word, 0) != null){
System.out.println("Element not in the Tree");
return;
}
this.root = removeBalanced(this.root, word);
}
private AVLNode removeBalanced(AVLNode node, String word) {
AVLNode lowestNode, highestNode, extremesFather;
int res = node.getWord().compareToIgnoreCase(word);
// Left case
if (res > 0) {
node.setLeft(removeBalanced(node.getLeft(), word));
}
// Right case
else if (res < 0) {
node.setRight(removeBalanced(node.getRight(), word));
}
// Node found
else {
// Case 0.0: Node == root
if (node == this.root) {
// No children
if (node.getLeft() == null && node.getRight() == null) {
this.root = null;
}
// Left child only
else if (node.getLeft() != null && node.getRight() == null) {
highestNode = HighestElement(node.getLeft());
extremesFather = findFather(highestNode);
if (extremesFather != this.root) {
extremesFather.setRight(null);
if (highestNode.getLeft() != null) {
extremesFather.setRight(highestNode.getLeft());
}
highestNode.setLeft(this.root.getLeft());
}
this.root.setLeft(null);
this.root = highestNode;
}
// Right child only
else if (node.getLeft() == null && node.getRight() != null) {
lowestNode = LowestElement(node.getRight());
extremesFather = findFather(lowestNode);
if (extremesFather != this.root) {
extremesFather.setLeft(null);
if (lowestNode.getRight() != null) {
extremesFather.setLeft(lowestNode.getRight());
}
lowestNode.setRight(this.root.getRight());
}
this.root.setRight(null);
this.root = lowestNode;
}
// Two children
else {
lowestNode = LowestElement(node.getRight());
extremesFather = findFather(lowestNode);
if (extremesFather == this.root) {
lowestNode.setLeft(this.root.getLeft());
}
else {
extremesFather.setLeft(null);
if (lowestNode.getRight() != null) {
extremesFather.setLeft(lowestNode.getRight());
}
lowestNode.setRight(this.root.getRight());
lowestNode.setLeft(this.root.getLeft());
this.root.setLeft(null);
}
this.root.setRight(null);
this.root = lowestNode;
}
}
// Case 1.0: Node != root
else {
AVLNode father = findFather(node);
// No children
if (node.getLeft() == null && node.getRight() == null) {
if (father.getLeft() == node) father.setLeft(null);
else father.setRight(null);
}
// Left child only
else if (node.getLeft() != null && node.getRight() == null) {
highestNode = HighestElement(node.getLeft());
extremesFather = findFather(highestNode);
if (highestNode.getLeft() != null) {
extremesFather.setRight(highestNode.getLeft());
}
if (node.getLeft() != highestNode) {
highestNode.setLeft(node.getLeft());
}
if (father.getLeft() == node) father.setLeft(highestNode);
else father.setRight(highestNode);
}
// Right child only
else if (node.getLeft() == null && node.getRight() != null) {
lowestNode = LowestElement(node.getRight());
extremesFather = findFather(lowestNode);
if (lowestNode.getRight() != null) {
extremesFather.setLeft(lowestNode.getRight());
}
if (node.getRight() != lowestNode) {
lowestNode.setRight(node.getRight());
}
if (father.getLeft() == node) father.setLeft(lowestNode);
else father.setRight(lowestNode);
}
// Two children
else {
lowestNode = LowestElement(node.getRight());
extremesFather = findFather(lowestNode);
if (extremesFather != node) {
if (lowestNode.getRight() != null) {
extremesFather.setLeft(lowestNode.getRight());
}
if (node.getRight() != lowestNode) {
lowestNode.setRight(node.getRight());
}
}
lowestNode.setLeft(node.getLeft());
if (father.getLeft() == node) father.setLeft(lowestNode);
else father.setRight(lowestNode);
}
}
}// Rebalance the node
return rebalance(node);
}
public AVLNode HighestElement(AVLNode node) {
AVLNode curr = node;
AVLNode highestNode = null;
while (curr != null) {
highestNode = curr;
curr = curr.getRight();
}
return highestNode;
}
public AVLNode LowestElement(AVLNode node) {
AVLNode curr = node;
AVLNode lowestNode = null;
while (curr != null) {
lowestNode = curr;
curr = curr.getLeft();
}
return lowestNode;
}
public int height(AVLNode node) {
if (node == null) return -1;
int hLeft = height(node.getLeft());
int hRight = height(node.getRight());
return Math.max(hLeft, hRight) + 1;
}
private int getBalance(AVLNode node) {
if (node == null) {
return 0;
}
return height(node.getLeft()) - height(node.getRight());
}
private AVLNode rotateRight(AVLNode y) {
AVLNode x = y.getLeft();
AVLNode T2 = x.getRight();
// Perform rotation
x.setRight(y);
y.setLeft(T2);
return x; // Return new root
}
private AVLNode rotateLeft(AVLNode x) {
AVLNode y = x.getRight();
AVLNode T2 = y.getLeft();
// Perform rotation
y.setLeft(x);
x.setRight(T2);
return y; // Return new root
}
private AVLNode rebalance(AVLNode node) {
int balance = getBalance(node);
// Left Case
if (balance > 1 && getBalance(node.getLeft()) >= 0) {
return rotateRight(node);
}
//Right Case
if (balance < -1 && getBalance(node.getRight()) <= 0) {
return rotateLeft(node);
}
// Left Right Case
if (balance > 1 && getBalance(node.getLeft()) < 0) {
node.setLeft(rotateLeft(node.getLeft()));
return rotateRight(node);
}
// Right Left Case
if (balance < -1 && getBalance(node.getRight()) > 0) {
node.setRight(rotateRight(node.getRight()));
return rotateLeft(node);
}
return node;
}
public void printPreOrder(AVLNode node) {
if (node != null) {
node.print();
printPreOrder(node.getLeft());
printPreOrder(node.getRight());
}
}
public void printInOrder(AVLNode node) {
if (node != null) {
printInOrder(node.getLeft());
node.print();
printInOrder(node.getRight());
}
}
public void printPostOrder(AVLNode node) {
if (node != null) {
printPostOrder(node.getLeft());
printPostOrder(node.getRight());
node.print();
}
}
}