Repository navigation
Expand file tree
/
Copy pathMyRedBlackTree.java
More file actions
357 lines (314 loc) · 10.6 KB
/
Copy pathMyRedBlackTree.java
File metadata and controls
357 lines (314 loc) · 10.6 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package Tree;
public class MyRedBlackTree<T extends Comparable<T>> {
// 색상 : 코드가 읽기 쉽도록 boolean으로 표시 (true = Red, false = Black)
private static final boolean RED = true;
private static final boolean BLACK = false;
private static class Node<T> {
T data;
boolean color;
Node<T> left, right, parent;
Node(T data, boolean color) {
this.data = data;
this.color = color;
}
}
private Node<T> root;
private final Node<T> NIL; // 모든 리프가 공유하는 검정 센티넬(Null 대신 사용)
public MyRedBlackTree() {
NIL = new Node<>(null, BLACK);
NIL.left = NIL;
NIL.right = NIL;
NIL.parent = NIL;
root = NIL;
}
// 왼쪽 회전 (RR 케이스)
// @formatter:off
// x y
// / \ / \
// T1 y ──────► x T3
// / \ / \
// T2 T3 T1 T2
// @formatter:on
private void rotateLeft(Node<T> x) {
Node<T> y = x.right;
Node<T> T2 = y.left;
x.right = T2;
if (T2 != NIL) {
T2.parent = x;
}
y.parent = x.parent;
if (x.parent == NIL) {
root = y;
} else if (x == x.parent.left) {
x.parent.left = y;
} else {
x.parent.right = y;
}
y.left = x;
x.parent = y;
}
// ===== 회전 =====
// 오른쪽 회전 (LL 케이스)
// @formatter:off
// y x
// / \ / \
// x T3 ──────► T1 y
// / \ / \
// T1 T2 T2 T3
// @formatter:on
private void rotateRight(Node<T> y) {
Node<T> x = y.left;
Node<T> T2 = x.right;
y.left = T2;
if (T2 != NIL) {
T2.parent = y;
}
x.parent = y.parent;
if (y.parent == NIL) {
root = x;
} else if (y == y.parent.left) {
y.parent.left = x;
} else {
y.parent.right = x;
}
x.right = y;
y.parent = x;
}
// 삽입
public void insert(T data) {
if (data == null) {
throw new NullPointerException("NPE");
}
Node<T> newNode = new Node<>(data, RED);
newNode.left = newNode.right = NIL;
Node<T> prev = NIL;
Node<T> x = root;
while (x != NIL) {
prev = x;
int cmp = data.compareTo(x.data);
if (cmp < 0) {
x = x.left;
} else if (cmp > 0) {
x = x.right;
} else {
return;
}
}
newNode.parent = prev;
if (prev == NIL) {
root = newNode;
} else if (newNode.data.compareTo(prev.data) < 0) {
prev.left = newNode;
} else {
prev.right = newNode;
}
insertFixup(newNode);
}
private void insertFixup(Node<T> newNode) {
while (newNode.parent.color == RED) {
if (newNode.parent == newNode.parent.parent.left) {
Node<T> uncle = newNode.parent.parent.right;
if (uncle.color == RED) {
// Case 1: 삼촌 Red -> 색만 바꾸고 위로 전파
newNode.parent.color = BLACK;
uncle.color = BLACK;
newNode.parent.parent.color = RED;
newNode = newNode.parent.parent;
} else {
// Case 2: 삼촌 Black -> 회전
if (newNode == newNode.parent.right) {// LR 케이스 -> LL 케이스로
newNode = newNode.parent;
rotateLeft(newNode);
}
newNode.parent.color = BLACK;
newNode.parent.parent.color = RED;
rotateRight(newNode.parent.parent);
}
} else {
Node<T> uncle = newNode.parent.parent.left;
if (uncle.color == RED) {
newNode.parent.color = BLACK;
uncle.color = BLACK;
newNode.parent.parent.color = RED;
newNode = newNode.parent.parent;
} else {
if (newNode == newNode.parent.left) {
newNode = newNode.parent;
rotateRight(newNode);
}
newNode.parent.color = BLACK;
newNode.parent.parent.color = RED;
rotateLeft(newNode.parent.parent);
}
}
}
root.color = BLACK; // 루트 노드는 항상 BLACK
}
// 삭제
// u 자리에 v를 삽입
private void transplant(Node<T> u, Node<T> v) {
if (u.parent == NIL) {
root = v;
} else if (u == u.parent.left) {
u.parent.left = v;
} else {
u.parent.right = v;
}
v.parent = u.parent;
}
private Node<T> minNode(Node<T> node) {
while (node.left != NIL) {
node = node.left;
}
return node;
}
public void delete(T data) {
if (data == null) {
throw new NullPointerException("NPE");
}
Node<T> target = root;
while (target != NIL) {
int cmp = data.compareTo(target.data);
if (cmp < 0) {
target = target.left;
} else if (cmp > 0) {
target = target.right;
} else {
break;
}
}
if (target == NIL)
return; // 없는 키
Node<T> removed = target;
boolean removedColor = removed.color;
Node<T> fixNode; // removed 자리를 채울 노드(이중 블랙 후보)
if (target.left == NIL) {
fixNode = target.right;
transplant(target, target.right);
} else if (target.right == NIL) {
fixNode = target.left;
transplant(target, target.left);
} else {
removed = minNode(target.right);
removedColor = removed.color;
fixNode = removed.right;
if (removed.parent == target) {
fixNode.parent = removed;
} else {
transplant(removed, removed.right);
removed.right = target.right;
removed.right.parent = removed;
}
transplant(target, removed);
removed.left = target.left;
removed.left.parent = removed;
removed.color = target.color;
}
if (removedColor == BLACK) {
deleteFixup(fixNode);
}
}
private void deleteFixup(Node<T> fixNode) {
while (fixNode != root && fixNode.color == BLACK) {
if (fixNode == fixNode.parent.left) {
Node<T> sibling = fixNode.parent.right;
if (sibling.color == RED) { // case 1 : 형제 red
sibling.color = BLACK;
fixNode.parent.color = RED;
rotateLeft(fixNode.parent);
sibling = fixNode.parent.right;
}
if (sibling.left.color == BLACK && sibling.right.color == BLACK) { // 형제 : black, 조카 둘다 black
sibling.color = RED;
fixNode = fixNode.parent;
} else {
if (sibling.right.color == BLACK) { // case 3-1 왼쪽 조카가 RED
sibling.left.color = BLACK;
sibling.color = RED;
rotateRight(sibling);
sibling = fixNode.parent.right;
}
// case 3-2 오른쪽 조카가 RED
sibling.color = fixNode.parent.color;
fixNode.parent.color = BLACK;
sibling.right.color = BLACK;
rotateLeft(fixNode.parent);
fixNode = root; // 탈출용
}
} else { // fixNode = fixNode.parent.right
Node<T> sibling = fixNode.parent.left;
if (sibling.color == RED) {
sibling.color = BLACK;
fixNode.parent.color = RED;
rotateRight(fixNode.parent);
sibling = fixNode.parent.left;
}
if (sibling.right.color == BLACK && sibling.left.color == BLACK) {
sibling.color = RED;
fixNode = fixNode.parent;
} else {
if (sibling.left.color == BLACK) {
sibling.right.color = BLACK;
sibling.color = RED;
rotateLeft(sibling);
sibling = fixNode.parent.left;
}
sibling.color = fixNode.parent.color;
fixNode.parent.color = BLACK;
sibling.left.color = BLACK;
rotateRight(fixNode.parent);
fixNode = root;
}
}
}
fixNode.color = BLACK;
}
// 조회
public boolean contains(T data) {
if (data == null) {
return false;
}
Node<T> current = root;
while (current != NIL) {
int cmp = data.compareTo(current.data);
if (cmp < 0) {
current = current.left;
} else if (cmp > 0) {
current = current.right;
} else {
return true;
}
}
return false;
}
// 순회
public void inorder() {
inorder(root);
System.out.println();
}
private void inorder(Node<T> node) {
if (node == NIL) {
return;
}
inorder(node.left);
System.out.println(node.data);
inorder(node.right);
}
public boolean isBalanced() {
if (root.color != BLACK) {
return false;
}
return blackHeight(root) != -1;
}
private int blackHeight(Node<T> node) {
if (node == NIL)
return 1;
if (node.color == RED && (node.left.color == RED || node.right.color == RED))
return -1;
int left = blackHeight(node.left);
int right = blackHeight(node.right);
if (left == -1 || right == -1 || left != right)
return -1;
return left + (node.color == BLACK ? 1 : 0);
}
}