-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree.cpp
334 lines (315 loc) · 8.53 KB
/
rbtree.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
#include<bits/stdc++.h>
using namespace std;
struct RBTreeNode {
bool black;
int val;
RBTreeNode *left,*right,*parent;
RBTreeNode(int v = 0) {
val = v;
black = false;
left = right = parent = NULL;
}
};
struct RBTree {
private:
RBTreeNode *root;
void leftRoate(RBTreeNode*node) {
RBTreeNode*tmp = node->left;//tmp可能为NULL
RBTreeNode*parent = node->parent;
RBTreeNode*grandpa = parent->parent;
node->left = parent;
parent->right = tmp;
if(tmp) tmp->parent = parent;
parent->parent = node;
node->parent = grandpa;
if(!grandpa) {
root = node;
return;
}
if(grandpa->left == parent) grandpa->left = node;
else grandpa->right = node;
}
void rightRoate(RBTreeNode*node) { //《不要随便传引用!!!》-- RBTreeNode*&node 在旋转时传入node->parent->left
RBTreeNode*tmp = node->right; //然后第六行赋值parent->left = tmp(tmp == node) 直接导致node<-NULL,段错误找了好久
RBTreeNode*parent = node->parent;
RBTreeNode*grandpa = parent->parent;
node->right= parent;
parent->left= tmp;
if(tmp) tmp->parent = parent;//tmp可能为NULL,这里段错误找了好久
parent->parent = node;
node->parent = grandpa;
if(!grandpa) { //如果改变了根要把树的根给改变了,不然会出现只剩一个根的情况*********这里出错找了好久
root = node;
return;
}
if(grandpa->left == parent) grandpa->left = node;
else grandpa->right = node;
}
void insertAdjust(RBTreeNode *root) {
if(!root->parent) { //当前插入节点为根,变为黑
root->black = true;
return;
}
RBTreeNode* parent = root->parent;
if(parent->black) { //红连黑,什么都不用做
return;
}
RBTreeNode* &grandpa = parent->parent; //父节点为红,必不为根
//assert(grandpa && grandpa->black);
if(grandpa->left && grandpa->right && !grandpa->left->black && !grandpa->right->black) {
//祖父节点为黑,其两个儿子节点为红,祖父节点变红,儿子节点变黑,往上调整
grandpa->black = false;
grandpa->left->black = grandpa->right->black = true;
insertAdjust(grandpa);
return;
}
if(grandpa->right == parent){
if(parent->left == root) {
rightRoate(root);
} else {
root = root->parent;
}
leftRoate(root);
swap(root->black,root->left->black);
return;
}
if(grandpa->left == parent) {
if(parent->right == root) {
leftRoate(root);
} else {
root = root->parent;
}
rightRoate(root);
swap(root->black,root->right->black);
return;
}
//assert(0);
}
void insert(RBTreeNode *parent,RBTreeNode *&root,int val) {
if(!root) {
root = new RBTreeNode(val);
root->parent = parent;
insertAdjust(root);
return;
}
//printf("root:%p left:%p right:%p parent:%p val:%d black:%d\n",root,root->left,root->right,root->parent,root->val,root->black);
if(val > root->val) {
insert(root,root->right,val);
}
else {
insert(root,root->left,val);
}
}
RBTreeNode* begin(RBTreeNode *root) {
while(root && root->left) root = root->left;
return root;
}
RBTreeNode* next(RBTreeNode *root) {
if(root->right) {
root = root->right;
while(root->left) root = root->left;
return root;
}
while(root->parent && root->parent->right == root) root = root->parent;
return root->parent;
}
int check(RBTreeNode *root) {
if(!root) return 0;
if(root->left) {
assert(!(!root->black && !root->left->black));
assert(root->left->parent == root);
assert(root->left->val <= root->val);
}
if(root->right) {
assert(root->right->parent == root);
assert(root->right->val >= root->val);
}
int a = check(root->left);
int b = check(root->right);
assert(a == b);
return a + root->black;
}
bool erase(RBTreeNode *&root,RBTreeNode *node) {
if(!node) return false;
if(node->left && node->right) { //两个子节点,拿左儿子的最右儿子或右儿子的最左儿子复制到要删除的节点
RBTreeNode *tmp = node->left;
while(tmp->right) tmp = tmp->right;//取左儿子的最右儿子
node->val = tmp->val; //copy
node = tmp;
}
if(!node->black) { //被删除节点为红->这个节点不可能为根,直接删除即可
RBTreeNode*child = node->left;
RBTreeNode*parent = node->parent;
if(child) child->parent = parent;
assert(parent);
if(parent->left == node) parent->left = child;
else parent->right = child;
delete node;
return true;
}
RBTreeNode *child=NULL;
if(node->left) {
child = node->left;
} else if(node->right) {
child = node->right;
}
RBTreeNode *parent = node->parent;
if(child) child->parent = parent;
if(!parent) root = child;
else if(parent->left == node) parent->left = child;
else parent->right = child;
eraseAdjust(child,parent);
delete node;
return true;
}
void eraseAdjust(RBTreeNode *node,RBTreeNode *parent) {
if(!parent) {
//1.1 子节点为根,染为黑
if(node) node->black = true;
return;
}
if(node && !node->black) {
//1.2子节点为红,直接染为黑
node->black = true;
return;
}
RBTreeNode *brother = (parent->left == node ? parent->right : parent->left);
if(!parent->black) { //2.父节点为红,兄弟节点必为黑
assert((!brother||brother->black));
if((!brother->left || brother->left->black) && (!brother->right || brother->right->black)) {
//2.1 兄弟节点两个儿子为 black,black
swap(brother->black,parent->black);
return;
}
if(parent->left == node) {
if(brother && brother->left && !brother->left->black) {
//2.2 兄弟节点两个儿子为 red,?
parent->black = true;
rightRoate(brother->left);
leftRoate(parent->right);
return;
}
//2.3 兄弟节点两个儿子为 black,red
leftRoate(brother);
return;
} else {
if(brother && brother->right && !brother->right->black) {
//2.2 兄弟节点两个儿子为 ?,red
parent->black = true;
leftRoate(brother->right);
rightRoate(parent->left);
return;
}
//2.3 兄弟节点两个儿子为 red,black
rightRoate(brother);
return;
}
}
//3.父节点为黑
if(brother && !brother->black) {
//3.1 兄弟节点为红,两个儿子节点必为黑
swap(parent->black,brother->black);
if(parent->left == node) leftRoate(brother);
else rightRoate(brother);
eraseAdjust(node,parent); //处理后node路径上还是少一个黑色节点,重新调整
return;
}
//3.2 兄弟节点为黑
assert(brother); //brother必不为nil,否则两边黑节点个数不一样
if((!brother->left||brother->left->black) && (!brother->right||brother->right->black)) {
//3.2.1 两个儿子节点为黑
brother->black = false;
eraseAdjust(parent,parent->parent);
return;
}
if(parent->left == node) {
if(brother->left && !brother->left->black) {
//3.2.2 兄弟节点左儿子为红
brother->left->black = true;
rightRoate(brother->left);
leftRoate(parent->right);
return;
}
//3.2.3 兄弟节点儿子为 black,red
brother->right->black = true;
leftRoate(brother);
return;
} else {
if(brother->right && !brother->right->black) {
//3.2.2 兄弟节点左儿子为红
brother->right->black = true;
leftRoate(brother->right);
rightRoate(parent->left);
return;
}
//3.2.3 兄弟节点儿子为 black,red
brother->left->black = true;
rightRoate(brother);
return;
}
}
RBTreeNode* find(RBTreeNode *root,int val) {
while(root && root->val != val) {
if(root->val > val) root = root->left;
else root = root->right;
}
return root;
}
public:
RBTree() { root = NULL; }
void insert(int x) {
insert(NULL,root,x);
}
void print() {
for(RBTreeNode *it = begin(root);it;) {
printf("%d",it->val);
it = next(it);
if(!it) printf("\n");
else printf(" ");
}
/*for(RBTreeNode *it = begin(root);it;it=next(it)) {
printf("self:%p parent:%p left:%p right:%p val:%d black:%d\n",it,it->parent,it->left,it->right,it->val,it->black);
}*/
}
RBTreeNode* begin() {
return begin(root);
}
RBTreeNode* next() {
return next(root);
}
int check() {
check(root);
}
RBTreeNode* find(int val) {
return find(root,val);
}
bool erase(RBTreeNode *node) {
return erase(root,node);
}
};
int main() {
int n;
while(scanf("%d",&n)==1) {
RBTree a;
vector<int>arr;
for(int i=0,x;i<n;i++) {
//scanf("%d",&x);
x = rand();
arr.push_back(x);
a.insert(x);
a.check();
}
a.print();
printf("--------------------------\n");
for(int i=0,x;i<n;i++) {
//scanf("%d",&x);
RBTreeNode *tmp = a.find(arr[i]);
printf("erase:%d\n",arr[i]);
a.erase(tmp);
//a.print();
a.check();
}
break;
}
return 0;
}