-
Notifications
You must be signed in to change notification settings - Fork 3
/
RBTree.h
445 lines (444 loc) · 9 KB
/
RBTree.h
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
#pragma once
#include <functional>
#include <utility>
namespace ds
{
//可能有点过度设计吧
//不需要与stl交互,所以就全部驼峰式命名
//不是给人继承的(MS实现),而是给人用的(SGI STL实现)
template <typename Traits>
class RBTree
{
private:
typedef typename Traits::KeyType KeyType;
typedef typename Traits::ValueType ValueType;
typedef typename Traits::KeyCompare KeyCompare;
const static bool B = false;
const static bool R = true;
struct Node
{
ValueType value;
int size = 0;
Node *left = nullptr;
Node *right = nullptr;
Node *p = nullptr;
bool color = B;
ValueType &operator*() { return value; }
ValueType *operator->() { return &(operator*()); }
const KeyType& key() { return Traits::KeyOf(value); }
Node() = default;
Node(Node* p, Node* l, Node* r, bool c, const ValueType &v) :value(v), size(1), left(l), right(r), p(p), color(c) { }
};
public:
typedef Node* LinkType;
private:
typedef std::pair<Node*, Node*> NodePair;
KeyCompare comp;
Node* nil_;
Node* root_;
void rotataRight(Node *x)
{
Node* y = x->left;
x->left = y->right;
if (y->right != nil_)
y->right->p = x;
y->p = x->p;
if (x->p == nil_)
root_ = y;
else
{
if (x == x->p->left)
x->p->left = y;
else x->p->right = y;
}
y->right = x;
x->p = y;
y->size = x->size;
x->size = x->left->size + x->right->size + 1;
}
void rotataLeft(Node *x)
{
Node* y = x->right;
x->right = y->left;
if (y->left != nil_)
y->left->p = x;
y->p = x->p;
if (x->p == nil_)
root_ = y;
else
{
if (x == x->p->right)
x->p->right = y;
else x->p->left = y;
}
y->left = x;
x->p = y;
y->size = x->size;
x->size = x->left->size + x->right->size + 1;
}
void putFixUp(Node *z)
{
while (z->p->color == R)
{
if (z->p == z->p->p->left)
{
Node *y = z->p->p->right;
if (y->color == R)
{
y->color = z->p->color = B;
z->p->p->color = R;
z = z->p->p;
}
else
{
if (z == z->p->right)
{
z = z->p;
rotataLeft(z);
}
z->p->color = B;
z->p->p->color = R;
rotataRight(z->p->p);
}
}
else
{
Node *y = z->p->p->left;
if (y->color == R)
{
y->color = z->p->color = B;
z->p->p->color = R;
z = z->p->p;
}
else
{
if (z == z->p->left)
{
z = z->p;
rotataRight(z);
}
z->p->color = B;
z->p->p->color = R;
rotataLeft(z->p->p);
}
}
}
root_->color = B;
}
void delFixUp(Node *x)
{
while (x != root_&&x->color == B)
{
if (x == x->p->left)
{
Node *w = x->p->right;
if (w->color == R)//case 1,will enter case 234 later
{
x->p->color = R;
w->color = B;
rotataLeft(x->p);
w = x->p->right;
}
if (w->left->color == B&&w->right->color == B)//case 2,will not enter case 34
{
w->color = R;
x = x->p;
}
else
{
if (w->right->color == B)//case 3,will enter case 4
{
w->color = R;
w->left->color = B;
rotataRight(w);
w = x->p->right;
}
//case 4,after which the "while" will finish
w->color = x->p->color;//swap the color of w and x->p(w->p)
x->p->color = B;
w->right->color = B;
rotataLeft(w->p);
x = root_;
}
}
else
{
Node *w = x->p->left;
if (w->color == R)//case 1,will enter case 234 later
{
x->p->color = R;
w->color = B;
rotataRight(x->p);
w = x->p->left;
}
if (w->right->color == B&&w->left->color == B)//case 2,will not enter case 34
{
w->color = R;
x = x->p;
}
else
{
if (w->left->color == B)//case 3,will enter case 4
{
w->color = R;
w->right->color = B;
rotataLeft(w);
w = x->p->left;
}
//case 4,after which the "while" will finish
w->color = x->p->color;//swap the color of w and x->p(w->p)
x->p->color = B;
w->left->color = B;
rotataRight(w->p);
x = root_;
}
}
}
x->color = B;
}
void transplant(Node *to, Node *from)
{
from->p = to->p;
if (to->p == nil_)
root_ = from;
else
{
if (to == to->p->left)
to->p->left = from;
else to->p->right = from;
}
}
void clear(Node *x)
{
if (!x || x == nil_)
return;
clear(x->left);
clear(x->right);
delete x;
}
void cpy(Node *dest, const Node *sour, const Node *sournil_)
{
if (sour->left != sournil_)
{
dest->left = new Node(dest, nil_, nil_, sour->left->color, ValueType(sour->left->value));
dest->left->size = sour->left->size;
cpy(dest->left, sour->left, sournil_);
}
if (sour->right != sournil_)
{
dest->right = new Node(dest, nil_, nil_, sour->right->color, ValueType(sour->right->value));
dest->right->size = sour->right->size;
cpy(dest->right, sour->right, sournil_);
}
}
Node* minChild(Node *x) const
{
while (x->left != nil_)
x = x->left;
return x;
}
Node* maxChild(Node *x) const
{
while (x->right != nil_)
x = x->right;
return x;
}
Node* insert(Node *p, const ValueType& value)
{
Node *z = new Node(p, nil_, nil_, R, value);
if (p == nil_)
root_ = z;
else
{
if (comp(p->key(), Traits::KeyOf(value)))
p->right = z;
else
p->left = z;
}
while (p != nil_)
++p->size, p = p->p;
putFixUp(z);
return z;
}
public:
Node* nextNode(Node *x) const
{
if (x->right != nil_)
return minChild(x->right);
while (x->p->right == x)
x = x->p;
return x->p;//x是最大节点时p为nil_
}
Node* prevNode(Node *x) const
{
if (x == nil_)//end()的前一个
return maxChild(root_);
if (x->left != nil_)
return maxChild(x->left);
while (x->p->left == x)
x = x->p;
return x->p;//x是最小节点时p为nil_
}
NodePair findNode(const KeyType& key)
{
Node *x = root_, *p = nil_;
while (x != nil_)
{
if (comp(x->key(), key))
p = x, x = x->right;
else if (!comp(key, x->key()))
return { x,p };
else p = x, x = x->left;
}
return { nullptr,p };
}
int lower(const KeyType &rend, bool afterEqual) const
{
Node *x = root_;
int cnt = 0;
while (x != nil_)
{
if (comp(x->key() , rend) || (afterEqual && !comp(rend , x->key())))
{
cnt += x->left->size + 1;
x = x->right;
}
else
x = x->left;
}
return cnt;
}
Node * kth(int k) //k>size() or k<1时自动返回end()
{
Node *x = root_;
while (x != nil_)
{
if (x->left->size + 1 == k)
return x;
if (x->left->size + 1 < k)
{
k -= x->left->size + 1;
x = x->right;
}
else
x = x->left;
}
return nil_;
}
Node* insert(const ValueType &value)
{
NodePair np = findNode(Traits::KeyOf(value));
if (np.first)
return np.first;
return insert(np.second, value);
}
void erase(Node *z)
{
Node *tmp = z;
while (tmp != nil_)
--tmp->size, tmp = tmp->p;
Node *y = z, *x;
bool oldColor = y->color;
if (z->left == nil_)
{
x = z->right;
transplant(z, z->right);
}
else if (z->right == nil_)
{
x = z->left;
transplant(z, z->left);
}
else
{
y = minChild(z->right);
oldColor = y->color;
x = y->right;
if (y->p == z)
x->p = y;
else
{
Node *tempy = y;
while (tempy != z)
{
--tempy->size;
tempy = tempy->p;
}
transplant(y, y->right);
y->right = z->right;
y->right->p = y;
}
transplant(z, y);
y->left = z->left, y->left->p = y;
y->color = z->color;
y->size = y->left->size + y->right->size + 1;
}
delete z;
if (oldColor == B)
delFixUp(x);
}
Node * nil() { return nil_; }
Node * minChild() const { return minChild(root_); }
Node * maxChild() const { return maxChild(root_); }
int size()const { return root_->size; }
void swap(RBTree &rhs) noexcept
{
std::swap(root_, rhs.root_);
std::swap(nil_, rhs.nil_);
}
RBTree(KeyCompare comp = KeyCompare()):comp(comp),nil_(new Node)
{
root_ = nil_;
root_->p = root_->left = root_->right = nil_;
}
RBTree(const RBTree&rhs)
{
nil_ = new Node;
if (rhs.root_ == rhs.nil_)
{
root_ = nil_;
root_->p = root_->left = root_->right = nil_;
}
else
{
root_ = new Node(nil_, nil_, nil_, rhs.root_->color, ValueType(rhs.root_->value));
root_->size = rhs.root_->size;
cpy(root_, rhs.root_, rhs.nil_);
}
}
RBTree& operator=(const RBTree &rhs)
{
if (this != &rhs)
{
RBTree tmp(rhs);
swap(tmp);
}
return *this;
}
RBTree(RBTree &&rhs) noexcept
{
if (this != &rhs)
{
nil_ = rhs.nil_;
root_ = rhs.root_;
rhs.nil_ = rhs.root_ = nullptr;
}
}
RBTree& operator=(RBTree &&rhs) noexcept
{
if (this != &rhs)
{
~RBTree();
nil_ = rhs.nil_;
root_ = rhs.root_;
rhs.nil_ = rhs.root_ = nullptr;
}
return *this;
}
~RBTree()
{
clear(root_);
delete nil_;
}
};
}