-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRedBlackTree.h
322 lines (295 loc) · 6.46 KB
/
RedBlackTree.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
/*
RedBlackTree.h
红黑树实现(没有父亲指针)
2016/10/20
*/
#ifndef REDBLACKTREE_H_
#define REDBLACKTREE_H_
#include <iostream>
#include <algorithm>
#include "dsexception.h"
template<typename Comparable>
class RedBlackTree
{
public:
explicit RedBlackTree(const Comparable & negInf)
{
/*negInf代表一个不用的值,传给header节点*/
// 空节点
nullNode = new RedBlackNode{};
nullNode->left = nullNode;
nullNode->right = nullNode;
// 标兵节点
header = new RedBlackNode{ negInf };
header->left = nullNode;
header->right = nullNode;
}
// copy构造函数
RedBlackTree(const RedBlackTree & rhs)
{
nullNode = new RedBlackNode{};
nullNode->left = nullNode;
nullNode->right = nullNode;
header = new RedBlackNode{ rhs.header->element };
header->left = nullNode;
header->right = clone(rhs.header->right);
}
// move构造函数
RedBlackTree(RedBlackTree && rhs)
:nullNode{ rhs.nullNode }, header{rhs.header}
{
rhs.nullNode = nullptr;
rhs.header = nullptr;
}
// copy赋值操作符重载
RedBlackTree & operator=(const RedBlackTree & rhs)
{
RedBlackTree copy = rhs;
std::swap(copy, *this);
return *this;
}
// move赋值操作符重载
RedBlackTree & operator=(RedBlackTree && rhs)
{
std::swap(header, rhs.header);
std::swap(nullNode, rhs.nullNode);
return *this;
}
~RedBlackTree()
{
makeEmpty();
delete header;
delete nullNode;
}
const Comparable & findMax() const
{
if (isEmpty())
{
throw UnderflowException{};
}
RedBlackNode *p = header->right;
while (p->right != nullNode)
{
p = p->right;
}
return p->element;
}
const Comparable & findMin() const
{
if (isEmpty())
{
throw UnderflowException{};
}
RedBlackNode *p = header->right;
while (p->left != nullNode)
{
p = p->left;
}
return p->element;
}
bool contains(const Comparable & x) const
{
RedBlackNode *p = header->right;
while (p != nullNode)
{
if (x < p->element)
{
p = p->left;
}
else if (x > p->element)
{
p = p->right;
}
else
{
return true;
}
}
return false;
}
bool isEmpty() const { return header->right == nullNode; }
void printTree() const
{
if (isEmpty())
{
std::cout << "Empty tree." << std::endl;
}
printTree(header->right);
}
// 插入函数
void insert(const Comparable & x)
{
current = parent = grand = great = header;
nullNode->element = x; // 保证到达叶子节点
while (current->element != x)
{
great = grand;
grand = parent;
parent = current;
current = x < current->element ? current->left : current->right;
if (current->left->color == RED && current->right->color == RED)
{
handleReorient(x);
}
}
if (current != nullNode)
{
return; // 已经存在包含相同值的节点
}
current = new RedBlackNode{ x, nullNode, nullNode };
if (x < parent->element)
{
parent->left = current;
}
else
{
parent->right = current;
}
handleReorient(x);
}
/*void insert(Comparable && x);
void remove(const Comparable & x);*/
void makeEmpty()
{
if (header == nullptr)
{
return;
}
reclaimMemory(header->right);
header->right = nullNode;
}
private:
// 颜色枚举类型
enum COLOR
{
RED,
BLACK
};
// 节点
struct RedBlackNode
{
Comparable element;
RedBlackNode *left;
RedBlackNode *right;
int color;
RedBlackNode(const Comparable & el = Comparable{}, RedBlackNode *lt = nullptr,
RedBlackNode *rt = nullptr, int theColor = BLACK)
:element{ el }, left{ lt }, right{ rt }, color{ theColor }
{}
RedBlackNode(Comparable && el, RedBlackNode *lt = nullptr, RedBlackNode *rt = nullptr,
int theColor = BLACK)
:element{ std::move(el) }, left{ lt }, right{ rt }, color{ theColor }
{}
};
RedBlackNode *header; // 哨兵节点,不存任何实质元素,右节点指向树的实际根节点
RedBlackNode *nullNode; // 空节点,与实例绑定(切记)
// 插入操作的辅助指针(可认为是静态的)
RedBlackNode *current;
RedBlackNode *parent;
RedBlackNode *grand;
RedBlackNode *great;
// 递归函数
// 回收内存
void reclaimMemory(RedBlackNode *t)
{
if (t != t->left)
{
reclaimMemory(t->left);
reclaimMemory(t->right);
delete t;
}
}
// 中序遍历打印树
void printTree(RedBlackNode *t) const
{
if (t != t->left)
{
printTree(t->left);
std::cout << t->element << std::endl;
printTree(t->right);
}
}
RedBlackNode * clone(RedBlackNode *t) const // 用于复制
{
if (t == t->left) // 空节点 不能使用nullNode因为不同实例的nullNode不同
{
return nullNode;
}
return new RedBlackNode{ t->element, clone(t->left), clone(t->right), t->color };
}
// 红黑树主要操作
/**
* 供insert调用(当一个节点具有两个红色子节点时)
* 进行flip和rotate
*/
void handleReorient(const Comparable & item)
{
// 颜色翻转
current->color = RED;
current->left->color = BLACK;
current->right->color = BLACK;
// 如果parent是红色,那么要进行rotate
if (parent->color == RED)
{
grand->color = RED; // 祖父节点涂红
if ((item < parent->element) != (item < grand->element)) // R-L 或者 L-R
{
parent = rotate(item, grand); // 转化成L-L或者R-R
}
current = rotate(item, great); // 处理L-L或者R-R
current->color = BLACK; // 当前颜色涂黑
}
// 根节点涂黑色
header->color = BLACK;
}
/**
* handleReorient调用的旋转函数,包含四种情况
* item是handleReorient函数中的,用于区分四种情况
* theParent是要旋转子树根节点的父节点
* 最后返回旋转子树的根节点
*/
RedBlackNode * rotate(const Comparable & item, RedBlackNode *theParent)
{
if (item < theParent->element) // 子树是左子树
{
if (item < theParent->left->element) // L-L
{
rotateWithLeftChild(theParent->left);
}
else // L-R
{
rotateWithRightChild(theParent->left);
}
return theParent->left;
}
else // 子树是右子树
{
if (item < theParent->right->element) // R-L
{
rotateWithLeftChild(theParent->right);
}
else // R-R
{
rotateWithRightChild(theParent->right);
}
return theParent->right;
}
}
// 具有左节点的节点右旋
void rotateWithLeftChild(RedBlackNode * & t)
{
RedBlackNode * lt = t->left;
t->left = lt->right;
lt->right = t;
t = lt;
}
// 具有右节点的节点左旋
void rotateWithRightChild(RedBlackNode * & t)
{
RedBlackNode *rt = t->right;
t->right = rt->left;
rt->left = t;
t = rt;
}
};
#endif