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