-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLtree.java
314 lines (260 loc) · 11.3 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
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
package java.trees;
public class AVLtree {
//classe necessaria para a estrutura ligada
private static class Node {
private int value;
private int balancingFactor;
private Node parent;
private Node leftChild;
private Node rightChild;
public Node(int value) {
this.value = value;
}
}
private Node root;
public AVLtree() {
this.root = null;
}
//insere um no na arvore obedecendo a definicao de arvore avl
public boolean insert(int value) {
if (this.contains(value)) return false;
Node newNode = new Node(value);
if (this.isEmpty()) this.root = newNode;
else this.insert(newNode, this.root);
return true;
}
//como a arvore avl e um derivado de arvore binaria de busca, ela segue a msm filosofia de insercao
private void insert(Node nodeToInsert, Node nodeToCompare) {
if (nodeToInsert.value < nodeToCompare.value) {
if (nodeToCompare.leftChild != null) {
this.insert(nodeToInsert, nodeToCompare.leftChild);
} else {
nodeToCompare.leftChild = nodeToInsert;
nodeToInsert.parent = nodeToCompare;
this.checkBalance(nodeToCompare); //efetundo o balanceamento apos a insercao
}
} else if (nodeToInsert.value > nodeToCompare.value) {
if (nodeToCompare.rightChild != null) {
this.insert(nodeToInsert, nodeToCompare.rightChild);
} else {
nodeToCompare.rightChild = nodeToInsert;
nodeToInsert.parent = nodeToCompare;
this.checkBalance(nodeToCompare); //efetundo o balanceamento apos a insercao
}
}
}
//checagem de qual rotação de balanceamento usar, assim a efetuando
private void checkBalance(Node node) {
this.setBalancingFactor(node); //setando o balanceamento do no atual
if (node.balancingFactor == -2) { //caso 1: H D (p) < H E (p) então H(p) = -2
if (this.treeHeight(node.leftChild.rightChild) < this.treeHeight(node.leftChild.leftChild)) {
//caso 1.1: H D (u) < H E (u)
node = this.rotateRight(node);
} else if (this.treeHeight(node.leftChild.rightChild) > this.treeHeight(node.leftChild.leftChild)){
//caso 1.2: H D (u) > H E (u)
node = this.doubleRotationLeftRight(node);
}
} else if (node.balancingFactor == 2) { //caso 2: H D (p) > H E (p) então H(p) = 2
if (this.treeHeight(node.rightChild.rightChild) < this.treeHeight(node.rightChild.leftChild)) {
//caso 2.1: H D (u) < H E (u)
node = this.doubleRotationRightLeft(node);
} else if (this.treeHeight(node.rightChild.rightChild) > this.treeHeight(node.rightChild.leftChild)) {
//caso 2.2: H D (u) > H E (u)
node = this.rotateLeft(node);
}
}
//balanceamento do pai do no, apos todas as movimentacoes acima
if (node.parent != null) this.checkBalance(node.parent);
else this.root = node;
}
private Node rotateRight(Node nodeRoot) {
//filho a esquerda agora oculpa o lugar de seu pai
Node leftChild = nodeRoot.leftChild;
leftChild.parent = nodeRoot.parent;
//configurando o novo pai de leftChild
if (leftChild.parent != null) {
if (leftChild.parent.rightChild == nodeRoot) leftChild.parent.rightChild = leftChild;
else if (leftChild.parent.leftChild == nodeRoot) leftChild.parent.leftChild = leftChild;
}
//no, antes pai, agora oculpa lugar de seu filho a esquerda
nodeRoot.leftChild = leftChild.rightChild;
if (nodeRoot.leftChild != null) nodeRoot.leftChild.parent = nodeRoot;
//configurando o novo pai de nodeRoot
leftChild.rightChild = nodeRoot;
nodeRoot.parent = leftChild;
//setando o balanceamento dos envolvidos
this.setBalancingFactor(nodeRoot);
this.setBalancingFactor(leftChild);
return leftChild;
}
private Node rotateLeft(Node nodeRoot) {
//filho a direita agora oculpa o lugar de seu pai
Node rightChild = nodeRoot.rightChild;
rightChild.parent = nodeRoot.parent;
//configurando o novo pai de rightChild
if (rightChild.parent != null) {
if (rightChild.parent.rightChild == nodeRoot) rightChild.parent.rightChild = rightChild;
else if (rightChild.parent.leftChild == nodeRoot) rightChild.parent.leftChild = rightChild;
}
//no, antes pai, agora oculpa lugar de seu filho a direita
nodeRoot.rightChild = rightChild.leftChild;
if (nodeRoot.rightChild != null) nodeRoot.rightChild.parent = nodeRoot;
//configurando o novo pai de nodeRoot
rightChild.leftChild = nodeRoot;
nodeRoot.parent = rightChild;
//setando o balanceamento dos envolvidos
this.setBalancingFactor(nodeRoot);
this.setBalancingFactor(rightChild);
return rightChild;
}
private Node doubleRotationLeftRight(Node nodeRoot) {
//rotaciona a esquerda o filho a esquerda
nodeRoot.leftChild = this.rotateLeft(nodeRoot.leftChild);
//depois, rotaciona a direita o nodeRoot
return this.rotateRight(nodeRoot);
}
private Node doubleRotationRightLeft(Node nodeRoot) {
//rotaciona a direita o filho a direita
nodeRoot.rightChild = this.rotateRight(nodeRoot.rightChild);
//depois, rotaciona a esquerda o nodeRoot
return this.rotateLeft(nodeRoot);
}
//calculo do balanceamento de um no, feito com auxilio da altura dos filhos
private void setBalancingFactor(Node node) {
node.balancingFactor = this.treeHeight(node.rightChild) - this.treeHeight(node.leftChild);
}
//remove elemento da arvore, se o msm existir nela
public boolean remove(int value) {
if (!this.contains(value)) return false;
else return this.remove(value, this.root);
}
private boolean remove(int value, Node nodeToCompare) {
//procurando pelo no a remover
if (value < nodeToCompare.value) {
if (nodeToCompare.leftChild != null) return this.remove(value, nodeToCompare.leftChild);
else return false;
} else if (value > nodeToCompare.value) {
if (nodeToCompare.rightChild != null) return this.remove(value, nodeToCompare.rightChild);
else return false;
}
this.removeNode(nodeToCompare);
return true;
}
private void removeNode(Node nodeToRemove) {
//sucessor do no a remover
Node successorNodeLevel1;
if (nodeToRemove.leftChild == null || nodeToRemove.rightChild == null) {
if (nodeToRemove.parent == null) {
this.root = null;
return;
}
//so possui no maximo um filho, portando, o sucessor nao precisa de sucessor
successorNodeLevel1 = nodeToRemove;
} else {
successorNodeLevel1 = this.lookSuccessor(nodeToRemove);
nodeToRemove.value = successorNodeLevel1.value;
}
//sucessor do sucessor do no a remover
Node successorNodeLevel2;
//selecionando e setando successorNodeLevel2
if (successorNodeLevel1.leftChild != null) successorNodeLevel2 = successorNodeLevel1.leftChild;
else successorNodeLevel2 = successorNodeLevel1.rightChild;
//setando o parent de successorNodeLevel2
if (successorNodeLevel2 != null) successorNodeLevel2.parent = successorNodeLevel1.parent;
//setando o novo parent de successorNodeLevel2
if (successorNodeLevel1.parent == null) {
this.root = successorNodeLevel2;
} else {
if (successorNodeLevel1 == successorNodeLevel1.parent.leftChild) successorNodeLevel1.parent.leftChild = successorNodeLevel2;
else successorNodeLevel1.parent.rightChild = successorNodeLevel2;
this.checkBalance(successorNodeLevel1.parent);
}
}
//metodo que retorna um sucessor adequado para um determinado no
private Node lookSuccessor(Node predecessorNode) {
Node successorNode;
//procura pelo menor no a direita da arvore, cuja a raiz e predecessorNode
if (predecessorNode.rightChild != null) {
successorNode = predecessorNode.rightChild;
while (successorNode.leftChild != null) {
successorNode = successorNode.leftChild;
}
} else {
//procura por um no sucessor, a partir do pai de predecessorNode
successorNode = predecessorNode.parent;
while (successorNode != null && predecessorNode == successorNode.rightChild) {
predecessorNode = successorNode;
successorNode = predecessorNode.parent;
}
}
return successorNode;
}
//verifica se existe um determinado valor na arvore
public boolean contains(int value) {
return this.contains(this.root, value);
}
private boolean contains(Node node, int value) {
if (node == null) return false;
else if (value < node.value) return this.contains(node.leftChild, value); //procura na sub-arvore da esquerda
else if (value > node.value) return this.contains(node.rightChild, value); //procura na sub-arvore da direita
else return true;
}
public int numberOfNodes() {
return this.numberOfNodes(this.root);
}
//retorna a quantidade de nos da arvore
private int numberOfNodes(Node node) {
return node == null ? 0 : 1 + this.numberOfNodes(node.leftChild) + this.numberOfNodes(node.rightChild);
}
public int treeHeight() {
return this.treeHeight(this.root);
}
//retorna a altura de uma arvore/sub-arvore
private int treeHeight(Node node) {
if (node == null) return -1;
return 1 + Math.max(this.treeHeight(node.leftChild), this.treeHeight(node.rightChild));
}
//verifica se a arvore esta vazia
public boolean isEmpty() {
return this.root == null;
}
//imprime a arvore no percurso "em ordem"
public void inOrder() {
System.out.print("InOrder: ");
this.inOrder(this.root);
System.out.println();
}
private void inOrder(Node node) {
if (node != null) {
this.inOrder(node.leftChild);
System.out.print(node.value + " ");
this.inOrder(node.rightChild);
}
}
//imprime a arvore no percurso "pre ordem"
public void preOrder() {
System.out.print("PreOrder: ");
this.preOrder(this.root);
System.out.println();
}
private void preOrder(Node node) {
if (node != null) {
System.out.print(node.value + " ");
this.preOrder(node.leftChild);
this.preOrder(node.rightChild);
}
}
//imprime a arvore no percurso "pos ordem"
public void postOrder() {
System.out.print("PostOrder: ");
this.postOrder(this.root);
System.out.println();
}
private void postOrder(Node node) {
if (node != null) {
this.postOrder(node.leftChild);
this.postOrder(node.rightChild);
System.out.print(node.value + " ");
}
}
}