-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb_tree.cpp
More file actions
344 lines (322 loc) · 6.63 KB
/
rb_tree.cpp
File metadata and controls
344 lines (322 loc) · 6.63 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
#include "rb_tree.h"
#include <iostream>
using namespace std;
rb_tree::rb_tree(void)
:root(NULL),nil(NULL)
{
}
rb_tree::~rb_tree(void)
{
}
bool rb_tree::insert( int value )
{
if ( root == NULL )
{
nil = new node( 0, BLACK ); //创建空结点以减少对边界结点指针为NULL的判断
root = new node( value, BLACK, nil, nil );//根结点
return true;
}
node* cur = root;
node* parent = NULL;
while ( cur != nil ) //查找适合插入的位置,比当前结点小则查找左结点,大则找右结点
{
parent = cur;
if ( cur->key > value )
{
cur = cur->left;
}
else if ( cur->key < value )
{
cur = cur->right;
}
else
{
cout << "duplicate value:" << value << endl;
return false;
}
}
cur = new node( value, RED, nil, nil, parent );
if ( parent->key > value )
{
parent->left = cur;
}
else
{
parent->right = cur;
}
insert_fixup( cur ); //插入的结点可能会导致不满足红黑树的条件,需要调整
return true;
}
//插入元素之后更新状态
void rb_tree::insert_fixup( node* c )
{
while ( c->parent != NULL && c->parent->color == RED )//插入的元素一定为红,判断其父结点是否也为红,为红则不满足条件:红结点的子结点都为黑
{
node* grandpa = c->parent->parent;
if ( grandpa->left == c->parent )//插入的是左子树
{
node* uncle = grandpa->right;
if ( uncle->color == RED )//叔父结点为红,将其父及叔父结点涂黑,祖父结点涂红
{
grandpa->color = RED;
c->parent->color = BLACK;
uncle->color = BLACK;
c = grandpa; //继续判断涂红的祖父结点是否满足红黑树要求
}
else if ( uncle->color == BLACK )//叔父结点为黑
{
if ( c == c->parent->right )//如果是右结点,则要变成左结点,应该只存在父结点的左结点为nil的情况
{
c = c->parent;
left_rotate( c );
}
c->parent->color = BLACK;
c->parent->parent->color = RED;
right_rotate( c->parent->parent );//右旋,将当前结点的父结点变成袓父结点
}
}
else
{
node* uncle = grandpa->left;
if ( uncle->color == RED )
{
grandpa->color = RED;
c->parent->color = BLACK;
uncle->color = BLACK;
c = grandpa;
}
else if ( uncle->color == BLACK )
{
if ( c == c->parent->left )
{
c = c->parent;
right_rotate( c );
}
c->parent->color = BLACK;
c->parent->parent->color = RED;
left_rotate( c->parent->parent );
}
}
}
root->color = BLACK;
}
//与左旋相反
void rb_tree::right_rotate( node* c )
{
if ( c->left != nil )
{
c->left->parent = c->parent;
if ( c->parent != NULL )
{
if ( c->parent->left == c )
{
c->parent->left = c->left;
}
else
{
c->parent->right = c->left;
}
}
else
{
root = c->left;
}
node* temp = c->left->right;
c->parent = c->left;
c->left->right = c;
c->left = temp;
if ( temp != nil )
{
temp->parent = c;
}
}
}
//当前结点的right存在,将right变成当前结点的父结点,当前结点变成right的左结点,right的左子树变成当前结点的右子树
void rb_tree::left_rotate( node* c )
{
if ( c->right != nil )
{
c->right->parent = c->parent;
if ( c->parent != NULL )
{
if ( c->parent->left == c )
{
c->parent->left = c->right;
}
else
{
c->parent->right = c->right;
}
}
else
{
root = c->right;
}
node* temp = c->right->left;
c->parent = c->right;
c->right->left = c;
c->right = temp;
if ( temp != nil )
{
temp->parent = c;
}
}
}
//查找n的中序后继
rb_tree::node* rb_tree::successor( rb_tree::node* n )
{
if ( n->right != nil )
{
node* temp = n->right;
while ( temp->left != nil )
{
temp = temp->left;
}
return temp;
}
else
{
node* temp = n->parent;
while ( temp->parent != NULL )
{
temp = temp->parent;
}
while ( temp->left != nil && temp->left != n )
{
temp = temp->left;
}
return temp;
}
}
void rb_tree::delete_node( node* del )
{
node* real_del = NULL;
if ( del->left == nil || del->right == nil )
{
real_del = del;
}
else
{
real_del = successor( del );
}
node* child = NULL;
if ( real_del->left != nil )
{
child = real_del->left;
}
else
{
child = real_del->right;
}
child->parent =real_del->parent;
if ( real_del->parent == NULL )
{
root = child;
}
else
{
if ( child->parent->left == real_del )
{
child->parent->left = child;
}
else
{
child->parent->right = child;
}
}
if ( real_del != del )
{
del->key = real_del->key;
}
if ( real_del->color == BLACK )
{
delete_fixup( child );
}
}
void rb_tree::delete_fixup( node* child )
{
node* temp = child;
while ( temp != root && temp->color == BLACK )
{
if ( temp->parent->left == temp )
{
node* brother = temp->parent->right;
if ( brother->color == RED )
{
brother->color=BLACK;
temp->parent->color=RED;
left_rotate( temp->parent );
brother = temp->parent->right;
}
if ( brother->left->color == BLACK && brother->right->color == BLACK )
{
brother->color=RED;
temp=temp->parent;
}
else if( brother->right->color == BLACK ) //case 3: brother的左子树是红色,右子树是黑色的话
{
brother->color=RED;
brother->left->color=BLACK;
right_rotate( brother );
brother=temp->parent->right;
}
brother->color=temp->parent->color; //case 4: brother的右子树是红色
temp->parent->color=BLACK;
brother->right->color=BLACK;
left_rotate( temp->parent);
temp=root;
}
else //对称情况,如果temp是其父结点的右子树
{
node *brother=temp->parent->left;
if( brother->color == RED )
{
brother->color=BLACK;
temp->parent->color=RED;
right_rotate( temp->parent);
brother=temp->parent->left;
}
if( brother->left->color == BLACK && brother->right->color == BLACK )
{
brother->color=RED;
temp=temp->parent;
}
else if( brother->left->color == BLACK )
{
brother->color=RED;
brother->right->color=BLACK;
left_rotate(brother);
brother=temp->parent->left;
}
brother->color=temp->parent->color;
temp->parent->color=BLACK;
brother->left->color=BLACK;
right_rotate(temp->parent);
temp=root;
}
}
temp->color = BLACK;
}
bool rb_tree::remove( int value )
{
if ( root == NULL )
return false;
node* temp = root;
while ( temp != nil )
{
if ( temp->key > value )
{
temp = temp->left;
}
else if ( temp->key < value )
{
temp = temp->right;
}
else
{
delete_node( temp );
return true;
}
}
return false;
}