-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeAVL.java
More file actions
318 lines (262 loc) · 6.52 KB
/
NodeAVL.java
File metadata and controls
318 lines (262 loc) · 6.52 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
package AVLTree;
public class NodeAVL< T,K extends Comparable<K> > implements Comparable<NodeAVL<T,K>> {
private T value;
private K key;
private int balanceFactor;
private NodeAVL< T,K > right;
private NodeAVL< T,K > lefth;
private NodeAVL< T,K > father;
public NodeAVL(T value, K key, int balanceFactor, NodeAVL<T, K> right, NodeAVL<T, K> left, NodeAVL<T, K> father) {
super();
this.value = value;
this.key = key;
this.balanceFactor = balanceFactor;
this.right = right;
this.lefth = left;
this.father = father;
}
public NodeAVL() {
super();
this.value = null;
this.key = null;
this.balanceFactor = -1;
this.right = null;
this.lefth = null;
this.father = null;
}
public NodeAVL(T value, K key) {
super();
this.value = value;
this.key = key;
this.balanceFactor = -1;
this.right = null;
this.lefth = null;
this.father = null;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public int getBalanceFactor() {
return balanceFactor;
}
public void setBalanceFactor(int balanceFactor) {
this.balanceFactor = balanceFactor;
}
public NodeAVL<T, K> getRight() {
return right;
}
public void setRight(NodeAVL<T, K> right) {
this.right = right;
}
public NodeAVL<T, K> getLeft() {
return lefth;
}
public void setLeft(NodeAVL<T, K> left) {
this.lefth = left;
}
public NodeAVL<T, K> getFather() {
return father;
}
public void setFather(NodeAVL<T, K> father) {
this.father = father;
}
public NodeAVL<T,K> maximum() {
if(right == null)
return this;
else
return right.maximum();
}
public NodeAVL<T,K> minimun() {
if(lefth == null)
return this;
else
return lefth.minimun();
}
public boolean add(NodeAVL<T,K> newElement) {
if(this.getKey().compareTo(newElement.getKey()) > 0) {
if(lefth == null) {
lefth = newElement;
lefth.balanceFactor = lefth.balanceFactor();
lefth.father = this;
return true;
}
else {
lefth.add(newElement);
return false;
}
}
else
if(right == null) {
right = newElement;
right.balanceFactor = right.balanceFactor();
right.father = this;
return true;
}
else {
right.add(newElement);
return false ;
}
}
public NodeAVL<T,K> buscar(K key) throws NodeNotFoundException {
// System.out.println(value + "buscado");
// System.out.println(this.getValue() + "actual");
if( key.compareTo(this.getKey()) == 0) {
return this;
}
else if (key.compareTo(this.getKey()) < 0 ) {
if(lefth == null)
throw new NodeNotFoundException("this node hasn't been found");
else
return lefth.buscar(key);
}
else {
if(right == null)
throw new NodeNotFoundException("this node hasn't been found");
else
return right.buscar(key);
}
}
public NodeAVL<T,K> remove(K key) {
if( (this.getKey().compareTo(key) == 0 ) ) {
if(this.isSon()) {
NodeAVL<T,K> y = this.getFather();
this.setFather(null);
if(this.isLeftSon())
y.setLeft(null);
else
y.setRight(null);
return null;
}
else {
if(this.haveRightSon() && this.haveLeftSon()) {
NodeAVL<T,K> successor = this.lefth.maximum();
NodeAVL<T, K> x = successor.getFather();
successor.setRight(this.getRight());
successor.setLeft(this.getLeft());
successor.setFather(this.getFather());
this.setLeft(null);
this.setRight(null);
this.setFather(null);
return x;
}
if(this.haveLeftSon()) {
NodeAVL<T, K> x = this.getLeft();
this.getLeft().setFather(this.getFather());
this.setFather(null);
this.setLeft(null);
return x;
}
if(this.haveRightSon()) {
NodeAVL<T, K> x = this.getRight();
this.getRight().setFather(this.getFather());
this.setFather(null);
this.setRight(null);
return x;
}
}
}
else if(this.getKey().compareTo(key) > 0)
return lefth.remove(key);
else
return right.remove(key);
return null;
}
/**
* haveLeftSon() : evaluate if the actual Node have a left son
* @return true : if the Node have a left son
* false : if the Node don't have a left son
* */
public boolean haveLeftSon() {
return this.getLeft() != null;
}
/**
* haveRighSon() : evaluate if the actual Node have a right son
* @return true : if the Node have a right son
* false : if the Node don't have a right son
* */
public boolean haveRightSon() {
return this.getRight() != null;
}
/**
* isRighSon() : evaluate if the actual Node is a right son
* @return true : if the Node is a right son
* false : if the Node is not a right son
* */
public boolean isLeftSon() {
return this.getFather() != null && this.getFather().getLeft() == this;
}
/**
* isLeftSon() : evaluate if the actual Node is a left son
* @return true : if the Node is a left son
* false : if the Node is not a left son
* */
public boolean isRigthSon() {
return this.getFather() != null && this.getFather().getRight() == this;
}
/**
* isRoot() : evaluate if the actual Node is a root of the tree
* @return true : if the Node is the root
* false : if the Node is not the root
* */
public boolean isRoot() {
return this.getFather() == null;
}
/**
* isSon() : evaluate if the actual Node is a son
* @return true : if the Node is a son
* false : if the Node is not a son
* */
public boolean isSon() {
return this.getLeft() == null && this.getRight() == null;
}
public NodeAVL<T,K> getUncle() {
if(this.getFather().isLeftSon())
return this.getGrandFather().getRight();
else
return this.getGrandFather().getLeft();
}
public NodeAVL<T,K> getGrandFather() {
return this.getFather().getFather();
}
public boolean isLeaf() {
return (right == null && lefth == null)? true:false;
}
public int height() {
if(isLeaf()) {
return 1;
}else {
if(lefth == null) {
return 1 + right.height();
}else if(right == null) {
return 1 + lefth.height();
}else {
return Math.max(1 + lefth.height() ,1 + right.height());
}
}
}
public int balanceFactor() {
if(isLeaf()) {
return 0;
}else if(lefth == null) {
return right.height();
}else if( right == null){
return lefth.height()*(-1);
}else {
return right.height() - lefth.height();
}
}
@Override
public int compareTo(NodeAVL<T, K> arg0) {
// TODO Auto-generated method stub
return 0;
}
}