-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeBlackRed.java
More file actions
304 lines (248 loc) · 6.23 KB
/
NodeBlackRed.java
File metadata and controls
304 lines (248 loc) · 6.23 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
package BlackRedTree;
public class NodeBlackRed< T, K extends Comparable<K> > implements Comparable<NodeBlackRed<T, K>> {
private T value;
private K key;
private NodeBlackRed< T , K > right;
private NodeBlackRed< T , K > left;
private NodeBlackRed< T , K > father;
/**
* this constant represent the color black
* this is only black and red trees
* */
public final static int BLACK = 0;
/**
* this constant represent the color black
* this is only black and red trees
* */
public final static int RED = 1;
/**
* represent the color of Node
* this is only black and red trees
* */
private int color;
public NodeBlackRed(T value, K key, NodeBlackRed<T, K> right, NodeBlackRed<T, K> left, NodeBlackRed<T, K> father,
int color) {
super();
this.value = value;
this.key = key;
this.right = right;
this.left = left;
this.father = father;
this.color = color;
}
public NodeBlackRed() {
this.value = null;
this.key = null;
this.right = null;
this.left = null;
this.father = null;
this.color = -1;
}
public NodeBlackRed(T value, K key) {
this.value = value;
this.key = key;
this.right = null;
this.left = null;
this.father = null;
this.color = -1;
}
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 NodeBlackRed<T, K> getRight() {
return right;
}
public void setRight(NodeBlackRed<T, K> right) {
this.right = right;
}
public NodeBlackRed<T, K> getLeft() {
return left;
}
public void setLeft(NodeBlackRed<T, K> left) {
this.left = left;
}
public NodeBlackRed<T, K> getFather() {
return father;
}
public void setFather(NodeBlackRed<T, K> father) {
this.father = father;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public NodeBlackRed<T,K> maximum() {
if(right == null)
return this;
else
return right.maximum();
}
public NodeBlackRed<T,K> minimun() {
if(left == null)
return this;
else
return left.minimun();
}
public boolean add(NodeBlackRed<T,K> newElement) {
if(this.key.compareTo(newElement.key) > 0) {
if(left == null) {
left = newElement;
left.color = NodeBlackRed.RED;
left.father = this;
return true;
}
else {
left.add(newElement);
return false;
}
}
else
if(right == null) {
right = newElement;
right.color = NodeBlackRed.RED;
right.father = this;
return true;
}
else {
right.add(newElement);
return false ;
}
}
public NodeBlackRed<T,K> buscar(K key) throws NodeNotFoundException {
// System.out.println(value + "buscado");
// System.out.println(this.getValue() + "actual");
if( key.compareTo(this.key) == 0) {
return this;
}
else if (key.compareTo(this.getKey()) < 0 ) {
if(left == null)
throw new NodeNotFoundException("this node hasn't been found");
else
return left.buscar(key);
}
else {
if(right == null)
throw new NodeNotFoundException("this node hasn't been found");
else
return right.buscar(key);
}
}
public NodeBlackRed<T, K> remove(K key) {
if( (this.getKey().compareTo(key) == 0 ) ) {
if(this.isSon()) {
this.setFather(null);
return null;
}
else {
if(this.haveRightSon() && this.haveLeftSon()) {
NodeBlackRed<T,K> successor = this.left.maximum();
NodeBlackRed<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()) {
NodeBlackRed<T, K> x = this.getLeft();
this.getLeft().setFather(this.getFather());
this.setFather(null);
this.setLeft(null);
return x;
}
if(this.haveRightSon()) {
NodeBlackRed<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 left.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 NodeBlackRed<T,K> getUncle() {
if(this.getFather().isLeftSon())
return this.getGrandFather().getRight();
else
return this.getGrandFather().getLeft();
}
public NodeBlackRed<T,K> getGrandFather() {
return this.getFather().getFather();
}
public void rePaint() {
if( this.color == RED)
this.color = BLACK;
else
this.color = RED;
}
@Override
public int compareTo(NodeBlackRed<T, K> comparable) {
return 0;
}
}