Browse Source

Made color checks into defines

tree_alloc
Cameron Weinfurt 1 year ago
parent
commit
2293d6aad1
  1. 60
      tree_alloc.c

60
tree_alloc.c

@ -166,9 +166,12 @@ void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
tmp->parent = parent;
}
#define IS_BLACK_NODE(n) (n == NULL || n->color == COLOR_BLACK)
#define IS_RED_NODE(n) (n != NULL && n->color == COLOR_RED)
void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
TreeAlloc *parent = ta->parent;
if (parent == NULL) {
ta->color = COLOR_BLACK;
return;
@ -176,31 +179,31 @@ void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
TreeAlloc *grandparent = parent->parent;
TreeAlloc *uncle = get_sibling(parent);
if (parent->color == COLOR_RED) {
if (uncle != NULL && uncle->color == COLOR_RED) {
if (IS_RED_NODE(parent)) {
if (IS_RED_NODE(uncle)) {
parent->color = COLOR_BLACK;
uncle->color = COLOR_BLACK;
grandparent->color = COLOR_RED;
repair_tree_after_insert(root_ptr, grandparent);
} else {
if (ta == parent->left && parent == grandparent->left) {
rotate_left(root_ptr, parent);
ta = ta->left;
parent = parent->left;
} else {
rotate_right(root_ptr, parent);
ta = ta->right;
parent = parent->right;
}
grandparent = parent->parent;
if (ta == parent->left) {
rotate_right(root_ptr, grandparent);
} else {
rotate_left(root_ptr, grandparent);
}
parent->color = COLOR_BLACK;
grandparent->color = COLOR_RED;
if (ta == parent->left && parent == grandparent->left) {
rotate_left(root_ptr, parent);
ta = ta->left;
parent = parent->left;
} else {
rotate_right(root_ptr, parent);
ta = ta->right;
parent = parent->right;
}
grandparent = parent->parent;
if (ta == parent->left) {
rotate_right(root_ptr, grandparent);
} else {
rotate_left(root_ptr, grandparent);
}
parent->color = COLOR_BLACK;
grandparent->color = COLOR_RED;
}
}
}
@ -218,24 +221,25 @@ void replace_node(TreeAlloc **root_ptr, TreeAlloc *node, TreeAlloc *replace) {
}
void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
if (node->color == COLOR_RED) {
if (IS_RED_NODE(node)) {
node->color = COLOR_BLACK;
} else {
TreeAlloc *sibling = get_sibling(node);
if (sibling != NULL && sibling->color == COLOR_RED) {
if (node->parent->left == node)
if (IS_RED_NODE(sibling)) {
if (node->parent->left == node) {
rotate_left(root_ptr, node->parent);
else
} else {
rotate_right(root_ptr, node->parent);
}
node->parent->parent->color = node->parent->color = COLOR_BLACK;
}
if (sibling->left->color == COLOR_BLACK && sibling->right->color == COLOR_BLACK) {
if (IS_BLACK_NODE(sibling->left) && IS_BLACK_NODE(sibling->right)) {
node->color = COLOR_BLACK;
sibling->color = COLOR_RED;
repair_after_remove(root_ptr, node->parent);
} else {
if (node->parent->left == node && sibling->right->color == COLOR_BLACK) {
if (node->parent->left == node && IS_BLACK_NODE(sibling)) {
rotate_right(root_ptr, sibling);
sibling = get_sibling(node);
sibling->color = COLOR_RED;
@ -243,7 +247,7 @@ void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
rotate_left(root_ptr, node->parent);
node->color = get_sibling(node->parent)->color = COLOR_BLACK;
} else if (node->parent->right == node && sibling->left->color == COLOR_BLACK) {
} else if (node->parent->right == node && IS_BLACK_NODE(sibling)) {
rotate_left(root_ptr, sibling);
sibling = get_sibling(node);
sibling->color = COLOR_RED;

Loading…
Cancel
Save