-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred-black-tree.cc
259 lines (215 loc) · 4.66 KB
/
red-black-tree.cc
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
/*Name: Red-Black tree with sentinel leaf node*
Skills: constructor in struct
*/
#include <iostream>
#include <queue>
using namespace std;
template<typename T>
struct Node
{
Node ()
: left (0),
right (0),
p (0),
colour ("red")
{
}
T key; // key value
struct Node<T>* left; // left child
struct Node<T>* right; // right child
struct Node<T>* p; // parant
string colour; // color
};
template<typename T>
class RedBlackTree
{
public:
RedBlackTree ()
: m_nil (0),
m_root (0)
{
// create sentinel node
m_nil = new struct Node<T>;
// set root to sentinel node
m_root = m_nil;
// configure sentinel node
m_nil->p = m_root;
m_nil->left = m_root;
m_nil->right = m_root;
m_nil->colour = "black";
}
virtual ~RedBlackTree ()
{
delete m_nil;
delete m_root;
}
/*APIs*/
bool Insert (T &key);
// bool Delete ();
void Print ();
void LeftRotate (struct Node<T> &node); // check right child == m_nil?
void RightRotate (struct Node<T> &node); // check left child == m_nil?
private:
struct Node<T>* m_nil; // sentinel node
struct Node<T>* m_root;
};
template<typename T>
bool RedBlackTree<T>::Insert (T &key)
{
struct Node<T> *node = new struct Node<T>;
struct Node<T> *cur = m_root;
struct Node<T> *pre = m_nil;
while (cur != m_nil)
{
pre = cur;
if ( key < cur->key )
cur = cur->left;
else if ( key >= cur->key )
cur = cur->right;
}
if (pre == m_nil) // first node
m_root = node;
else if ( key < pre->key ) // add new node in the left leaf
pre->left = node;
else if ( key >= pre->key ) // add new node in the right leaf
pre->right = node;
// configure new node
node->p = pre;
node->key = key;
node->left = m_nil;
node->right = m_nil;
node->colour = "red";
}
template<typename T>
void RedBlackTree<T>::Print ()
{
// print level by level
queue<struct Node<T>* > q;
int curLevelNum = 0;
int nextLevelNum = 0;
struct Node<T> *node = m_root;
q.push (node);
curLevelNum++;
while (q.size () > 0)
{
struct Node<T> *cur = q.front ();
cout << cur->key << " ";
q.pop ();
curLevelNum--;
if (cur->left != m_nil)
{
q.push (cur->left);
nextLevelNum++;
}
if (cur->right != m_nil)
{
q.push (cur->right);
nextLevelNum++;
}
if (curLevelNum == 0)
{
curLevelNum = nextLevelNum;
nextLevelNum = 0;
cout << endl;
}
}
}
template<typename T>
void RedBlackTree<T>::LeftRotate (struct Node<T> &node)
{
/*
y x
/ \ <- / \
x yr xl y
/ \ / \
xl yl yl yr
*/
// in this version, left rotate to the root node by default
struct Node<T> *x = m_root;
struct Node<T> *y = m_root->right;
// do not do left rotate if the right child is leaf
if ( y == m_nil)
return;
// move yl from y to x
x->right = y->left;
y->left->p = x;
// move x->p from x to y
y->p = x->p;
if ( x->p == m_nil)
m_root = y;
else if ( x == x->left)
x->p->left = y;
else
x->p->right = y;
// switch x and y
y->left = x;
x->p = y;
}
template<typename T>
void RedBlackTree<T>::RightRotate (struct Node<T> &node)
{
/*
y x
/ \ -> / \
x yr xl y
/ \ / \
xl xr xr yr
*/
// in this version, left rotate to the root node by default
struct Node<T> *y = m_root;
struct Node<T> *x = m_root->left;
// do not do right rotate if the right child is leaf
if ( x == m_nil)
return;
// move xr from x to y
y->left = x->right;
x->right->p = y;
// move y->p from y to x
x->p = y->p;
if ( y == m_root)
m_root = x;
else if ( y == y->p->left)
y->p->left = x;
else
y->p->right = x;
// switch x and y
x->right = y;
y->p = x;
}
int main (int argc, char *argv[])
{
typedef int type;
RedBlackTree<type> rb;
int i = 5;
rb.Insert (i);
i = 2;
rb.Insert (i);
i = 8;
rb.Insert (i);
i = 1;
rb.Insert (i);
i = 4;
rb.Insert (i);
i = 3;
rb.Insert (i);
i = 6;
rb.Insert (i);
i = 7;
rb.Insert (i);
i = 10;
rb.Insert (i);
i = 9;
rb.Insert (i);
cout << "Print: " << endl;
rb.Print ();
cout << "left rotate" << endl;
struct Node<type> node;
rb.LeftRotate (node);
cout << "Print: " << endl;
rb.Print ();
cout << "right rotate" << endl;
rb.RightRotate (node);
cout << "Print: " << endl;
rb.Print ();
return 0;
}