273
|
1 #include <stdio.h>
|
|
2
|
|
3 #include "../context.h"
|
|
4
|
|
5 extern enum Relational compare(struct Node* node1, struct Node* node2);
|
|
6
|
|
7 Tree* createRedBlackTree(struct Context* context) {
|
|
8 struct Tree* tree = new Tree();
|
|
9 struct RedBlackTree* redBlackTree = new RedBlackTree();
|
|
10 tree->tree = (union Data*)redBlackTree;
|
|
11 redBlackTree->root = NULL;
|
279
|
12 redBlackTree->nodeStack = createSingleLinkedStack(context);
|
273
|
13 tree->put = C_putRedBlackTree;
|
|
14 tree->get = C_getRedBlackTree;
|
|
15 // tree->remove = C_removeRedBlackTree;
|
|
16 // tree->clear = C_clearRedBlackTree;
|
|
17 return tree;
|
|
18 }
|
|
19
|
|
20 void printTree1(union Data* data) {
|
|
21 struct Node* node = &data->Node;
|
|
22 if (node == NULL) {
|
|
23 printf("NULL");
|
|
24 } else {
|
|
25 printf("key = %d (", node->key);
|
|
26 printTree1((union Data*)(node->right));
|
|
27 printf("), (");
|
|
28 printTree1((union Data*)(node->left));
|
|
29 printf(")");
|
|
30 }
|
|
31 }
|
|
32
|
|
33 void printTree(union Data* data) {
|
|
34 printTree1(data);
|
|
35 printf("\n");
|
|
36 }
|
|
37
|
337
|
38 __code putRedBlackTree(struct RedBlackTree* tree, struct Node* node) {
|
|
39 struct Node* newNode = &ALLOCATE(context, Node)->Node;
|
|
40 struct Node* root = tree->root;
|
|
41 printTree((union Data*)(tree->root));
|
|
42 tree->newNode = newNode;
|
|
43 tree->root = newNode; // this should done at stackClear
|
|
44 tree->parent = NULL;
|
273
|
45 if (root) {
|
337
|
46 tree->current = root;
|
|
47 tree->result = compare(tree->current, node);
|
280
|
48 goto meta(context, C_replaceNode);
|
273
|
49 }
|
280
|
50 goto meta(context, C_insertNode);
|
273
|
51 }
|
|
52
|
337
|
53 __code replaceNode(struct RedBlackTree* tree, struct Stack* nodeStack) {
|
|
54 struct Node* oldNode = tree->current;
|
|
55 struct Node* newNode = tree->newNode;
|
|
56 tree->previous = newNode;
|
|
57 *newNode = *oldNode;
|
|
58 nodeStack->stack = (union Data*)tree->nodeStack;
|
|
59 nodeStack->data = (union Data*)(newNode);
|
|
60 nodeStack->next = C_replaceNode1;
|
|
61 goto meta(context, tree->nodeStack->push);
|
273
|
62 }
|
|
63
|
337
|
64 __code replaceNode1(struct RedBlackTree* tree, struct Node* node, __code next(...)) {
|
|
65 struct Node* oldNode = tree->current;
|
|
66 struct Node* newNode = tree->previous;
|
|
67 struct Node* newnewNode = &ALLOCATE(context, Node)->Node;
|
|
68 int result = tree->result;
|
273
|
69 if (result == EQ) {
|
|
70 newNode->value = node->value;
|
|
71 // go to stack clear
|
|
72 goto meta(context, next);
|
|
73 } else if (result == GT) {
|
337
|
74 tree->current = oldNode->right;
|
273
|
75 newNode->right = newnewNode;
|
|
76 } else {
|
337
|
77 tree->current = oldNode->left;
|
273
|
78 newNode->left = newnewNode;
|
|
79 }
|
337
|
80 tree->newNode = newnewNode;
|
|
81 if (tree->current) {
|
|
82 tree->result = compare(tree->current, node);
|
280
|
83 goto meta(context, C_replaceNode);
|
273
|
84 }
|
280
|
85 goto meta(context, C_insertNode);
|
273
|
86
|
|
87 }
|
|
88
|
337
|
89 __code insertNode(struct RedBlackTree* tree, struct Stack* nodeStack, struct Node* node) {
|
|
90 struct Node* newNode = tree->newNode;
|
273
|
91 *newNode = *node;
|
|
92 newNode->color = Red;
|
337
|
93 tree->current = newNode;
|
|
94 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
95 nodeStack->next = C_insertCase1;
|
338
|
96 goto meta(context, tree->nodeStack->get2);
|
273
|
97 }
|
|
98
|
337
|
99 __code insertCase1(struct RedBlackTree* tree, struct Node *parent, struct Node *grandparent) {
|
273
|
100 if (parent != NULL) {
|
337
|
101 tree->parent = parent;
|
|
102 tree->grandparent = grandparent;
|
280
|
103 goto meta(context, C_insertCase2);
|
273
|
104 }
|
337
|
105 tree->root->color = Black;
|
280
|
106 goto meta(context, C_stackClear);
|
273
|
107 }
|
|
108
|
|
109 __code insertCase1_stub(struct Context* context) {
|
|
110 goto insertCase1(context,
|
|
111 &Gearef(context, Tree)->tree->Tree.tree->RedBlackTree,
|
|
112 &context->data[D_Stack]->Stack.data->Node,
|
|
113 &context->data[D_Stack]->Stack.data1->Node);
|
|
114 }
|
|
115
|
337
|
116 __code insertCase2(struct RedBlackTree* tree) {
|
|
117 if (tree->parent->color == Black) {
|
280
|
118 goto meta(context, C_stackClear);
|
273
|
119 }
|
280
|
120 goto meta(context, C_insertCase3);
|
273
|
121 }
|
|
122
|
337
|
123 __code insertCase3(struct RedBlackTree* tree, struct Stack* nodeStack) {
|
273
|
124 struct Node* uncle;
|
|
125
|
337
|
126 if (tree->grandparent->left == tree->parent)
|
|
127 uncle = tree->grandparent->right;
|
273
|
128 else
|
337
|
129 uncle = tree->grandparent->left;
|
273
|
130
|
|
131 if (uncle && (uncle->color == Red)) {
|
|
132 // do insertcase1 on grandparent, stack must be pop by two
|
337
|
133 tree->parent->color = Black;
|
273
|
134 uncle->color = Black;
|
337
|
135 tree->grandparent->color = Red;
|
|
136 tree->current = tree->grandparent;
|
|
137 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
138 nodeStack->next = C_insertCase1;
|
337
|
139 goto meta(context, tree->nodeStack->pop2);
|
273
|
140 }
|
280
|
141 goto meta(context, C_insertCase4);
|
273
|
142 }
|
|
143
|
337
|
144 __code insertCase4(struct RedBlackTree* tree, struct RotateTree* rotateTree) {
|
|
145 struct Stack* nodeStack = tree->nodeStack;
|
273
|
146
|
337
|
147 if ((tree->current == tree->parent->right) && (tree->parent == tree->grandparent->left)) {
|
|
148 tree->current = tree->current->left;
|
|
149 tree->parent = tree->grandparent;
|
273
|
150
|
338
|
151 rotateTree->traverse = tree;
|
273
|
152 rotateTree->next = C_insertCase5;
|
|
153
|
337
|
154 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
155 nodeStack->next = C_rotateLeft;
|
|
156 goto meta(context, nodeStack->pop);
|
337
|
157 } else if ((tree->current == tree->parent->left) && (tree->parent == tree->grandparent->right)) {
|
|
158 tree->parent = tree->grandparent;
|
|
159 tree->current = tree->current->right;
|
273
|
160
|
338
|
161 rotateTree->traverse = tree;
|
273
|
162 rotateTree->next = C_insertCase5;
|
|
163
|
337
|
164 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
165 nodeStack->next = C_rotateRight;
|
|
166 goto meta(context, nodeStack->pop);
|
|
167 }
|
|
168
|
280
|
169 goto meta(context, C_insertCase5);
|
273
|
170 }
|
|
171
|
338
|
172 __code insertCase5(struct RedBlackTree* tree, struct Stack* nodeStack) {
|
|
173 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
174 nodeStack->next = C_insertCase51;
|
338
|
175 goto meta(context, tree->nodeStack->pop2);
|
273
|
176 }
|
|
177
|
338
|
178 __code insertCase51(struct RedBlackTree* tree, struct RotateTree* rotateTree, struct Node* parent, struct Node* grandparent) {
|
|
179 struct Node* current = tree->current;
|
|
180 tree->parent = parent;
|
|
181 tree->grandparent = grandparent;
|
273
|
182
|
|
183 parent->color = Black;
|
|
184 grandparent->color = Red;
|
|
185
|
338
|
186 tree->current = grandparent;
|
273
|
187
|
338
|
188 rotateTree->traverse = tree;
|
273
|
189 rotateTree->next = C_stackClear;
|
|
190
|
|
191 if ((current == parent->left) && (parent == grandparent->left))
|
280
|
192 goto meta(context, C_rotateRight);
|
273
|
193 else
|
280
|
194 goto meta(context, C_rotateLeft);
|
273
|
195 }
|
|
196
|
|
197 __code insertCase51_stub(struct Context* context) {
|
|
198 struct Node* parent = &context->data[D_Stack]->Stack.data->Node;
|
|
199 struct Node* grandparent = &context->data[D_Stack]->Stack.data1->Node;
|
|
200 goto insertCase51(context,
|
|
201 &Gearef(context, Tree)->tree->Tree.tree->RedBlackTree,
|
|
202 Gearef(context, RotateTree),
|
|
203 parent,
|
|
204 grandparent);
|
|
205 }
|
|
206
|
338
|
207 __code rotateLeft(struct RedBlackTree* tree, struct Stack* nodeStack) {
|
|
208 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
209 nodeStack->next = C_rotateLeft1;
|
338
|
210 goto meta(context, tree->nodeStack->get);
|
273
|
211 }
|
|
212
|
|
213 __code rotateLeft_stub(struct Context* context) {
|
|
214 struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
|
|
215 goto rotateLeft(context, traverse, Gearef(context, Stack));
|
|
216 }
|
338
|
217
|
|
218 __code rotateLeft1(struct Node* node, struct RedBlackTree* tree, struct Node* parent, struct RotateTree* rotateTree) {
|
273
|
219 struct Node* tmp = node->right;
|
|
220
|
|
221 if (parent) {
|
|
222 if (node == parent->left)
|
|
223 parent->left = tmp;
|
|
224 else
|
|
225 parent->right = tmp;
|
|
226 } else {
|
338
|
227 tree->root = tmp;
|
273
|
228 }
|
|
229
|
|
230 node->right = tmp->left;
|
|
231 tmp->left = node;
|
338
|
232 tree->current = tmp;
|
273
|
233
|
|
234 goto meta(context, rotateTree->next);
|
|
235 }
|
338
|
236
|
273
|
237 __code rotateLeft1_stub(struct Context* context) {
|
|
238 struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
|
|
239 struct Node* parent = &context->data[D_Stack]->Stack.data->Node;
|
|
240 goto rotateLeft1(context,
|
|
241 traverse->current,
|
|
242 traverse,
|
|
243 parent,
|
|
244 Gearef(context, RotateTree));
|
|
245 }
|
|
246
|
338
|
247 __code rotateRight(struct RedBlackTree* tree, struct Stack* nodeStack) {
|
|
248 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
249 nodeStack->next = C_rotateRight1;
|
338
|
250 goto meta(context, tree->nodeStack->get);
|
273
|
251 }
|
|
252
|
|
253 __code rotateRight_stub(struct Context* context) {
|
|
254 struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
|
|
255 goto rotateLeft(context, traverse, Gearef(context, Stack));
|
|
256 }
|
|
257
|
|
258 __code rotateRight1(struct Node* node, struct RedBlackTree* traverse,struct Node *parent,struct RotateTree *rotateTree) {
|
|
259 struct Node* tmp = node->left;
|
|
260
|
|
261 if (parent) {
|
|
262 if (node == parent->left)
|
|
263 parent->left = tmp;
|
|
264 else
|
|
265 parent->right = tmp;
|
|
266 } else {
|
|
267 traverse->root = tmp;
|
|
268 }
|
|
269
|
|
270 node->left = tmp->right;
|
|
271 tmp->right = node;
|
|
272 traverse->current = tmp;
|
|
273
|
|
274 goto meta(context, rotateTree->next);
|
|
275 }
|
|
276
|
|
277 __code rotateRight1_stub(struct Context* context) {
|
|
278 struct RedBlackTree* traverse = context->data[D_RotateTree]->RotateTree.traverse;
|
|
279 struct Node* parent = &context->data[D_Stack]->Stack.data->Node;
|
|
280 goto rotateRight1(context,
|
|
281 traverse->current,
|
|
282 traverse,
|
|
283 parent,
|
|
284 Gearef(context, RotateTree));
|
|
285 }
|
|
286
|
338
|
287 __code stackClear(struct RedBlackTree* tree, struct Stack* nodeStack, __code next(...)) {
|
|
288 tree->current = 0;
|
|
289 nodeStack->stack = (union Data*)tree->nodeStack;
|
273
|
290 nodeStack->next = next;
|
338
|
291 goto meta(context, tree->nodeStack->clear);
|
273
|
292 }
|
|
293
|
338
|
294 __code getRedBlackTree(struct RedBlackTree* tree, __code next(...)) {
|
|
295 if (tree->root) {
|
|
296 tree->current = tree->root;
|
273
|
297
|
280
|
298 goto meta(context, C_search);
|
273
|
299 }
|
|
300
|
|
301 goto next(...);
|
|
302 }
|
|
303
|
338
|
304 __code search(struct RedBlackTree* tree, struct Node* node, __code next(...)) {
|
273
|
305 // compare(context, traverse, traverse->current->key, node->key);
|
338
|
306 tree->result = compare(tree->current, node);
|
|
307 if (tree->result == EQ) {
|
|
308 *node = *tree->current;
|
273
|
309
|
|
310 goto meta(context, next);
|
338
|
311 } else if (tree->result == GT) {
|
|
312 tree->current = tree->current->right;
|
273
|
313 } else {
|
338
|
314 tree->current = tree->current->left;
|
273
|
315 }
|
|
316
|
338
|
317 if (tree->current)
|
280
|
318 goto meta(context, C_search);
|
273
|
319
|
|
320 goto next(...);
|
|
321 }
|
|
322
|
|
323 /* /\* __code delete(struct Context* context, struct Tree* tree) { *\/ */
|
|
324 /* /\* if (tree->root) { *\/ */
|
|
325 /* /\* stack_push(context->code_stack, &context->next); *\/ */
|
|
326 /* /\* context->next = Delete1; *\/ */
|
|
327 /* /\* goto meta(context, Get); *\/ */
|
|
328 /* /\* } *\/ */
|
|
329
|
|
330 /* /\* goto meta(context, context->next); *\/ */
|
|
331 /* /\* } *\/ */
|
|
332
|
|
333 /* /\* __code delete_stub(struct Context* context) { *\/ */
|
|
334 /* /\* goto delete(context, &context->data[Tree]->tree); *\/ */
|
|
335 /* /\* } *\/ */
|
|
336
|
|
337 /* /\* __code delete1(struct Context* context, struct Tree* tree, struct Allocate* allocate) { *\/ */
|
|
338 /* /\* allocate->size = sizeof(struct Node); *\/ */
|
|
339 /* /\* allocator(context); *\/ */
|
|
340
|
|
341 /* /\* struct Node* root = tree->root; *\/ */
|
|
342
|
|
343 /* /\* tree->root = &context->data[context->dataNum]->node; *\/ */
|
|
344 /* /\* tree->current = root; *\/ */
|
|
345
|
|
346 /* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */
|
|
347
|
|
348 /* /\* goto meta(context, Replace_d1); *\/ */
|
|
349 /* /\* } *\/ */
|
|
350
|
|
351 /* /\* __code delete1_stub(struct Context* context) { *\/ */
|
|
352 /* /\* goto delete1(context, &context->data[Tree]->tree, &context->data[Allocate]->allocate); *\/ */
|
|
353 /* /\* } *\/ */
|
|
354
|
|
355 /* /\* __code delete2(struct Context* context, struct Node* current) { *\/ */
|
|
356 /* /\* if (current->color == Black) { *\/ */
|
|
357 /* /\* struct Node* child = current->right == NULL ? current->left : current->right; *\/ */
|
|
358 /* /\* current->color = child == NULL ? Black : child->color; *\/ */
|
|
359
|
|
360 /* /\* goto meta(context, DeleteCase1); *\/ */
|
|
361 /* /\* } *\/ */
|
|
362
|
|
363 /* /\* goto meta(context, Delete3); *\/ */
|
|
364 /* /\* } *\/ */
|
|
365
|
|
366 /* /\* __code delete2_stub(struct Context* context) { *\/ */
|
|
367 /* /\* goto delete2(context, context->data[Tree]->tree.current); *\/ */
|
|
368 /* /\* } *\/ */
|
|
369
|
|
370 /* /\* __code delete3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */
|
|
371 /* /\* struct Node* tmp = current->right == NULL ? current->left : current->right; *\/ */
|
|
372
|
|
373 /* /\* if (current->parent) { *\/ */
|
|
374 /* /\* if (current == current->parent->left) *\/ */
|
|
375 /* /\* current->parent->left = tmp; *\/ */
|
|
376 /* /\* else *\/ */
|
|
377 /* /\* current->parent->right = tmp; *\/ */
|
|
378 /* /\* } else { *\/ */
|
|
379 /* /\* tree->root = tmp; *\/ */
|
|
380 /* /\* } *\/ */
|
|
381
|
|
382 /* /\* if (tmp) *\/ */
|
|
383 /* /\* tmp->parent = current->parent; *\/ */
|
|
384
|
|
385 /* /\* if (current->parent == NULL && tmp) *\/ */
|
|
386 /* /\* tmp->color = Black; *\/ */
|
|
387
|
|
388 /* /\* current == current->parent->left ? (current->parent->left = NULL) : (current->parent->right = NULL); *\/ */
|
|
389
|
|
390 /* /\* stack_pop(context->code_stack, &context->next); *\/ */
|
|
391 /* /\* goto meta(context, context->next); *\/ */
|
|
392 /* /\* } *\/ */
|
|
393
|
|
394 /* /\* __code delete3_stub(struct Context* context) { *\/ */
|
|
395 /* /\* goto delete3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */
|
|
396 /* /\* } *\/ */
|
|
397
|
|
398 /* /\* __code replaceNodeForDelete1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode, int result) { *\/ */
|
|
399 /* /\* *newNode = *oldNode; *\/ */
|
|
400
|
|
401 /* /\* if (result == EQ) *\/ */
|
|
402 /* /\* goto meta(context, Replace_d2); *\/ */
|
|
403 /* /\* else if (result == GT) *\/ */
|
|
404 /* /\* tree->current = newNode->right; *\/ */
|
|
405 /* /\* else *\/ */
|
|
406 /* /\* tree->current = newNode->left; *\/ */
|
|
407
|
|
408 /* /\* tree->current->parent = newNode; *\/ */
|
|
409
|
|
410 /* /\* if (tree->current->left == NULL && tree->current->right == NULL) *\/ */
|
|
411 /* /\* goto meta(context, Delete2); *\/ */
|
|
412
|
|
413 /* /\* if (result == GT) *\/ */
|
|
414 /* /\* newNode->right = context->heap; *\/ */
|
|
415 /* /\* else if (result == LT) *\/ */
|
|
416 /* /\* newNode->left = context->heap; *\/ */
|
|
417
|
|
418 /* /\* allocator(context); *\/ */
|
|
419
|
|
420 /* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */
|
|
421
|
|
422 /* /\* goto meta(context, Replace_d1); *\/ */
|
|
423 /* /\* } *\/ */
|
|
424
|
|
425 /* /\* __code replaceNodeForDelete1_stub(struct Context* context) { *\/ */
|
|
426 /* /\* goto replaceNodeForDelete1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); *\/ */
|
|
427 /* /\* } *\/ */
|
|
428
|
|
429 /* /\* __code replaceNodeForDelete2(struct Context* context, struct Tree* tree, struct Node* newNode) { *\/ */
|
|
430 /* /\* if (tree->current->left && tree->current->right) { *\/ */
|
|
431 /* /\* newNode->left->parent = newNode; *\/ */
|
|
432 /* /\* tree->current = newNode->left; *\/ */
|
|
433 /* /\* newNode->left = context->heap; *\/ */
|
|
434 /* /\* tree->deleted = newNode; *\/ */
|
|
435
|
|
436 /* /\* allocator(context); *\/ */
|
|
437 /* /\* tree->current->parent = newNode; *\/ */
|
|
438
|
|
439 /* /\* goto meta(context, FindMax1); *\/ */
|
|
440 /* /\* } *\/ */
|
|
441
|
|
442 /* /\* goto meta(context, Delete2); *\/ */
|
|
443 /* /\* } *\/ */
|
|
444
|
|
445 /* /\* __code replaceNodeForDelete2_stub(struct Context* context) { *\/ */
|
|
446 /* /\* goto replaceNodeForDelete2(context, &context->data[Tree]->tree, &context->data[context->dataNum]->node); *\/ */
|
|
447 /* /\* } *\/ */
|
|
448
|
|
449 /* /\* __code findMax1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */
|
|
450 /* /\* *newNode = *oldNode; *\/ */
|
|
451
|
|
452 /* /\* if (newNode->right) *\/ */
|
|
453 /* /\* goto meta(context, FindMax2); *\/ */
|
|
454
|
|
455 /* /\* tree->deleted->key = newNode->key; *\/ */
|
|
456 /* /\* tree->deleted->value = newNode->value; *\/ */
|
|
457
|
|
458 /* /\* tree->current = newNode; *\/ */
|
|
459
|
|
460 /* /\* goto meta(context, Delete2); *\/ */
|
|
461 /* /\* } *\/ */
|
|
462
|
|
463 /* /\* __code findMax1_stub(struct Context* context) { *\/ */
|
|
464 /* /\* goto findMax1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */
|
|
465 /* /\* } *\/ */
|
|
466
|
|
467
|
|
468 /* /\* __code findMax2(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */
|
|
469 /* /\* *newNode = *oldNode; *\/ */
|
|
470
|
|
471 /* /\* if (newNode->right->right) { *\/ */
|
|
472 /* /\* tree->current = newNode->right; *\/ */
|
|
473 /* /\* newNode->right = context->heap; *\/ */
|
|
474
|
|
475 /* /\* allocator(context); *\/ */
|
|
476 /* /\* tree->current->parent = newNode; *\/ */
|
|
477
|
|
478 /* /\* goto meta(context, FindMax2); *\/ */
|
|
479 /* /\* } *\/ */
|
|
480
|
|
481 /* /\* tree->deleted->key = newNode->right->key; *\/ */
|
|
482 /* /\* tree->deleted->value = newNode->right->value; *\/ */
|
|
483
|
|
484 /* /\* tree->current = newNode; *\/ */
|
|
485
|
|
486 /* /\* goto meta(context, Delete2); *\/ */
|
|
487 /* /\* } *\/ */
|
|
488
|
|
489 /* /\* __code findMax2_stub(struct Context* context) { *\/ */
|
|
490 /* /\* goto findMax2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */
|
|
491 /* /\* } *\/ */
|
|
492
|
|
493 /* /\* __code deleteCase1(struct Context* context, struct Node* current) { *\/ */
|
|
494 /* /\* if (current->parent) *\/ */
|
|
495 /* /\* goto meta(context, DeleteCase2); *\/ */
|
|
496
|
|
497 /* /\* goto meta(context, Delete3); *\/ */
|
|
498 /* /\* } *\/ */
|
|
499
|
|
500 /* /\* __code deleteCase1_stub(struct Context* context) { *\/ */
|
|
501 /* /\* goto deleteCase1(context, context->data[Tree]->tree.current); *\/ */
|
|
502 /* /\* } *\/ */
|
|
503
|
|
504 /* /\* __code deleteCase2(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */
|
|
505 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */
|
|
506
|
|
507 /* /\* if ((sibling == NULL ? Black : sibling->color) == Red) { *\/ */
|
|
508 /* /\* current->parent->color = Red; *\/ */
|
|
509 /* /\* sibling->color = Black; *\/ */
|
|
510
|
|
511 /* /\* current == current->parent->left ? (current->parent->left = context->heap) : (current->parent->right = context->heap); *\/ */
|
|
512 /* /\* allocator(context); *\/ */
|
|
513 /* /\* context->data[context->dataNum]->node = *sibling; *\/ */
|
|
514
|
|
515 /* /\* tree->current = current->parent; *\/ */
|
|
516
|
|
517 /* /\* context->next = DeleteCase3; *\/ */
|
|
518 /* /\* stack_push(context->code_stack, &context->next); *\/ */
|
|
519
|
|
520 /* /\* if (current == current->parent->left) *\/ */
|
|
521 /* /\* goto meta(context, RotateL); *\/ */
|
|
522 /* /\* else *\/ */
|
|
523 /* /\* goto meta(context, RotateR); *\/ */
|
|
524 /* /\* } *\/ */
|
|
525
|
|
526 /* /\* goto meta(context, DeleteCase3); *\/ */
|
|
527 /* /\* } *\/ */
|
|
528
|
|
529 /* /\* __code deleteCase2_stub(struct Context* context) { *\/ */
|
|
530 /* /\* goto deleteCase2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */
|
|
531 /* /\* } *\/ */
|
|
532
|
|
533 /* /\* __code deleteCase3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */
|
|
534 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */
|
|
535
|
|
536 /* /\* if (current->parent->color == Black && *\/ */
|
|
537 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */
|
|
538 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */
|
|
539 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */
|
|
540 /* /\* sibling->color = Red; *\/ */
|
|
541
|
|
542 /* /\* tree->current = current->parent; *\/ */
|
|
543 /* /\* goto meta(context, DeleteCase1); *\/ */
|
|
544 /* /\* } *\/ */
|
|
545
|
|
546 /* /\* goto meta(context, DeleteCase4); *\/ */
|
|
547 /* /\* } *\/ */
|
|
548
|
|
549 /* /\* __code deleteCase3_stub(struct Context* context) { *\/ */
|
|
550 /* /\* goto deleteCase3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */
|
|
551 /* /\* } *\/ */
|
|
552
|
|
553 /* /\* __code deleteCase4(struct Context* context, struct Node* current) { *\/ */
|
|
554 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */
|
|
555
|
|
556 /* /\* if (current->parent->color == Red && *\/ */
|
|
557 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */
|
|
558 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */
|
|
559 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */
|
|
560 /* /\* sibling->color = Red; *\/ */
|
|
561 /* /\* current->parent->color = Black; *\/ */
|
|
562
|
|
563 /* /\* goto meta(context, Delete3); *\/ */
|
|
564 /* /\* } *\/ */
|
|
565
|
|
566 /* /\* goto meta(context, DeleteCase5); *\/ */
|
|
567 /* /\* } *\/ */
|
|
568
|
|
569 /* /\* __code deleteCase4_stub(struct Context* context) { *\/ */
|
|
570 /* /\* goto deleteCase4(context, context->data[Tree]->tree.current); *\/ */
|
|
571 /* /\* } *\/ */
|
|
572
|
|
573 /* /\* __code deleteCase5(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */
|
|
574 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */
|
|
575 /* /\* sibling->parent = current->parent; *\/ */
|
|
576
|
|
577 /* /\* if (current == current->parent->left && *\/ */
|
|
578 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */
|
|
579 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Red && *\/ */
|
|
580 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */
|
|
581 /* /\* sibling->color = Red; *\/ */
|
|
582 /* /\* sibling->left->color = Black; *\/ */
|
|
583
|
|
584 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */
|
|
585 /* /\* allocator(context); *\/ */
|
|
586 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */
|
|
587 /* /\* *tmp = *sibling; *\/ */
|
|
588 /* /\* tmp->parent = current; *\/ */
|
|
589
|
|
590 /* /\* tmp->left = context->heap; *\/ */
|
|
591 /* /\* allocator(context); *\/ */
|
|
592 /* /\* context->data[context->dataNum]->node = *sibling->left; *\/ */
|
|
593 /* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */
|
|
594
|
|
595 /* /\* tree->current = tmp; *\/ */
|
|
596
|
|
597 /* /\* context->next = DeleteCase6; *\/ */
|
|
598 /* /\* stack_push(context->code_stack, &context->next); *\/ */
|
|
599
|
|
600 /* /\* goto meta(context, RotateR); *\/ */
|
|
601 /* /\* } else if (current == current->parent->right && *\/ */
|
|
602 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */
|
|
603 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */
|
|
604 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Red) { *\/ */
|
|
605 /* /\* sibling->color = Red; *\/ */
|
|
606 /* /\* sibling->right->color = Black; *\/ */
|
|
607
|
|
608 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */
|
|
609 /* /\* allocator(context); *\/ */
|
|
610 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */
|
|
611 /* /\* *tmp = *sibling; *\/ */
|
|
612 /* /\* tmp->parent = current; *\/ */
|
|
613
|
|
614 /* /\* tmp->right = context->heap; *\/ */
|
|
615 /* /\* allocator(context); *\/ */
|
|
616 /* /\* context->data[context->dataNum]->node = *sibling->right; *\/ */
|
|
617 /* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */
|
|
618
|
|
619 /* /\* tree->current = tmp; *\/ */
|
|
620
|
|
621 /* /\* context->next = DeleteCase6; *\/ */
|
|
622 /* /\* stack_push(context->code_stack, &context->next); *\/ */
|
|
623 /* /\* goto meta(context, RotateL); *\/ */
|
|
624 /* /\* } *\/ */
|
|
625
|
|
626 /* /\* goto meta(context, DeleteCase6); *\/ */
|
|
627 /* /\* } *\/ */
|
|
628
|
|
629 /* /\* __code deleteCase5_stub(struct Context* context) { *\/ */
|
|
630 /* /\* goto deleteCase5(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */
|
|
631 /* /\* } *\/ */
|
|
632
|
|
633 /* /\* __code deleteCase6(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */
|
|
634 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */
|
|
635
|
|
636 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */
|
|
637 /* /\* allocator(context); *\/ */
|
|
638 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */
|
|
639 /* /\* *tmp = *sibling; *\/ */
|
|
640 /* /\* tmp->parent = current; *\/ */
|
|
641
|
|
642 /* /\* tmp->color = current->parent->color; *\/ */
|
|
643 /* /\* current->parent->color = Black; *\/ */
|
|
644
|
|
645 /* /\* context->next = Delete3; *\/ */
|
|
646 /* /\* stack_push(context->code_stack, &context->next); *\/ */
|
|
647
|
|
648 /* /\* if (current == current->parent->left) { *\/ */
|
|
649 /* /\* tmp->right->color = Black; *\/ */
|
|
650 /* /\* tree->current = current->parent; *\/ */
|
|
651
|
|
652 /* /\* goto meta(context, RotateL); *\/ */
|
|
653 /* /\* } else { *\/ */
|
|
654 /* /\* tmp->left->color = Black; *\/ */
|
|
655 /* /\* tree->current = current->parent; *\/ */
|
|
656
|
|
657 /* /\* goto meta(context, RotateR); *\/ */
|
|
658 /* /\* } *\/ */
|
|
659 /* /\* } *\/ */
|
|
660
|
|
661 /* /\* __code deleteCase6_stub(struct Context* context) { *\/ */
|
|
662 /* /\* goto deleteCase6(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */
|
|
663 /* /\* } *\/ */
|