-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBT.java
More file actions
345 lines (338 loc) · 7.51 KB
/
RBT.java
File metadata and controls
345 lines (338 loc) · 7.51 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
public class RBT {
public RBTStruct Nil=new RBTStruct(1,null,null,null,-1,0);
//定义根节点
public RBTStruct root=Nil;
//构造函数,也相当于构建了一个空红黑树
public RBT()
{}
//查找 指定节点
public RBTStruct RBT_Find(int key,RBTStruct rbtroot)
{
RBTStruct result = rbtroot;
while(result != Nil && result.key != key)
{
if(result.key < key)
result = result.right_child;
else
result = result.left_child;
}
return result;
}
//确定各节点的size属性
public int RBT_SetSize(RBTStruct node)
{
if(node == Nil)
return 0;
else
{
node.size=RBT_SetSize(node.left_child)+RBT_SetSize(node.right_child)+1;
return node.size;
}
}
//OS-Key-Rank
public int OS_Key_Rank(RBTStruct Troot, int k)
{
if(Troot.key == k)
return Troot.left_child.size+1;
else
{
if(Troot.key > k)
return OS_Key_Rank(Troot.left_child,k);
else
return OS_Key_Rank(Troot.right_child,k);
}
}
//计算给定节点的黑高度
public int RBT_Get_BlackHeight(RBTStruct node)
{
int bh=0;
RBTStruct temp = node;
while(temp != Nil)
{
if(temp.color == 1)
bh++;
temp = temp.left_child;
}
return bh;
}
//插入节点
public RBTStruct Insert(RBTStruct node)
{
RBTStruct temp_x=root;
RBTStruct temp_y=Nil;
//找到节点应插入的根节点
while(temp_x != Nil)
{
temp_y=temp_x;
if(node.key<temp_y.key)
temp_x=temp_x.left_child;
else
temp_x=temp_x.right_child;
}
//将插入节点的父节点赋值为temp_y
node.parent=temp_y;
//判断key值大小以决定node应该为temp_y的左子树还是右子树
if(temp_y == Nil)
{
root = node;
}
else
{
if(node.key<temp_y.key)
temp_y.left_child=node;
else
temp_y.right_child=node;
}
//设定node的各个属性值
node.left_child=Nil;
node.right_child=Nil;
node.color=0;
//若破坏了红黑树的性质,则进行修复
RBT_Insert_FixUp(node);
return root;
}
//删除节点
public RBTStruct RBT_Delete(RBTStruct node)
{
RBTStruct temp_x = null;
RBTStruct temp_y = null;
if(node.left_child == Nil|| node.right_child == Nil)
{
temp_y=node;
}
else
temp_y = Get_RBT_Successor(node);
if(temp_y.left_child != Nil)
temp_x = temp_y.left_child;
else
temp_x = temp_y.right_child;
temp_x.parent = temp_y.parent;
if(temp_y.parent == Nil)
root = temp_x;
else
{
if(temp_y == temp_y.parent.left_child)
temp_y.parent.left_child = temp_x;
else
temp_y.parent.right_child = temp_x;
}
if(temp_y != node)
{
node.key = temp_y.key;
}
if(temp_y.color ==1)
RBT_Delete_FixUp(temp_x);
return temp_y;
}
//获取根的子树中最小元素的节点
public RBTStruct Get_RBT_Min(RBTStruct node)
{
RBTStruct min=node;
while(min.left_child != Nil)
min = min.left_child;
return min;
}
//返回红黑树中的指定节点的后继
public RBTStruct Get_RBT_Successor(RBTStruct node)
{
if(node.right_child != Nil)
return Get_RBT_Min(node.right_child);
RBTStruct temp_y=node.parent;
while(temp_y != Nil && node == temp_y.right_child)
{
node = temp_y;
temp_y = temp_y.parent;
}
return temp_y;
}
//左旋
public void Left_Rotate(RBTStruct node)
{
RBTStruct temp_right=node.right_child;
//将node的右孩子指定为temp_right的左孩子
node.right_child=temp_right.left_child;
//判断temp_right的左孩子是否为空,不为空则可以将其父节点赋值为node
if(temp_right.left_child!=Nil)
temp_right.left_child.parent=node;
//将temp_right的父节点改为node的父节点
temp_right.parent=node.parent;
//判断node的父节点是否为空,为空则表示node为根节点
if(node.parent == Nil)
root=temp_right;
else
{
if(node == node.parent.left_child)
node.parent.left_child=temp_right;
else
node.parent.right_child=temp_right;
}
//将node作为temp_right的左孩子
temp_right.left_child=node;
node.parent=temp_right;
}
//右旋
public void Right_Rotate(RBTStruct node)
{
RBTStruct temp_left=node.left_child;
node.left_child=temp_left.right_child;
if(temp_left.right_child != Nil)
temp_left.right_child.parent=node;
temp_left.parent=node.parent;
if(node.parent == Nil)
root=temp_left;
else
{
if(node == node.parent.left_child)
node.parent.left_child = temp_left;
else
node.parent.right_child = temp_left;
}
temp_left.right_child = node;
node.parent = temp_left;
}
//插入修复
public void RBT_Insert_FixUp(RBTStruct node)
{
RBTStruct temp_y=null;
while(node.parent.color == 0)
{
//处理左子树
if(node.parent == node.parent.parent.left_child)
{
temp_y=node.parent.parent.right_child;
if(temp_y.color == 0)
{
node.parent.color =1;
temp_y.color = 1;
node.parent.parent.color = 0;
node = node.parent.parent;
}
else
{
if(node == node.parent.right_child)
{
node = node.parent;
Left_Rotate(node);
}
node.parent.color = 1;
node.parent.parent.color = 0;
Right_Rotate(node.parent.parent);
}
}
//处理右子树
else
{
temp_y = node.parent.parent.left_child;
if(temp_y.color == 0)
{
node.parent.color = 1;
temp_y.color = 1;
node.parent.parent.color = 0;
node = node.parent.parent;
}
else
{
if(node == node.parent.left_child)
{
node = node.parent;
Right_Rotate(node);
}
node.parent.color = 1;
node.parent.parent.color = 0;
Left_Rotate(node.parent.parent);
}
}
}
root.color = 1;
}
//删除修复
public void RBT_Delete_FixUp(RBTStruct node)
{
RBTStruct temp_w = null;
while(node != root && node.color == 1)
{
if(node == node.parent.left_child)
{
temp_w = node.parent.right_child;
if(temp_w.color == 0)
{
temp_w.color = 1;
node.parent.color = 0;
Left_Rotate(node.parent);
temp_w = node.parent.right_child;
}
if(temp_w.left_child.color == 1 && temp_w.right_child.color == 1)
{
temp_w.color = 0;
node = node.parent;
}
else
{
if(temp_w.right_child.color == 1)
{
temp_w.left_child.color = 1;
temp_w.color = 0;
Right_Rotate(temp_w);
temp_w = node.parent.right_child;
}
temp_w.color = node.parent.color;
node.parent.color = 1;
temp_w.right_child.color = 1;
Left_Rotate(node.parent);
node = root;
}
}
else
{
temp_w = node.parent.left_child;
if(temp_w.color == 0)
{
temp_w.color = 1;
node.parent.color = 0;
Right_Rotate(node.parent);
temp_w = node.parent.left_child;
}
if(temp_w.right_child.color == 1 && temp_w.left_child.color == 1)
{
temp_w.color = 0;
node = node.parent;
}
else
{
if(temp_w.left_child.color == 1)
{
temp_w.right_child.color = 1;
temp_w.color = 0;
Left_Rotate(temp_w);
temp_w = node.parent.left_child;
}
temp_w.color = node.parent.color;
node.parent.color = 1;
temp_w.left_child.color = 1;
Right_Rotate(node.parent);
node = root;
}
}
}
node.color =1;
}
//打印红黑树
public void RBT_Print(RBTStruct node)
{
char temp_color;
if(node == Nil)
System.out.println("Nil");
else
{
if(node.color == 0)
temp_color='R';
else
temp_color='B';
System.out.println("("+node.key+temp_color+",");
RBT_Print(node.left_child);
System.out.println(",");
RBT_Print(node.right_child);
System.out.println(")");
}
}
}