-
Notifications
You must be signed in to change notification settings - Fork 91
/
RB_Tree.cpp
414 lines (392 loc) · 10.9 KB
/
RB_Tree.cpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "stdafx.h"
#include "RB_Tree.h"
template <class T>
RB_Tree<T>::RB_Tree(T Root_Data):Root_Node(NULL)
{
if (Root_Node == NULL)
{
Root_Node = new RB_Tree_Node<T>(Root_Data);
Root_Node->color_tag = 0;
}
}
template <class T>
RB_Tree<T>::~RB_Tree()
{
}
/************************************************************************/
/* 函数功能:向红黑树中插入一个节点 */
// 入口参数:插入的数据
// 返回值:无
/************************************************************************/
template <class T>
void RB_Tree<T>::Insert_Node(T insert_data)
{
RB_Tree_Node<T>* temp_Node = Root_Node;
while(temp_Node != NULL)
{
if (insert_data > temp_Node->data)
{
if (temp_Node->Right_child == NULL)
{
temp_Node->Right_child = new RB_Tree_Node<T>(insert_data);
temp_Node->Right_child->color_tag = 1;
temp_Node->Right_child->Father_Node = temp_Node;
if (temp_Node->color_tag == 1)
{
Fix_Tree(temp_Node->Right_child);
}
break;
}
else
{
temp_Node = temp_Node->Right_child;
}
}
else
{
if (temp_Node->Left_child == NULL)
{
temp_Node->Left_child = new RB_Tree_Node<T>(insert_data);
temp_Node->Left_child->color_tag = 1;
temp_Node->Left_child->Father_Node = temp_Node;
if (temp_Node->color_tag == 1)
{
Fix_Tree(temp_Node->Left_child);
}
break;
}
else
{
temp_Node = temp_Node->Left_child;
}
}
}
}
/************************************************************************/
/* 函数功能:从红黑树中搜寻要删除的数据节点 */
// 入口参数:删除的数据
// 返回值:1表示删除成功 -1表示删除失败
/************************************************************************/
template<class T>
int RB_Tree<T>::Delete_Node(T delete_data)
{
RB_Tree_Node<T>* temp_Node = Root_Node;
while(temp_Node->data != delete_data && temp_Node != NULL)
{
if (delete_data > temp_Node->data)
{
temp_Node = temp_Node->Right_child;
}
else
{
temp_Node = temp_Node->Left_child;
}
}
if (temp_Node)
//找到返回的数据
{
int color_tag = temp_Node->color_tag;
if (temp_Node->Left_child == NULL && temp_Node->Right_child == NULL)
//左右子树为空则直接删除
{
//
delete temp_Node;
}
else
if (temp_Node->Left_child == NULL && temp_Node->Right_child != NULL)
//左子树为空,右子树不为空
{
if (temp_Node != Root_Node)
//不为根节点
{
if (temp_Node->Father_Node->Left_child == temp_Node)
{
temp_Node->Father_Node->Left_child = temp_Node->Right_child;
temp_Node->Right_child->Father_Node = temp_Node->Father_Node;
}
else
{
temp_Node->Father_Node->Right_child = temp_Node->Right_child;
temp_Node->Right_child->Father_Node = temp_Node->Father_Node;
}
}
else
//根节点
{
Root_Node = temp_Node->Right_child;
temp_Node->Right_child->Father_Node = Root_Node;
}
}
else if (temp_Node->Left_child != NULL && temp_Node->Right_child == NULL)
//左子树不为空,右子树为空
{
if (temp_Node != Root_Node)
//不为根节点
{
if (temp_Node->Father_Node->Left_child == temp_Node)
{
temp_Node->Father_Node->Left_child = temp_Node->Left_child;
temp_Node->Left_child->Father_Node = temp_Node->Father_Node;
}
else
{
temp_Node->Father_Node->Right_child = temp_Node->Left_child;
temp_Node->Left_child->Father_Node = temp_Node->Father_Node;
}
}
else
//根节点
{
Root_Node = temp_Node->Left_child;
temp_Node->Left_child->Father_Node = Root_Node;
//delete temp_Node;
}
}
else if (temp_Node->Left_child != NULL && temp_Node->Right_child != NULL)
//左右子树都不为空
{
RB_Tree_Node<T>* Successor_Node = Find_Successor_Node(temp_Node);
if (temp_Node == temp_Node->Father_Node->Left_child)
{
temp_Node->Father_Node->Left_child = Successor_Node;
Successor_Node->Right_child = temp_Node->Right_child;
Successor_Node->Left_child = temp_Node->Left_child;
Successor_Node->Father_Node = temp_Node->Father_Node;
Successor_Node->color_tag = temp_Node->color_tag;
if (Successor_Node->Right_child)
{
Successor_Node->Father_Node->Left_child = Successor_Node->Right_child;
Successor_Node->Right_child->Father_Node = Successor_Node->Father_Node;
}
}
else
{
temp_Node->Father_Node->Right_child = Successor_Node;
Successor_Node->Right_child = temp_Node->Right_child;
Successor_Node->Left_child = temp_Node->Left_child;
Successor_Node->Father_Node = temp_Node->Father_Node;
Successor_Node->color_tag = temp_Node->color_tag;
if (Successor_Node->Right_child)
{
Successor_Node->Father_Node->Left_child = Successor_Node->Right_child;
Successor_Node->Right_child->Father_Node = Successor_Node->Father_Node;
}
}
}
if (color_tag == 0)
//如果删除的节点为黑色 则需调整红黑树
{
Fix_Tree_Delete(RB_Tree_Node<T>* temp_Node)
}
}
//未发现删除的数据 返回-1
else
{
return -1;
}
return 1;
}
/************************************************************************/
/* 函数功能:插入节点后修整红黑树,保证满足性质 */
// 入口参数:插入的当前节点
// 返回值:无
/************************************************************************/
template <class T>
void RB_Tree<T>::Fix_Tree(RB_Tree_Node<T>* current_Node)
{
RB_Tree_Node<T>* temp_current_Node = current_Node;
RB_Tree_Node<T>* uncle_Node;
while(true)
{
if ( temp_current_Node->Father_Node == NULL)
{
break;
}
if (temp_current_Node->Father_Node->color_tag != 1)
{
break;
}
RB_Tree_Node<T>* father_Node = temp_current_Node->Father_Node;
RB_Tree_Node<T>* grandfa_Node = father_Node->Father_Node;
if (grandfa_Node)
{
if (father_Node == grandfa_Node->Left_child)
{
uncle_Node = grandfa_Node->Right_child;
//如果有叔叔节点时
if (uncle_Node)
{
//情况1 叔叔为红色 将父亲节点和叔叔节点设置为黑色
//祖父节点设置为红色 将祖父节点设置为当前节点
if (uncle_Node->color_tag == 1)
{
uncle_Node->color_tag = 0;
father_Node->color_tag = 0;
grandfa_Node->color_tag = 1;
temp_current_Node = grandfa_Node;
}
//情况2:叔叔是黑色 且当前节点为右孩子 将父节点作为当前节点 对父节点进行左旋
else if (temp_current_Node == father_Node->Right_child)
{
temp_current_Node = temp_current_Node->Father_Node;
Left_Rotate(temp_current_Node);
}
//情况3:叔叔是黑色 且当前节点为左孩子 将父节点设为黑色 祖父节点设为红色 对祖父节点右旋
else
{
father_Node->color_tag = 0;
grandfa_Node->color_tag = 1;
Right_Rotate(grandfa_Node);
}
}
//没有叔叔节点时
else
{
if (temp_current_Node == father_Node->Right_child)
{
temp_current_Node = temp_current_Node->Father_Node;
Left_Rotate(temp_current_Node);
}
else
{
father_Node->color_tag = 0;
grandfa_Node->color_tag = 1;
Right_Rotate(grandfa_Node);
}
}
}
else
{
uncle_Node = grandfa_Node->Left_child;
if (uncle_Node)
{
//情况1 叔叔为红色 将父亲节点和叔叔节点设置为黑色
//祖父节点设置为红色 将祖父节点设置为当前节点
if (uncle_Node->color_tag == 1)
{
uncle_Node->color_tag = 0;
father_Node->color_tag = 0;
grandfa_Node->color_tag = 1;
temp_current_Node = grandfa_Node;
}
//情况2:叔叔是黑色 且当前节点为右孩子 将父节点作为当前节点 对父节点进行左旋
else if (temp_current_Node == father_Node->Left_child)
{
temp_current_Node = temp_current_Node->Father_Node;
Right_Rotate(temp_current_Node);
}
//情况3:叔叔是黑色 且当前节点为左孩子 将父节点设为黑色 祖父节点设为红色 对祖父节点右旋
else
{
father_Node->color_tag = 0;
grandfa_Node->color_tag = 1;
Left_Rotate(grandfa_Node);
}
}
else
{
if (temp_current_Node == father_Node->Left_child)
{
temp_current_Node = temp_current_Node->Father_Node;
Right_Rotate(temp_current_Node);
}
else
{
father_Node->color_tag = 0;
grandfa_Node->color_tag = 1;
Left_Rotate(grandfa_Node);
}
}
}
}
}
Root_Node->color_tag = 0;
}
/************************************************************************/
/* 函数功能:对当前节点进行左旋操作 */
// 入口参数:左旋的当前节点
// 返回值:无
/************************************************************************/
template <class T>
void RB_Tree<T>::Left_Rotate(RB_Tree_Node<T>* current_Node)
{
RB_Tree_Node<T>* Right_child = current_Node->Right_child;
RB_Tree_Node<T>* father_Node = current_Node->Father_Node;
current_Node->Right_child = Right_child->Left_child;
Right_child->Father_Node = father_Node;
if (father_Node == NULL)
{
Root_Node = Right_child;
}
else if(current_Node == father_Node->Left_child)
{
father_Node->Left_child = Right_child;
}
else
{
father_Node->Right_child = Right_child;
}
Right_child->Left_child = current_Node;
current_Node->Father_Node = Right_child;
}
/************************************************************************/
/* 函数功能:对当前节点进行右旋操作 */
// 入口参数:右旋的当前节点
// 返回值:无
/************************************************************************/
template <class T>
void RB_Tree<T>::Right_Rotate(RB_Tree_Node<T>* current_Node)
{
RB_Tree_Node<T>* left_Node = current_Node->Left_child;
RB_Tree_Node<T>* father_Node = current_Node->Father_Node;
current_Node->Left_child = left_Node->Right_child;
left_Node->Right_child = current_Node;
if (father_Node == NULL)
{
Root_Node = left_Node;
}
else if (current_Node = father_Node->Left_child)
{
father_Node->Left_child = left_Node;
}
else
{
father_Node->Right_child = left_Node;
}
current_Node->Father_Node = left_Node;
left_Node->Father_Node = father_Node;
}
/************************************************************************/
/* 函数功能:找寻当前节点的中序后继节点 */
// 入口参数:当前节点
// 返回值:当前节点的中序后继节点
/************************************************************************/
template<class T>
RB_Tree_Node<T>* RB_Tree<T>::Find_Successor_Node(RB_Tree_Node<T>* current_Node)
{
RB_Tree_Node<T>* temp_Node = current_Node->Right_child;
while(temp_Node->Left_child != NULL)
{
temp_Node = temp_Node->Left_child;
}
return temp_Node;
}
/************************************************************************/
/* 函数功能:清除该节点相关的所有信息 */
// 入口参数:当前节点
// 返回值:无
/************************************************************************/
template<class T>
void RB_Tree<T>::erase_Node(RB_Tree_Node<T>* Node_del)
{
if (Node_del->Father_Node)
{
Node_del->Father_Node = NULL;
}
Node_del->color_tag = NULL;
Node_del->Father_Node = NULL;
Node_del->Left_child = NULL;
Node_del->Right_child = NULL;
Node_del->data = NULL;
delete Node_del;
}