-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb_tree.h
405 lines (369 loc) · 9.08 KB
/
rb_tree.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
#ifndef _ZJQ_RB_TREE_H_
#define _ZJQ_RE_TREE_H_
#include <iostream>
using namespace std;
namespace rbt{
template<typename Key , typename Val>
class map{
private:
enum color_type { red , black };
struct rbnode{
rbnode() : left(nullptr) , right(nullptr) , parent(nullptr) {}
color_type color;
rbnode* left;
rbnode* right;
rbnode* parent;
Val data;
Key key;
};
rbnode* root;
size_t length;
public:
rbnode* nullnode;
using p_rbnode = rbnode*;
using size_type = size_t;
map() : nullnode(new rbnode()) , length(0) {
root = nullnode;
nullnode->left = root;
nullnode->right = root;
nullnode->parent = root;
nullnode->color = black;
}
~map() { clear(); delete nullnode; }
// class iterator{
// private:
// p_rbnode no;
// public:
// bool operator !=(const iterator& it) { return no != it.no; }
// bool operator ==(const iterator& it) { return no == it.no; }
// void plus(){
// if (no->right != nullnode){
// no = no->right;
// while(no->left != nullnode){
// no = no->left;
// }
// }
// else{
// p_rbnode y = no->parent;
// while(no == y->right && y->parent != nullnode){
// no = y;
// y = y->parent;
// }
// if (y->parent != nullnode){
// no = y;
// }
// }
// }
// p_rbnode operator ++() { plus(); return no; }
// iterator(p_rbnode p = nullptr) : no(p) {}
// Val& operator *() { return no->data; }
// };
Val operator [] (Key k_) { p_rbnode p = find(k_); return p->data; }
// iterator begin(){
// p_rbnode temp = root;
// while(temp->left != nullnode){
// temp = temp->left;
// }
// return temp;
// }
// iterator end(){
// p_rbnode temp = root;
// while(temp->right != nullnode){
// temp = temp->right;
// }
// return temp->right;
// }
p_rbnode find(Key k_);
void insert(const Key& k_ , const Val& v_);
void insert_fixup(p_rbnode node);
void erase(Key k_);
void erase_fixup(p_rbnode node);
void rotate_left(p_rbnode node);
void rotate_right(p_rbnode node);
p_rbnode findorder(p_rbnode node);
bool empty() {return length == 0; }
size_type size() { return length; }
void print();
void print(p_rbnode node);
void clear();
};
template<typename Key , typename Val>
inline typename map<Key , Val>::p_rbnode map<Key , Val>::find(Key k_){
p_rbnode index = root;
while(index != nullnode){
if (k_ < index->key){
index = index->left;
}
else if (k_ > index->key){
index = index->right;
}
else{
break;
}
}
return index;
}
template<typename Key , typename Val>
void map<Key , Val>::insert(const Key& k_ , const Val& v_){
p_rbnode p = nullnode;
p_rbnode index = root;
while(index != nullnode){
p = index;
if (k_ < index->key){
index = index->left;
}
else if (k_ > index->key){
index = index->right;
}
else{
return;
}
}
p_rbnode node = new rbnode();
node->key = k_;
node->data = v_;
node->color = red;
node->left = nullnode;
node->right = nullnode;
if (p == nullnode){
root = node;
root->parent = nullnode;
nullnode->left = root;
nullnode->right = root;
nullnode->parent = root;
}
else{
if (k_ < p->key){
p->left = node;
}
else{
p->right = node;
}
node->parent = p;
}
insert_fixup(node);
++length;
}
template<typename Key , typename Val>
void map<Key , Val>::insert_fixup(p_rbnode node){
while(node->parent->color == red){
if (node->parent == node->parent->parent->left){
p_rbnode uncle = node->parent->parent->right;
if (uncle->color == red){
node->parent->color = black;
uncle->color = black;
node->parent->parent->color = red;
node = node->parent->parent;
}
else{
if (node == node->parent->right){
node = node->parent;
rotate_left(node);
}
node->parent->color = black;
node->parent->parent->color = red;
rotate_right(node->parent->parent);
}
}
else{
p_rbnode uncle = node->parent->parent->left;
if (uncle->color == red){
node->parent->color = black;
uncle->color = black;
node->parent->parent->color = red;
node = node->parent->parent;
}
else{
if (node == node->parent->left){
node = node->parent;
rotate_right(node);
}
node->parent->color = black;
node->parent->parent->color = red;
rotate_left(node->parent->parent);
}
}
}
root->color = black;
}
template<typename Key , typename Val>
void map<Key , Val>::erase(Key k_){
p_rbnode node = find(k_);
if (node == nullnode){
return;
}
if (node->left != nullnode && node->right != nullnode){
p_rbnode order = findorder(node);
node->data = order->data;
node->key = order->key;
node = order;
}
p_rbnode node_child;
if (node->left != nullnode){
node_child = node->left;
}
else if (node->right != nullnode){
node_child = node->right;
}
else{
node_child = nullnode;
}
node_child->parent = node->parent;
if (node->parent == nullnode){
root = node_child;
nullnode->left = root;
nullnode->right = root;
nullnode->parent = root;
}
else if (node == node->parent->left){
node->parent->left = node_child;
}
else{
node->parent->right = node_child;
}
if (node->color == black && length > 1){
erase_fixup(node_child);
}
delete node;
--length;
}
template<typename Key , typename Val>
void map<Key , Val>::erase_fixup(p_rbnode node){
while(node != root && node->color == black){
if (node == node->parent->left){
p_rbnode bro = node->parent->right;
if (bro->color == red){
color_type temp = bro->color;
bro->color = node->parent->color;
node->parent->color = temp;
rotate_left(node->parent);
}
else{
if (bro->left->color == black && bro->right->color == black){
bro->color = red;
node = node->parent;
}
else if (bro->left->color == red){
bro->color = red;
bro->left->color = black;
rotate_right(bro);
}
bro->color = node->parent->color;
node->parent->color = black;
bro->right->color = black;
rotate_left(node->parent);
}
}
else{
p_rbnode bro = node->parent->left;
if (bro->color == red){
color_type temp = bro->color;
bro->color = node->parent->color;
node->parent->color = temp;
rotate_right(node->parent);
}
else{
if (bro->left->color == black && bro->right->color == black){
bro->color = red;
node = node->parent;
}
else if (bro->right->color == red){
bro->color = red;
bro->right->color = black;
rotate_left(bro);
}
bro->color = node->parent->color;
node->parent->color = black;
bro->left->color = black;
rotate_right(node->parent);
}
}
}
root->color = black;
}
template<typename Key , typename Val>
inline void map<Key , Val>::rotate_left(p_rbnode node){
if (node == nullnode || node->right == nullnode){
return;
}
p_rbnode r = node->right;
r->parent = node->parent;
node->right = r->left;
if (r->left != nullnode){
r->left->parent = node;
}
if (node->parent == nullnode){
root = r;
nullnode->left = root;
nullnode->right = root;
}
else{
if (node == node->parent->left){
node->parent->left = r;
}
else{
node->parent->right = r;
}
}
node->parent = r;
r->left = node;
}
template<typename Key , typename Val>
inline void map<Key , Val>::rotate_right(p_rbnode node){
if (node == nullnode || node->left == nullnode){
return;
}
p_rbnode l = node->left;
l->parent = node->parent;
node->left = l->right;
if (l->right != nullnode){
l->right->parent = node;
}
if (node->parent == nullnode){
root = l;
nullnode->left = root;
nullnode->right = root;
}
else{
if (node == node->parent->right){
node->parent->right = l;
}
else{
node->parent->left = l;
}
}
node->parent = l;
l->right = node;
}
template<typename Key , typename Val>
inline typename map<Key , Val>::p_rbnode map<Key , Val>::findorder(p_rbnode node){
if (node == nullnode){
return nullnode;
}
p_rbnode result = node->right;
while(result->left != nullnode){
result = result->left;
}
return result;
}
template<typename Key , typename Val>
inline void map<Key , Val>::clear(){
while(root != nullnode){
erase(root->key);
}
}
template<typename Key , typename Val>
inline void map<Key , Val>::print(){
print(root);
}
template<typename Key , typename Val>
inline void map<Key , Val>::print(p_rbnode node){
if (node == nullnode){
return;
}
print(node->left);
cout << node->data << endl;
print(node->right);
}
}
#endif