Browse Source

Fixed broken insert algorithm

tree_alloc
Thomas Johnson 1 year ago
parent
commit
8b31636906
  1. 38
      tree_alloc.c

38
tree_alloc.c

@ -256,14 +256,17 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
#endif
if (after->right != NULL) {
after->right->parent = to_insert;
to_insert->right = after->right;
}
after->right = to_insert;
to_insert->parent = after;
to_insert->color = COLOR_RED;
repair_tree_after_insert(root_ptr, to_insert);
if (after->right != NULL) {
after = after->right;
while (after->left != NULL) {
after = after->left;
}
after->left = to_insert;
to_insert->parent = after;
} else {
after->right = to_insert;
to_insert->parent = after;
}
#ifdef DEBUG
printf("== POST-INSERT-FIXUP ===\n");
printf("===== CURRENT TREE =====\n");
@ -281,14 +284,17 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
debug_print_tree(0, *root_ptr);
printf("===== END OF TREES =====\n");
#endif
if (before->left != NULL) {
before->left->parent = to_insert;
to_insert->left = before->left;
}
before->left = to_insert;
to_insert->parent = before;
to_insert->color = COLOR_RED;
repair_tree_after_insert(root_ptr, to_insert);
if (before->left != NULL) {
before = before->left;
while (before->right != NULL) {
before = before->right;
}
before->right = to_insert;
to_insert->parent = before;
} else {
before->left = to_insert;
to_insert->parent = before;
}
#ifdef DEBUG
printf("== POST-INSERT-FIXUP ===\n");
printf("===== CURRENT TREE =====\n");

Loading…
Cancel
Save