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