-
Notifications
You must be signed in to change notification settings - Fork 1
/
RedBlackTree.hpp
380 lines (337 loc) · 9.8 KB
/
RedBlackTree.hpp
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
#ifndef REDBLACKTREE_H
#define REDBLACKTREE_H
// @Author Liu JD
// left-leaning red black tree.
// insert node method is in recursive.
#define RED true
#define BLACK false
template<typename T, typename KEY> class RBTree;
template<typename T, typename KEY> class RBTNode
{
friend class RBTree<T, KEY>;
protected:
KEY key;
T value;
bool color;
RBTNode<T, KEY> *pLeft;
RBTNode<T, KEY> *pRight;
RBTNode<T, KEY> *pParent;
T *getvalue();
public:
RBTNode(T v, KEY k, bool c=BLACK): value(v), key(k), color(c) {pLeft = NULL; pRight = NULL; pParent = NULL;}
void print() const;
};
template<typename T, typename KEY> void RBTNode<T, KEY>::print() const
{
std::cout << key << ": " << value << ", ";
if (color == RED)
std::cout << "red";
else
std::cout << "black";
std::cout << std::endl;
}
template<typename T, typename KEY> T *RBTNode<T, KEY>::getvalue()
{
T *pvalue = &this->value;
return pvalue;
}
template<typename T, typename KEY> class RBTree
{
private:
RBTNode<T, KEY> *pRoot;
public:
RBTree() { pRoot = NULL; }
//~RBTree() { while(pRoot != NULL) deleteone(pRoot->key); } // simply way to delete the entire tree.
~RBTree();
private:
bool isRed(RBTNode<T, KEY> *h) const;
RBTNode<T, KEY> *RotateLeft(RBTNode<T, KEY> *);
RBTNode<T, KEY> *RotateRight(RBTNode<T, KEY> *);
void flipcolors(RBTNode<T, KEY> *);
void regaincolors(RBTNode<T, KEY> *);
RBTNode<T, KEY> *MoveRedLeft(RBTNode<T, KEY> *);
RBTNode<T, KEY> *MoveRedRight(RBTNode<T, KEY> *);
void setnull(RBTNode<T, KEY> *);
public:
// retrieve data from a given key
T *get(KEY Key);
RBTNode<T, KEY> *getmin();
protected:
RBTNode<T, KEY> *get(RBTNode<T, KEY> *h, KEY Key);
RBTNode<T, KEY> *getmin(RBTNode<T, KEY> *h);
public:
// insert a new node given a value and a key.
// Key should be comparable and value should has a copy constructor and operator =.
void put(T Value, KEY Key);
protected:
RBTNode<T, KEY> *put(RBTNode<T, KEY> *h, T Value, KEY Key);
public:
// delete the minimum or maximum value.
void deletemin();
void deletemax();
// delete a specified node.
void deleteone(KEY Key);
protected:
RBTNode<T, KEY> *deletemin(RBTNode<T, KEY> *h);
RBTNode<T, KEY> *deletemax(RBTNode<T, KEY> *h);
RBTNode<T, KEY> *deleteone(RBTNode<T, KEY> *h, KEY Key);
public:
// Traverse the RBTree
void Inorder_Tree_Walk();
protected:
void Inorder_Tree_Walk(RBTNode<T, KEY> *p);
};
template<typename T, typename KEY> RBTree<T, KEY>::~RBTree()
{
setnull(pRoot);
}
template<typename T, typename KEY> void RBTree<T, KEY>::setnull(RBTNode<T, KEY> *h)
{
if (h == NULL)
{
return;
}
setnull(h->pLeft);
setnull(h->pRight);
{
//std::cout << "delete " << h->key << std::endl;
delete h;
h = NULL;
}
}
template<typename T, typename KEY> bool RBTree<T, KEY>::isRed(RBTNode<T, KEY> *h) const
{
if ( h != NULL)
return h->color;
return BLACK;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::RotateLeft(RBTNode<T, KEY> *h)
{
RBTNode<T, KEY> *x = h->pRight;
h->pRight = x->pLeft;
x->pLeft = h;
x->color = h->color;
h->color = RED;
return x;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::RotateRight(RBTNode<T, KEY> *h)
{
RBTNode<T, KEY> *x = h->pLeft;
h->pLeft = x->pRight;
x->pRight = h;
x->color = h->color;
h->color = RED;
return x;
}
template<typename T, typename KEY> void RBTree<T, KEY>::flipcolors(RBTNode<T, KEY> *h)
{
h->color = RED;
h->pLeft->color = BLACK;
h->pRight->color = BLACK;
}
template<typename T, typename KEY> void RBTree<T, KEY>::regaincolors(RBTNode<T, KEY> *h)
{
h->color = BLACK;
if (h->pLeft)
h->pLeft->color = RED;
if (h->pRight)
h->pRight->color = RED;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::MoveRedLeft(RBTNode<T, KEY> *h)
{
regaincolors(h);
if (isRed(h->pRight->pLeft))
{
h->pRight = RotateRight(h->pRight);
h = RotateLeft(h);
flipcolors(h);
}
return h;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::MoveRedRight(RBTNode<T, KEY> *h)
{
regaincolors(h);
if (isRed(h->pLeft->pLeft))
{
h = RotateRight(h);
flipcolors(h);
}
return h;
}
template<typename T, typename KEY> void RBTree<T, KEY>::put(T Value, KEY Key)
{
pRoot = put(pRoot, Value, Key);
pRoot->color = BLACK;
//std::cout << "Insert new node: " << Key << " Value:" << Value << ", Root: " << pRoot->key << std::endl;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::put(RBTNode<T, KEY> *h, T Value, KEY Key)
{
if (h == NULL)
{
h = new RBTNode<T, KEY>(Value, Key, RED);
return h;
}
if (h->key > Key) h->pLeft = put(h->pLeft, Value, Key);
else if (h->key < Key) h->pRight = put(h->pRight, Value, Key);
else h->value = Value;
if (!isRed(h->pLeft) && isRed(h->pRight)) { h = RotateLeft(h); }
if (isRed(h->pLeft) && isRed(h->pLeft->pLeft)) { h=RotateRight(h); }
if (isRed(h->pLeft) && isRed(h->pRight)) { flipcolors(h); }
return h;
}
template<typename T, typename KEY> void RBTree<T, KEY>::deletemin()
{
if (pRoot) // check if the tree is empty.
{
if (!isRed(pRoot->pLeft)) // Root contains only one node. Both its left and right child are black.
pRoot->color = RED;
pRoot = deletemin(pRoot);
if (pRoot)
pRoot->color = BLACK;
}
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::deletemin(RBTNode<T, KEY> *h)
{
if (h->pLeft == NULL)
{
// be careful here hasn't check if the h is empty.
//std::cout << "Delete min " << h->key << std::endl;
delete h;
return NULL;
}
if (!isRed(h->pLeft) && !isRed(h->pLeft->pLeft))
h = MoveRedLeft(h);
h->pLeft = deletemin(h->pLeft);
if (isRed(h->pRight))
h = RotateLeft(h);
return h;
}
template<typename T, typename KEY> void RBTree<T, KEY>::deletemax()
{
if (pRoot) // check if the tree is empty.
{
if (!isRed(pRoot->pLeft))
pRoot->color = RED;
pRoot = deletemax(pRoot);
if (pRoot)
pRoot->color = BLACK;
}
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::deletemax(RBTNode<T, KEY> *h)
{
if (isRed(h->pLeft))
h = RotateRight(h);
if (h->pRight == NULL)
{
// be careful here hasn't check if the h is empty.
//std::cout << "Delete max " << h->key << std::endl;
delete h;
return NULL;
}
if (!isRed(h->pRight) && !isRed(h->pRight->pLeft))
h = MoveRedRight(h);
h->pRight = deletemax(h->pRight);
if (isRed(h->pRight))
h = RotateLeft(h);
return h;
}
template<typename T, typename KEY> void RBTree<T, KEY>::deleteone(KEY Key)
{
if (pRoot)
{
//std::cout << "Deleting: " << Key << std::endl;
if (!isRed(pRoot->pLeft))
pRoot->color = RED;
pRoot = deleteone(pRoot, Key);
if (pRoot)
pRoot->color = BLACK;
}
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::deleteone(RBTNode<T, KEY> *h, KEY Key)
{
// Delete a node.
// However will be not sure Key exists in the tree. Check if the Key exists before deleting.
if (Key < h->key)
{
if (!isRed(h->pLeft) && !isRed(h->pLeft->pLeft))
h = MoveRedLeft(h);
h->pLeft = deleteone(h->pLeft, Key);
}
else{
if (isRed(h->pLeft))
h = RotateRight(h);
if ((Key == h->key) && !(h->pRight) )
{
//std::cout << "Delete one specified node: " << h->key << ", value: " << h->value << std::endl;
delete h;
return NULL;
}
if (!isRed(h->pRight) && !isRed(h->pRight->pLeft))
h = MoveRedRight(h);
if (Key == h->key)
{
//std::cout << "To delete " << h->key << ", delete subtree's min node" << std::endl;
RBTNode<T, KEY> *temp = getmin(h->pRight);
h->value = temp->value;
h->key = temp->key;
h->pRight = deletemin(h->pRight);
}
else
h->pRight = deleteone(h->pRight, Key);
}
if (isRed(h->pRight))
h = RotateLeft(h);
return h;
}
template<typename T, typename KEY> T *RBTree<T, KEY>::get(KEY Key)
{
if (pRoot)
{
RBTNode<T, KEY> *pNode = get(pRoot, Key);
return pNode->getvalue();
}
else return NULL;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::get(RBTNode<T, KEY> *h, KEY Key)
{
if ((h == NULL) || (Key == h->key) )
return h;
if (Key < h->key)
return get(h->pLeft, Key);
else
return get(h->pRight, Key);
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::getmin()
{
if (pRoot)
return getmin(pRoot);
else
return NULL;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::getmin(RBTNode<T, KEY> *h)
{
if (h->pLeft)
return getmin(h->pLeft);
else return h;
}
template<typename T, typename KEY> void RBTree<T, KEY>::Inorder_Tree_Walk()
{
Inorder_Tree_Walk(pRoot);
if (pRoot)
{
std::cout << "Root: ";
pRoot->print();
}
else
std::cout << "RBTree Empty!" << std::endl;
}
template<typename T, typename KEY> void RBTree<T, KEY>::Inorder_Tree_Walk(RBTNode<T, KEY> *p)
{
if (p != NULL)
{
Inorder_Tree_Walk(p->pLeft);
p->print();
Inorder_Tree_Walk(p->pRight);
}
}
#endif