/
19RBTree.c
212 lines (194 loc) · 6.14 KB
/
19RBTree.c
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
/*************************************************************************
> File Name: test.cpp
> Author: snowflake
> Mail: ︿( ̄︶ ̄)︿
> Created Time: 2018年09月28日 星期五 16时21分13秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct RBNode {
int key, color; // 0: red 1: black 2:double black
struct RBNode *lchild, *rchild;
} RBNode;
RBNode *NIL;
__attribute__((constructor))
void init_nil() {
NIL = (RBNode *)malloc(sizeof(RBNode));
NIL->color = 1;
NIL->key = 0;
NIL->lchild = NIL->rchild = NIL;
return ;
}
RBNode *get_node(int key) {
RBNode *p = (RBNode *)malloc(sizeof(RBNode));
p->key = key;
p->color = 0;
p->lchild = p->rchild = NIL;
return p;
}
int hasRedchild(RBNode *node) {
return node->lchild->color == 0 || node->rchild->color == 0;
}
RBNode *left_rotate(RBNode *root) {
RBNode *temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
return temp;
}
RBNode *right_rotate(RBNode *root) {
RBNode *temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
return temp;
}
RBNode *insert_maintain(RBNode *root) {
if (!hasRedchild(root)) return root;//当前节点没有红色孩子节点的时候,插入不会发生冲突,如果是判断是否为NIL节点时,当他的2个子孩子为黑时也会调整
if (root->lchild->color == 0 && root->rchild->color == 0) {
if (hasRedchild(root->lchild) || hasRedchild(root->rchild)) {
root->color = 0;
root->lchild->color = root->rchild->color = 1;
}
return root;
}
if (root->lchild->color == 0) {
if (!hasRedchild(root->lchild)) return root;
if (root->lchild->rchild->color == 0) {
root->lchild = left_rotate(root->lchild);
}
root = right_rotate(root);
} else {
if (!hasRedchild(root->rchild)) return root;
if (root->rchild->lchild->color == 0) {
root->rchild = right_rotate(root->rchild);
}
root = left_rotate(root);
}
root->color = 0;
root->lchild->color = root->rchild->color = 1;
return root;
}
RBNode *__insert(RBNode *root, int key) {
if (root == NIL) return get_node(key);
if (root->key == key) return root;
if (root->key > key) root->lchild = __insert(root->lchild, key);
else root->rchild = __insert(root->rchild, key);
return insert_maintain(root);
}
RBNode *insert(RBNode *root, int key) {
root = __insert(root, key);
root->color = 1;
return root;
}
RBNode *predecessor(RBNode *node) {
RBNode *temp = node->lchild;
while (temp->rchild != NIL) temp = temp->rchild;
return temp;
}
RBNode *erase_maintain(RBNode *root) {
if (root->lchild->color != 2 && root->rchild->color != 2) return root;
if (root->lchild->color == 0 || root->rchild->color == 0) {
root->color = 0;
if (root->lchild->color == 2) {
root->rchild->color = 1;
root = left_rotate(root);
root->lchild = erase_maintain(root->lchild);
} else {
root->lchild->color = 1;
root = right_rotate(root);
root->rchild = erase_maintain(root->rchild);
}
return root;
}
if (root->lchild->color == 2) {
if (!hasRedchild(root->rchild)) {
root->color += 1;
root->lchild->color = 1;
root->rchild->color = 0;
return root;
} else if (root->rchild->lchild->color == 0) {
root->rchild = right_rotate(root->rchild);
root->rchild->color = 1;
root->rchild->rchild->color = 0;
}
root->lchild->color = 1;
root = left_rotate(root);
root->color = root->lchild->color;
root->lchild->color = root->rchild->color = 1;
} else {
if (!hasRedchild(root->lchild)) {
root->color += 1;
root->rchild->color = 1;
root->lchild->color = 0;
return root;
} else if (root->lchild->rchild->color == 0) {
root->lchild = left_rotate(root->lchild);
root->lchild->color = 1;
root->lchild->lchild->color = 0;
}
root->rchild->color = 1;
root = right_rotate(root);
root->color = root->rchild->color;
root->lchild->color = root->rchild->color = 1;
}
return root;
}
RBNode *__erase(RBNode *root, int key) {
if (root == NIL) return root;
if (root->key == key) {
if (root->lchild == NIL && root->rchild == NIL) {
NIL->color += root->color;
free(root);
return NIL;
} else if (root->lchild == NIL || root->rchild == NIL) {
RBNode *temp = (root->lchild != NIL) ? root->lchild : root->rchild;
temp->color += root->color;
free(root);
return temp;
} else {
RBNode *temp = predecessor(root);
root->key = temp->key;
root->lchild = __erase(root, temp->key);
}
} else {
if (root->key > key) root->lchild = __erase(root->lchild, key);
else root->rchild = __erase(root->rchild, key);
}
return erase_maintain(root);
}
RBNode *erase(RBNode *root, int key) {
root = __erase(root, key);
root->color = 1;
return root;
}
void clear(RBNode *root) {
if (root == NIL) return ;
clear(root->lchild);
clear(root->rchild);
free(root);
return ;
}
void output(RBNode *root) {
if (root == NIL) return ;
printf("(%d | %d, %d, %d)\n", root->color, root->key, root->lchild->key, root->rchild->key);
output(root->lchild);
output(root->rchild);
return ;
}
int main() {
int op, value;
RBNode *root = NIL;
while (scanf("%d%d", &op, &value) != EOF) {
switch (op) {
case 0:
printf("\ninsert %d\n", value);
root = insert(root, value); break;
case 1:
printf("\ndelete %d\n", value);
root = erase(root, value); break;
default: printf("error\n");
}
printf("----------\n");
output(root);
}
return 0;
}