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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
#include <bits/stdc++.h>
using namespace std;
#define bro(x) (((x)->ftr->lc == (x)) ? ((x)->ftr->rc) : ((x)->ftr->lc))
#define islc(x) ((x) != NULL && (x)->ftr->lc == (x))
#define isrc(x) ((x) != NULL && (x)->ftr->rc == (x))
template <typename T>
class redblacktree {
protected:
struct Node;
Node* _root; ////根节点位置
Node* _hot; ////临时维护的节点
void init(T);
void connect34(Node*, Node*, Node*, Node*, Node*, Node*, Node*);
void SolveDoubleRed(Node*);
void SolveDoubleBlack(Node*);
Node* find(T, const int); ////允许重复的查找
Node* rfind(T, const int); ////不允许重复的查找
Node* findkth(int, Node*);
int find_rank(T, Node*);
public:
struct iterator;
redblacktree() : _root(NULL), _hot(NULL) {}
int get_rank(T);
iterator insert(T);
bool remove(T);
int size();
bool empty();
iterator kth(int);
iterator lower_bound(T);
iterator upper_bound(T);
};
template <typename T>
struct redblacktree<T>::Node {
T val; ////节点信息
bool RBc; ////节点颜色,若为true,则节点为Red;否则节点为Black.
Node* ftr; ////父亲
Node* lc; ////左儿子
Node* rc; ////右儿子
int s; ////域
Node(T v = T(), bool RB = true, Node* f = NULL, Node* l = NULL,
Node* r = NULL, int ss = 1)
: val(v), RBc(RB), ftr(f), lc(l), rc(r), s(ss) {}
Node* succ() { ////删除节点时用到的替代节点
Node* ptn = rc;
while (ptn->lc != NULL) {
--(ptn->s);
ptn = ptn->lc;
}
return ptn;
}
Node* left_node() { ////直接前驱
Node* ptn = this;
if (!lc) {
while (ptn->ftr && ptn->ftr->lc == ptn) ptn = ptn->ftr;
ptn = ptn->ftr;
} else {
ptn = ptn->lc;
while (ptn->rc) {
ptn = ptn->rc;
}
}
return ptn;
}
Node* right_node() { ////直接后继
Node* ptn = this;
if (!rc) {
while (ptn->ftr && ptn->ftr->rc == ptn) ptn = ptn->ftr;
ptn = ptn->ftr;
} else {
ptn = ptn->rc;
while (ptn->lc) {
ptn = ptn->lc;
}
}
return ptn;
}
void maintain() { ////维护域s
s = 1;
if (lc) s += lc->s;
if (rc) s += rc->s;
}
};
template <typename T>
struct redblacktree<T>::iterator {
private:
Node* _real__node;
public:
iterator& operator++() {
_real__node = _real__node->right_node();
return *this;
}
iterator& operator--() {
_real__node = _real__node->left_node();
return *this;
}
T operator*() { return _real__node->val; }
iterator(Node* node_nn = NULL) : _real__node(node_nn) {}
iterator(T const& val_vv) : _real__node(rfind(val_vv, 0)) {}
iterator(iterator const& iter) : _real__node(iter._real__node) {}
};
template <typename T>
typename redblacktree<T>::iterator redblacktree<T>::insert(T v) {
Node* ptn = find(v, 1);
if (_hot == NULL) {
init(v);
return iterator(_root);
}
ptn = new Node(v, true, _hot, NULL, NULL, 1);
if (_hot->val <= v)
_hot->rc = ptn;
else
_hot->lc = ptn;
SolveDoubleRed(ptn);
return iterator(ptn);
}
template <typename T>
void redblacktree<T>::init(T v) {
_root = new Node(v, false, NULL, NULL, NULL, 1);
}
template <typename T>
typename redblacktree<T>::Node* redblacktree<T>::find(T v, const int op) {
Node* ptn = _root; ////从根节点开始查找
_hot = NULL; ////维护父亲节点
while (ptn != NULL) {
_hot = ptn;
ptn->s += op;
if (ptn->val > v)
ptn = ptn->lc;
else
ptn = ptn->rc;
}
return ptn;
}
template <typename T>
typename redblacktree<T>::Node* redblacktree<T>::rfind(T v, const int op) {
Node* ptn = _root;
_hot = NULL;
while (ptn != NULL && ptn->val != v) {
_hot = ptn;
ptn->s += op;
if (ptn->val > v)
ptn = ptn->lc;
else
ptn = ptn->rc;
}
return ptn;
}
template <typename T>
void redblacktree<T>::SolveDoubleRed(Node* nn) {
while ((!(nn->ftr)) || nn->ftr->RBc) {
if (nn == _root) {
_root->RBc = false;
return;
}
Node* pftr = nn->ftr;
if (!(pftr->RBc)) return; ////No double-red
Node* uncle = bro(nn->ftr);
Node* grdftr = nn->ftr->ftr;
if (uncle != NULL && uncle->RBc) { ////RR-2
grdftr->RBc = true;
uncle->RBc = false;
pftr->RBc = false;
nn = grdftr;
} else { ////RR-1
if (islc(pftr)) {
if (islc(nn)) {
pftr->ftr = grdftr->ftr;
if (grdftr == _root)
_root = pftr;
else if (grdftr->ftr->lc == grdftr)
grdftr->ftr->lc = pftr;
else
grdftr->ftr->rc = pftr;
connect34(pftr, nn, grdftr, nn->lc, nn->rc, pftr->rc, uncle);
pftr->RBc = false;
grdftr->RBc = true;
} else {
nn->ftr = grdftr->ftr;
if (grdftr == _root)
_root = nn;
else if (grdftr->ftr->lc == grdftr)
grdftr->ftr->lc = nn;
else
grdftr->ftr->rc = nn;
connect34(nn, pftr, grdftr, pftr->lc, nn->lc, nn->rc, uncle);
nn->RBc = false;
grdftr->RBc = true;
}
} else {
if (islc(nn)) {
nn->ftr = grdftr->ftr;
if (grdftr == _root)
_root = nn;
else if (grdftr->ftr->lc == grdftr)
grdftr->ftr->lc = nn;
else
grdftr->ftr->rc = nn;
connect34(nn, grdftr, pftr, uncle, nn->lc, nn->rc, pftr->rc);
nn->RBc = false;
grdftr->RBc = true;
} else {
pftr->ftr = grdftr->ftr;
if (grdftr == _root)
_root = pftr;
else if (grdftr->ftr->lc == grdftr)
grdftr->ftr->lc = pftr;
else
grdftr->ftr->rc = pftr;
connect34(pftr, grdftr, nn, uncle, pftr->lc, nn->lc, nn->rc);
pftr->RBc = false;
grdftr->RBc = true;
}
}
return;
}
}
}
template <typename T>
void redblacktree<T>::connect34(Node* nroot, Node* nlc, Node* nrc, Node* ntree1,
Node* ntree2, Node* ntree3, Node* ntree4) {
nlc->lc = ntree1;
if (ntree1 != NULL) ntree1->ftr = nlc;
nlc->rc = ntree2;
if (ntree2 != NULL) ntree2->ftr = nlc;
nrc->lc = ntree3;
if (ntree3 != NULL) ntree3->ftr = nrc;
nrc->rc = ntree4;
if (ntree4 != NULL) ntree4->ftr = nrc;
nroot->lc = nlc;
nlc->ftr = nroot;
nroot->rc = nrc;
nrc->ftr = nroot;
nlc->maintain();
nrc->maintain();
nroot->maintain();
}
template <typename T>
typename redblacktree<T>::iterator redblacktree<T>::lower_bound(T v) {
Node* ptn = _root;
while (ptn) {
_hot = ptn;
if (ptn->val < v) {
ptn = ptn->rc;
} else {
ptn = ptn->lc;
}
}
if (_hot->val < v) {
ptn = _hot;
} else {
ptn = _hot->left_node();
}
return iterator(ptn);
}
template <typename T>
typename redblacktree<T>::iterator redblacktree<T>::upper_bound(T v) {
Node* ptn = _root;
while (ptn) {
_hot = ptn;
if (ptn->val > v) {
ptn = ptn->lc;
} else {
ptn = ptn->rc;
}
}
if (_hot->val > v) {
ptn = _hot;
} else {
ptn = _hot->right_node();
}
return iterator(ptn);
}
template <typename T>
typename redblacktree<T>::iterator redblacktree<T>::kth(int rank) {
return iterator(findkth(rank, _root));
}
template <typename T>
typename redblacktree<T>::Node* redblacktree<T>::findkth(int rank, Node* ptn) {
if (!(ptn->lc)) {
if (rank == 1) {
return ptn;
} else {
return findkth(rank - 1, ptn->rc);
}
} else {
if (ptn->lc->s == rank - 1)
return ptn;
else if (ptn->lc->s >= rank)
return findkth(rank, ptn->lc);
else
return findkth(rank - (ptn->lc->s) - 1, ptn->rc);
}
}
template <typename T>
int redblacktree<T>::get_rank(T v) {
return find_rank(v, _root);
}
template <typename T>
int redblacktree<T>::find_rank(T v, Node* ptn) {
if (!ptn)
return 1;
else if (ptn->val >= v)
return find_rank(v, ptn->lc);
else
return (1 + ((ptn->lc) ? (ptn->lc->s) : 0) + find_rank(v, ptn->rc));
}
template <typename T>
int redblacktree<T>::size() {
return _root->s;
}
template <typename T>
bool redblacktree<T>::empty() {
return !_root;
}
template <typename T>
bool redblacktree<T>::remove(T v) {
Node* ptn = rfind(v, -1);
if (!ptn) return false;
Node* node_suc;
while (ptn->lc || ptn->rc) { ////迭代寻找真后继
if (!(ptn->lc)) {
node_suc = ptn->rc;
} else if (!(ptn->rc)) {
node_suc = ptn->lc;
} else {
node_suc = ptn->succ();
}
--(ptn->s);
ptn->val = node_suc->val;
ptn = node_suc;
}
if (!(ptn->RBc)) {
--(ptn->s);
SolveDoubleBlack(ptn);
}
if (ptn == _root) {
_root = NULL;
delete ptn;
return true;
}
if (ptn->ftr->lc == ptn)
ptn->ftr->lc = NULL;
else
ptn->ftr->rc = NULL;
delete ptn;
return true;
}
template <typename T>
void redblacktree<T>::SolveDoubleBlack(Node* nn) {
while (nn != _root) {
Node* pftr = nn->ftr;
Node* bthr = bro(nn);
if (bthr->RBc) { ////BB-1
bthr->RBc = false;
pftr->RBc = true;
if (_root == pftr) _root = bthr;
if (pftr->ftr) {
if (pftr->ftr->lc == pftr)
pftr->ftr->lc = bthr;
else
pftr->ftr->rc = bthr;
}
bthr->ftr = pftr->ftr;
if (islc(nn)) {
connect34(bthr, pftr, bthr->rc, nn, bthr->lc, bthr->rc->lc,
bthr->rc->rc);
} else {
connect34(bthr, bthr->lc, pftr, bthr->lc->lc, bthr->lc->rc, bthr->rc,
nn);
}
bthr = bro(nn);
pftr = nn->ftr;
}
if (bthr->lc && bthr->lc->RBc) { ////BB-3
bool oldRBc = pftr->RBc;
pftr->RBc = false;
if (pftr->lc == nn) {
if (pftr->ftr) {
if (pftr->ftr->lc == pftr)
pftr->ftr->lc = bthr->lc;
else
pftr->ftr->rc = bthr->lc;
}
bthr->lc->ftr = pftr->ftr;
if (_root == pftr) _root = bthr->lc;
connect34(bthr->lc, pftr, bthr, pftr->lc, bthr->lc->lc, bthr->lc->rc,
bthr->rc);
} else {
bthr->lc->RBc = false;
if (pftr->ftr) {
if (pftr->ftr->lc == pftr)
pftr->ftr->lc = bthr;
else
pftr->ftr->rc = bthr;
}
bthr->ftr = pftr->ftr;
if (_root == pftr) _root = bthr;
connect34(bthr, bthr->lc, pftr, bthr->lc->lc, bthr->lc->rc, bthr->rc,
pftr->rc);
}
pftr->ftr->RBc = oldRBc;
return;
} else if (bthr->rc && bthr->rc->RBc) { ////BB-3
bool oldRBc = pftr->RBc;
pftr->RBc = false;
if (pftr->lc == nn) {
bthr->rc->RBc = false;
if (pftr->ftr) {
if (pftr->ftr->lc == pftr)
pftr->ftr->lc = bthr;
else
pftr->ftr->rc = bthr;
}
bthr->ftr = pftr->ftr;
if (_root == pftr) _root = bthr;
connect34(bthr, pftr, bthr->rc, pftr->lc, bthr->lc, bthr->rc->lc,
bthr->rc->rc);
} else {
if (pftr->ftr) {
if (pftr->ftr->lc == pftr)
pftr->ftr->lc = bthr->rc;
else
pftr->ftr->rc = bthr->rc;
}
bthr->rc->ftr = pftr->ftr;
if (_root == pftr) _root = bthr->rc;
connect34(bthr->rc, bthr, pftr, bthr->lc, bthr->rc->lc, bthr->rc->rc,
pftr->rc);
}
pftr->ftr->RBc = oldRBc;
return;
}
if (pftr->RBc) { ////BB-2R
pftr->RBc = false;
bthr->RBc = true;
return;
} else { ////BB-2B
bthr->RBc = true;
nn = pftr;
}
}
}
|