Skip to content

Commit 8ca14d9

Browse files
committed
add nil root
1 parent 722e720 commit 8ca14d9

File tree

2 files changed

+74
-49
lines changed

2 files changed

+74
-49
lines changed

RB_Tree/main.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
int main(int argc, char* argv[]) {
77
rbTree<int> *myTree = new rbTree<int>();
88
myTree->InsertNode(1);
9-
// myTree->InsertNode(2);
10-
// myTree->InsertNode(3);
11-
12-
for (int i = 2; i <= 20; ++i) {
9+
for (int i = 2; i <= 10; ++i) {
1310
myTree->InsertNode(i);
1411
}
1512
myTree->print();

RB_Tree/rbTree.h

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <unordered_map>
77
#include <queue>
88
#include <iostream>
9+
10+
std::unordered_map<int, std::string> colorMap = {
11+
{0, "(b)"},
12+
{1, "(r)"}
13+
};
14+
915
template <typename T>
1016
class rbTree{
1117
public:
@@ -59,12 +65,21 @@ void rbTree<T>::InsertNode(T val) {
5965
rootNode->color_tag = 0;
6066
return;
6167
}
62-
while (tempNode != nullptr) {
68+
while (tempNode->data != -1) {
6369
if (val > tempNode->data) {
64-
if (tempNode->rightChild == nullptr) {
70+
if (tempNode->rightChild->data == -1) {
71+
delete tempNode->rightChild;
6572
tempNode->rightChild = new rbTreeNode<T>(val);
6673
tempNode->rightChild->color_tag = 1;
6774
tempNode->rightChild->fatherNode = tempNode;
75+
tempNode->rightChild->leftChild = new rbTreeNode<T>(-1);
76+
tempNode->rightChild->leftChild->fatherNode = tempNode->rightChild;
77+
tempNode->rightChild->leftChild->color_tag = 0;
78+
79+
tempNode->rightChild->rightChild = new rbTreeNode<T>(-1);
80+
tempNode->rightChild->rightChild->fatherNode = tempNode->rightChild;
81+
tempNode->rightChild->rightChild->color_tag = 0;
82+
6883
if (tempNode->color_tag == 1) {
6984
fixTree(tempNode->rightChild);
7085
}
@@ -73,10 +88,20 @@ void rbTree<T>::InsertNode(T val) {
7388
tempNode = tempNode->rightChild;
7489
}
7590
} else {
76-
if (tempNode->leftChild == nullptr) {
91+
if (tempNode->leftChild->data == -1) {
92+
delete tempNode->leftChild;
7793
tempNode->leftChild = new rbTreeNode<T>(val);
7894
tempNode->leftChild->color_tag = 1;
7995
tempNode->leftChild->fatherNode = tempNode;
96+
97+
tempNode->leftChild->leftChild = new rbTreeNode<T>(-1);
98+
tempNode->leftChild->leftChild->fatherNode = tempNode->leftChild;
99+
tempNode->leftChild->leftChild->color_tag = 0;
100+
101+
tempNode->leftChild->rightChild = new rbTreeNode<T>(-1);
102+
tempNode->leftChild->rightChild->fatherNode = tempNode->leftChild;
103+
tempNode->leftChild->rightChild->color_tag = 0;
104+
80105
if (tempNode->color_tag == 1) {
81106
fixTree(tempNode->leftChild);
82107
}
@@ -91,23 +116,35 @@ void rbTree<T>::InsertNode(T val) {
91116
template <typename T>
92117
int rbTree<T>::deleteNode(T valDelete) {
93118
rbTreeNode<T> *tempNode = rootNode;
94-
while (tempNode->data != valDelete && tempNode != nullptr) {
119+
while (tempNode->data != valDelete && tempNode->data != -1) {
95120
if (valDelete > tempNode->data) {
96121
tempNode = tempNode->rightChild;
97122
} else {
98123
tempNode = tempNode->leftChild;
99124
}
100125
}
101126
// not find the val to delete
102-
if (!tempNode) {
127+
if (tempNode->data == -1) {
103128
return -1;
104129
}
105130
int colorTag = tempNode->color_tag;
106131
rbTreeNode<T> *fixPos = nullptr;
107-
if (tempNode->leftChild == nullptr && tempNode->rightChild == nullptr) {
132+
if (tempNode->leftChild->data == -1 && tempNode->rightChild->data == -1) {
133+
if (tempNode = tempNode->fatherNode->leftChild) {
134+
tempNode->fatherNode->leftChild = new rbTreeNode<T>(-1);
135+
tempNode->fatherNode->leftChild->fatherNode = tempNode->fatherNode;
136+
tempNode->fatherNode->leftChild->color_tag = 0;
137+
} else {
138+
tempNode->fatherNode->rightChild = new rbTreeNode<T>(-1);
139+
tempNode->fatherNode->rightChild->fatherNode = tempNode->fatherNode;
140+
tempNode->fatherNode->rightChild->color_tag = 0;
141+
}
142+
delete tempNode->leftChild;
143+
delete tempNode->rightChild;
108144
delete tempNode;
145+
109146
return 1;
110-
} else if (tempNode->leftChild == nullptr && tempNode->rightChild != nullptr) {
147+
} else if (tempNode->leftChild->data == -1 && tempNode->rightChild->data != -1) {
111148
fixPos = tempNode->rightChild;
112149
if (tempNode != rootNode) {
113150
if (tempNode->fatherNode->leftChild == tempNode) {
@@ -121,7 +158,9 @@ int rbTree<T>::deleteNode(T valDelete) {
121158
rootNode = tempNode->rightChild;
122159
tempNode->rightChild->fatherNode = nullptr;
123160
}
124-
} else if (tempNode->leftChild != nullptr && tempNode->rightChild == nullptr) {
161+
delete tempNode->leftChild;
162+
delete tempNode;
163+
} else if (tempNode->leftChild->data != -1 && tempNode->rightChild->data == -1) {
125164
fixPos = tempNode->leftChild;
126165
if (tempNode != rootNode) {
127166
if (tempNode->fatherNode->leftChild == tempNode) {
@@ -135,7 +174,9 @@ int rbTree<T>::deleteNode(T valDelete) {
135174
rootNode = tempNode->leftChild;
136175
tempNode->leftChild->fatherNode = nullptr;
137176
}
138-
} else if (tempNode->leftChild != nullptr && tempNode->rightChild != nullptr) {
177+
delete tempNode->rightChild;
178+
delete tempNode;
179+
} else if (tempNode->leftChild->data != -1 && tempNode->rightChild->data != -1) {
139180

140181
if (tempNode != rootNode) {
141182
rbTreeNode<T> *succNode = findSuccessorNode(tempNode);
@@ -146,19 +187,23 @@ int rbTree<T>::deleteNode(T valDelete) {
146187
if (tempNode == tempNode->fatherNode->leftChild) {
147188
tempNode->fatherNode->leftChild = succNode;
148189

190+
delete succNode->leftChild;
149191
succNode->leftChild = tempNode->leftChild;
150192
succNode->leftChild->fatherNode = succNode;
151193

152194
succNode->fatherNode = tempNode->fatherNode;
153195
succNode->color_tag = tempNode->color_tag;
196+
delete tempNode;
154197
} else {
155198
tempNode->fatherNode->rightChild = succNode;
156199

200+
delete succNode->leftChild;
157201
succNode->leftChild = tempNode->leftChild;
158202
succNode->leftChild->fatherNode = succNode;
159203

160204
succNode->fatherNode = tempNode->fatherNode;
161205
succNode->color_tag = tempNode->color_tag;
206+
delete tempNode;
162207
}
163208
} else {
164209
if (succNode->rightChild) {
@@ -171,22 +216,26 @@ int rbTree<T>::deleteNode(T valDelete) {
171216
succNode->rightChild = tempNode->rightChild;
172217
succNode->rightChild->fatherNode = succNode;
173218

219+
delete succNode->leftChild;
174220
succNode->leftChild = tempNode->leftChild;
175221
succNode->leftChild->fatherNode = succNode;
176222

177223
succNode->fatherNode = tempNode->fatherNode;
178224
succNode->color_tag = tempNode->color_tag;
225+
delete tempNode;
179226
} else {
180227
tempNode->fatherNode->rightChild = succNode;
181228

182229
succNode->rightChild = tempNode->rightChild;
183230
succNode->rightChild->fatherNode = succNode;
184231

232+
delete succNode->leftChild;
185233
succNode->leftChild = tempNode->leftChild;
186234
succNode->leftChild->fatherNode = succNode;
187235

188236
succNode->fatherNode = tempNode->fatherNode;
189237
succNode->color_tag = tempNode->color_tag;
238+
delete tempNode;
190239
}
191240
}
192241

@@ -196,18 +245,21 @@ int rbTree<T>::deleteNode(T valDelete) {
196245
colorTag = succNode->color_tag;
197246

198247
if (succNode->fatherNode == tempNode) {
248+
delete succNode->leftChild;
199249
succNode->leftChild = tempNode->leftChild;
200250
succNode->leftChild->fatherNode = succNode;
201251

202252
succNode->fatherNode = nullptr;
203253
rootNode = succNode;
204254

205-
succNode->color_tag = tempNode->color_tag;
255+
succNode->color_tag = tempNode->color_tag;
256+
delete tempNode;
206257
} else {
207258
if (succNode->rightChild) {
208259
succNode->fatherNode->leftChild = succNode->rightChild;
209260
succNode->rightChild->fatherNode = succNode->fatherNode;
210261
}
262+
delete succNode->leftChild;
211263
succNode->leftChild = tempNode->leftChild;
212264
succNode->leftChild->fatherNode = succNode;
213265

@@ -216,8 +268,8 @@ int rbTree<T>::deleteNode(T valDelete) {
216268

217269
succNode->fatherNode = nullptr;
218270
rootNode = succNode;
219-
220271
succNode->color_tag = tempNode->color_tag;
272+
delete tempNode;
221273
}
222274
}
223275

@@ -238,12 +290,14 @@ void rbTree<T>::fixTree(rbTreeNode<T> *curNode) {
238290
grandFatherNode = tempCurNode->fatherNode->fatherNode;
239291
if (tempCurNode->fatherNode == grandFatherNode->leftChild) {
240292
uncleNode = grandFatherNode->rightChild;
293+
// uncle node exist and is red
241294
if (uncleNode && uncleNode->color_tag == 1) {
242295
tempCurNode->fatherNode->color_tag = 0;
243296
uncleNode->color_tag = 0;
244297
grandFatherNode->color_tag = 1;
245298
tempCurNode = grandFatherNode;
246-
} else {
299+
} else { // uncle node does not exist
300+
// left -> right
247301
if (tempCurNode == tempCurNode->fatherNode->rightChild) {
248302
tempCurNode = tempCurNode->fatherNode;
249303
leftRotate(tempCurNode);
@@ -271,59 +325,33 @@ void rbTree<T>::fixTree(rbTreeNode<T> *curNode) {
271325
}
272326
}
273327
rootNode->color_tag = 0;
274-
// while (true) {
275-
// if (tempCurNode->fatherNode == nullptr) {
276-
// break;
277-
// }
278-
// if (tempCurNode->fatherNode->color_tag == 1) {
279-
// break;
280-
// }
281-
// rbTreeNode<T> *fatherNode = tempCurNode->fatherNode;
282-
// rbTreeNode<T> *grandFatherNode = fatherNode->fatherNode;
283-
// if (grandFatherNode) {
284-
// if (fatherNode == grandFatherNode->leftChild) {
285-
// uncleNode = grandFatherNode->rightChild;
286-
// if (uncleNode) {
287-
// // sitution 1 father and uncle is red, recoler them and grandfather(only when grandfather is not root)
288-
// if (uncleNode->color_tag == 1) {
289-
// uncleNode->color_tag = 0;
290-
// fatherNode->color_tag = 0;
291-
// if (grandFatherNode != rootNode) {
292-
// grandFatherNode = 1;
293-
// } else {
294-
// break;
295-
// }
296-
// tempCurNode = grandFatherNode;
297-
// }
298-
// } else if (tempCurNode == fatherNode->rightChild) {
299-
// // 叔叔是黑色节点,当前节点为右孩子,左旋,父节点作为当前节点
300-
// }
301-
// }
302-
// }
303-
// }
304328
}
305329

306330
template <typename T>
307331
void rbTree<T>::fixTreeDelete(rbTreeNode<T>* curNode) {
308332
while (curNode != rootNode && curNode->color_tag == 0) {
309333
if (curNode == curNode->fatherNode->leftChild) {
310334
rbTreeNode<T> *brotherNode = curNode->fatherNode->rightChild;
335+
// situation 1, brother is red
311336
if (brotherNode->color_tag == 1) {
312337
brotherNode->color_tag = 0;
313338
curNode->fatherNode->color_tag = 1;
314339
leftRotate(curNode->fatherNode);
315340
brotherNode = curNode->fatherNode->rightChild;
316341
}
342+
// situation 2
317343
if (brotherNode->leftChild->color_tag == 0 && brotherNode->rightChild->color_tag == 0) {
318344
brotherNode->color_tag = 1;
319345
curNode = curNode->fatherNode;
320346
continue;
321347
} else if (brotherNode->rightChild->color_tag == 0) { // brother's right is black
348+
// situation 3
322349
brotherNode->leftChild->color_tag = 0;
323350
brotherNode->color_tag = 1;
324351
rightRotate(brotherNode);
325352
brotherNode = curNode->fatherNode->rightChild;
326353
}
354+
// situation 4
327355
brotherNode->color_tag = curNode->fatherNode->color_tag;
328356
curNode->fatherNode->color_tag = 0;
329357
brotherNode->rightChild->color_tag = 0;
@@ -400,7 +428,7 @@ void rbTree<T>::rightRotate(rbTreeNode<T> *curNode) {
400428
template <typename T>
401429
rbTreeNode<T>* rbTree<T>::findSuccessorNode(rbTreeNode<T>* curNode) {
402430
rbTreeNode<T> *tempNode = curNode->rightChild;
403-
while (tempNode->leftChild != nullptr) {
431+
while (tempNode->leftChild->data != -1) {
404432
tempNode = tempNode->leftChild;
405433
}
406434
return tempNode;
@@ -434,7 +462,7 @@ void rbTree<T>::print() {
434462
std::unordered_map<rbTreeNode<T>*, int> first_locations;
435463
for (auto &i : intv) {
436464
location = template_str.size();
437-
template_str += std::to_string(i->data) + " ";
465+
template_str += std::to_string(i->data) + colorMap[i->color_tag] + " ";
438466
first_locations[i] = location;
439467
}
440468
for (auto &i : template_str) i = ' ';
@@ -448,7 +476,7 @@ void rbTree<T>::print() {
448476
auto node = q.front();
449477
q.pop();
450478
cur_loc = first_locations[node];
451-
std::string num_str = std::to_string(node->data);
479+
std::string num_str = std::to_string(node->data) + colorMap[node->color_tag];
452480
//左边,如果存在左孩子,那么在第二行对应位置打印'/',在第一行补上'_'
453481
if (node->leftChild) {
454482
q.push(node->leftChild);

0 commit comments

Comments
 (0)