-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
307 lines (268 loc) · 8.5 KB
/
Copy pathRedBlackTree.java
File metadata and controls
307 lines (268 loc) · 8.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
303
304
305
306
307
package com.ssm.controller.tree;
import java.util.Comparator;
/**
* 红黑树
*/
public class RedBlackTree<E> extends BalanceBinarySearchTree<E>{
//
private static final boolean RED = false;
//
private static final boolean BLACK = true;
/**
*
*/
public RedBlackTree() {
super(null);
}
/**
* 比较器
* @param comparator
*/
public RedBlackTree(Comparator<E> comparator) {
super(comparator);
}
/**
* 添加之后进行平衡
*/
@Override
void addAfterBalance(Node<E> node) {
//得到父节点
Node<E> parent = node.parent;
//添加的是根节点
if(parent == null){
blackColor(node);
return ;
}
//父节点是黑色
if(isBlackColor(parent)){
return ;
}
//叔父节点
Node<E> uncle = parent.sibling();
//祖父节点
Node<E> grand = redColor(parent.parent);
//叔父节点是红色
if(isRedColor(uncle)){
//把父节点和叔父节点染成黑色
blackColor(parent);
blackColor(uncle);
//把祖父节点当做是新添加的节点
addAfterBalance(grand);
return ;
}
//叔父节点不是红色
if(parent.isLeftChild()){ // L
if(node.isLeftChild()){ // LL
//父节点染成黑色
blackColor(parent);
}else{ // LR
//当前节点染成黑色
blackColor(node);
//父节点左旋转
rotateLeft(parent);
}
//右旋转
rotateRight(grand);
}else{ // R
if(node.isLeftChild()){ // RL
//当前节点染成黑色
blackColor(node);
//父节点右旋转
rotateRight(parent);
}else{ // RR
//父节点染成黑色
blackColor(parent);
}
//祖父节点左旋转
rotateLeft(grand);
}
}
/**
* 删除之后进行平衡
*/
@Override
void removeAfterBalance(Node<E> node) {
//删除的节点是红色就直接删除
// if(isRedColor(node)){
// return ;
// }
//删除的节点是黑色
//取代node节点是红色
if(isRedColor(node)){
//把替代节点染成黑色
blackColor(node);
return;
}
Node<E> parent = node.parent;
//删除的是根节点
if(parent == null){
return ;
}
//删除的是黑色叶子节点
//判断被删除的node是左还是右
boolean left = parent.left == null || node.isLeftChild();
Node<E> sibling = left ? parent.right : parent.left;
if(left){ //被删除的节点在左边==>>兄弟在右边
if(isRedColor(sibling)){ //兄弟节点是红色
//兄弟染成黑色
blackColor(sibling);
//父节点染成红色
redColor(parent);
//父节点进行左旋转
rotateLeft(parent);
//变换兄弟
sibling = parent.right;
}
//兄弟节点必定是黑色
//判断兄弟节点的左右子节点都是黑色
if(isBlackColor(sibling.left) && isBlackColor(sibling.right)){
//判断父节点是否是黑色
boolean parentColor = isBlackColor(parent);
//父节点向下和兄弟节点合并
blackColor(parent);
redColor(sibling);
//父节点是黑色向下合并会造成下溢,把父节点当做是被删除的节点继续递归
if(parentColor){
removeAfterBalance(parent);
}
}else{ //兄弟节点至少有一个红色子节点
//兄弟节点的左子节点是黑色==>>需要将兄弟节点进行左旋转
if(isBlackColor(sibling.right)){ //RL
//兄弟节点进行右旋转
rotateRight(sibling);
//旋转后重新赋值兄弟节点
sibling = parent.right;
}
//继承原先父节点的颜色
color(sibling , colorOf(parent));
//兄弟节点的右子节点染成黑色
blackColor(sibling.right);
//父节点染成黑色
blackColor(parent);
//统一进行左旋转 RR
rotateLeft(parent);
}
}else{ //被删除的节点在右边==>>兄弟在左边
if(isRedColor(sibling)){ //兄弟节点是红色
//兄弟染成黑色
blackColor(sibling);
//父节点染成红色
redColor(parent);
//父节点进行右旋转
rotateRight(parent);
//变换兄弟
sibling = parent.left;
}
//兄弟节点必定是黑色
//判断兄弟节点的左右子节点都是黑色
if(isBlackColor(sibling.left) && isBlackColor(sibling.right)){
//判断父节点是否是黑色
boolean parentColor = isBlackColor(parent);
//父节点向下和兄弟节点合并
blackColor(parent);
redColor(sibling);
//父节点是黑色向下合并会造成下溢,把父节点当做是被删除的节点继续递归
if(parentColor){
removeAfterBalance(parent);
}
}else{ //兄弟节点至少有一个红色子节点
//兄弟节点的左子节点是黑色==>>需要将兄弟节点进行左旋转
if(isBlackColor(sibling.left)){ //LR
//兄弟节点进行左旋转
rotateLeft(sibling);
//旋转后重新赋值兄弟节点
sibling = parent.left;
}
//继承原先父节点的颜色
color(sibling , colorOf(parent));
//兄弟节点的左子节点染成黑色
blackColor(sibling.left);
//父节点染成黑色
blackColor(parent);
//统一进行右旋转 LL
rotateRight(parent);
}
}
}
@Override
public Node<E> createNode(E element, Node<E> parent) {
return new RBNode<E>(element, parent);
}
/**
* toString
*/
@Override
public String toString() {
StringBuffer sf = new StringBuffer();
toString( (RBNode)getRootNode() , sf , "");
return sf.toString();
}
private void toString(RBNode<E> node , StringBuffer sf , String prefix){
if(node == null){
return;
}
toString( (RBNode)node.left , sf , prefix+"L---");
sf.append(prefix).append(node.toString()).append("\n");
toString( (RBNode)node.right , sf , prefix+"R---");
}
/**
* 给节点染色
*/
Node<E> color(Node<E> node , boolean color){
if(node == null){
return node;
}
//进行染色
((RBNode<E>)node).color = color;
return node;
}
/**
* 染成红色
*/
Node<E> redColor(Node<E> node){
return color(node , RED);
}
/**
* 染成黑色
*/
Node<E> blackColor(Node<E> node){
return color(node , BLACK);
}
/**
* 查看节点什么颜色
*/
boolean colorOf(Node<E> node){
return node == null ? BLACK : ((RBNode<E>)node).color;
}
/**
* 节点是否是红色
*/
boolean isRedColor(Node<E> node){
return colorOf(node) == RED;
}
/**
* 节点是否是黑色
*/
boolean isBlackColor(Node<E> node){
return colorOf(node) == BLACK;
}
/**
* RedBlackTreeNode
*/
class RBNode<E> extends Node<E>{
//default RED
boolean color = RED;
/**
* 构造方法
*/
public RBNode(E element, Node<E> parent) { super(element, parent); }
@Override
public String toString() {
String str = "";
if(color == RED){
str = "red_";
}
return str + element.toString();
}
}
}