Browse Source

Fixed problem where the root was allowed to be red

Cameron Weinfurt 5 months ago
parent
commit
b771400238
1 changed files with 21 additions and 1 deletions
  1. 21
    1
      tree_alloc.c

+ 21
- 1
tree_alloc.c View File

@@ -322,7 +322,15 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
322 322
 		node->left->parent = tmp;
323 323
 	}
324 324
 
325
-	if (do_repair && replace && replace->parent != NULL) {
325
+	if (replace && replace->parent == NULL) {
326
+#ifdef DEBUG
327
+      printf("=== PRE-REMOVE-FIXUP ===\n");
328
+      printf("===== CURRENT TREE =====\n");
329
+      debug_print_tree(0, *root_ptr);
330
+      printf("===== END OF TREES =====\n");
331
+#endif
332
+	  replace->color = COLOR_BLACK;
333
+	} else if (do_repair && replace) {
326 334
 #ifdef DEBUG
327 335
       printf("=== PRE-REMOVE-FIXUP ===\n");
328 336
       printf("===== CURRENT TREE =====\n");
@@ -331,6 +339,12 @@ void remove_node(TreeAlloc **root_ptr, TreeAlloc *node) {
331 339
 #endif
332 340
 		repair_after_remove(root_ptr, replace);
333 341
 	}
342
+#ifdef DEBUG
343
+      printf("=== POST-REMOVE ===\n");
344
+      printf("===== CURRENT TREE =====\n");
345
+      debug_print_tree(0, *root_ptr);
346
+      printf("===== END OF TREES =====\n");
347
+#endif
334 348
 }
335 349
 
336 350
 
@@ -345,6 +359,12 @@ void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
345 359
   *root_ptr = to_insert;
346 360
   to_insert->parent = NULL;
347 361
   to_insert->color = COLOR_BLACK;
362
+#ifdef DEBUG
363
+  printf("= POST-INSERT-SINGLETON =\n");
364
+  printf("===== CURRENT TREE =====\n");
365
+  debug_print_tree(0, *root_ptr);
366
+  printf("===== END OF TREES =====\n");
367
+#endif
348 368
 }
349 369
 
350 370
 void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {

Loading…
Cancel
Save