Browse Source

Fixed broken insert algorithm

Thomas Johnson 5 months ago
parent
commit
8b31636906
1 changed files with 22 additions and 16 deletions
  1. 22
    16
      tree_alloc.c

+ 22
- 16
tree_alloc.c View File

@@ -256,14 +256,17 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
256 256
 	debug_print_tree(0, *root_ptr);
257 257
 	printf("===== END OF TREES =====\n");
258 258
 #endif
259
-	if (after->right != NULL) {
260
-		after->right->parent = to_insert;
261
-		to_insert->right = after->right;
262
-	}
263
-	after->right = to_insert;
264
-	to_insert->parent = after;
265
-	to_insert->color = COLOR_RED;
266
-	repair_tree_after_insert(root_ptr, to_insert);
259
+  if (after->right != NULL) {
260
+    after = after->right;
261
+    while (after->left != NULL) {
262
+      after = after->left;
263
+    }
264
+    after->left = to_insert;
265
+    to_insert->parent = after;
266
+  } else {
267
+    after->right = to_insert;
268
+    to_insert->parent = after;
269
+  }
267 270
 #ifdef DEBUG
268 271
 	printf("== POST-INSERT-FIXUP ===\n");
269 272
 	printf("===== CURRENT TREE =====\n");
@@ -281,14 +284,17 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
281 284
 	debug_print_tree(0, *root_ptr);
282 285
 	printf("===== END OF TREES =====\n");
283 286
 #endif
284
-	if (before->left != NULL) {
285
-		before->left->parent = to_insert;
286
-		to_insert->left = before->left;
287
-	}
288
-	before->left = to_insert;
289
-	to_insert->parent = before;
290
-	to_insert->color = COLOR_RED;
291
-	repair_tree_after_insert(root_ptr, to_insert);
287
+  if (before->left != NULL) {
288
+    before = before->left;
289
+    while (before->right != NULL) {
290
+      before = before->right;
291
+    }
292
+    before->right = to_insert;
293
+    to_insert->parent = before;
294
+  } else {
295
+    before->left = to_insert;
296
+    to_insert->parent = before;
297
+  }
292 298
 #ifdef DEBUG
293 299
 	printf("== POST-INSERT-FIXUP ===\n");
294 300
 	printf("===== CURRENT TREE =====\n");

Loading…
Cancel
Save