From 99ce2633f51d0bbb61ce30be8ed0479d67503e6a Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 24 Jun 2014 15:48:57 +0100 Subject: [PATCH] use rebalance macros --- src/tree/delete.c | 94 ++--------------------------------------------- src/tree/insert.c | 52 +------------------------- 2 files changed, 4 insertions(+), 142 deletions(-) diff --git a/src/tree/delete.c b/src/tree/delete.c index 77ad384..aa2de16 100644 --- a/src/tree/delete.c +++ b/src/tree/delete.c @@ -104,97 +104,9 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) * rebalancing process...(this does not make much sense, but * to be honest I don't know now.) */ - while(1) { - TR_Tree sibling; - - // case 1 - if (NULL == TR_TREE_PARENT(node)) { - break; - } - - sibling = TR_TREE_SIBLING(node); - if (NULL != sibling && rbRed == sibling->color) { - // case 2 - node->parent->color = rbRed; - sibling->color = rbBlack; - - if (NULL != node->parent->right && node != node->parent->right) { - TR_TREE_ROTATE(left, this, node->parent); - } else { - TR_TREE_ROTATE(right, this, node->parent); - } - sibling = TR_TREE_SIBLING(node); - } - - if (NULL == sibling - || (rbBlack == sibling->color - && TR_TREE_NODE_BLACK(sibling->left) - && TR_TREE_NODE_BLACK(sibling->right))) { - // case 3/4 - if (NULL != sibling) { - sibling->color = rbRed; - } - - if (rbBlack == node->parent->color) { - // case 3 - node = node->parent; - continue; - } else { - // case 4 - node->parent->color = rbBlack; - break; - } - } - - // case 5 - if (NULL != sibling && rbBlack == sibling->color) { - - if (node == node->parent->left - && TR_TREE_NODE_BLACK(sibling->right) - && TR_TREE_NODE_STRICT_RED(sibling->left)) { - - sibling->color = rbRed; - sibling->left->color = rbBlack; - - TR_TREE_ROTATE(right, this, sibling); - - } else if (node == node->parent->right - && TR_TREE_NODE_BLACK(sibling->left) - && TR_TREE_NODE_STRICT_RED(sibling->right)) { - - sibling->color = rbRed; - sibling->right->color = rbBlack; - - TR_TREE_ROTATE(left, this, sibling); - } - sibling = TR_TREE_SIBLING(node); - } - - // case 6 - // do we need to reinitialize sibling here? - if (NULL != sibling) { - sibling->color = node->parent->color; - } - - if (NULL != node && NULL != node->parent) { - node->parent->color = rbBlack; - - if (NULL != node->parent->right - && node != node->parent->right) { - - if (NULL != sibling->right) { - sibling->right->color = rbBlack; - } - TR_TREE_ROTATE(left, this, node->parent); - } else { - if (NULL != sibling->left) { - sibling->left->color = rbBlack; - } - TR_TREE_ROTATE(right, this, node->parent); - } - } - - break; + { + TR_Tree sibling = TR_TREE_SIBLING(node); + TR_TREE_BALANCE_DELETE(this, node, sibling); } TR_delete(del_node); diff --git a/src/tree/insert.c b/src/tree/insert.c index 1ad85a7..18e0787 100644 --- a/src/tree/insert.c +++ b/src/tree/insert.c @@ -69,57 +69,7 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) * we expect node not to be NULL and pointing to our * new node at this point...now rabalance the tree */ - while (1) { - // case 1 - if (NULL == TR_TREE_PARENT(node)) { - node->color = rbBlack; - break; - } - - // case 2 - if (rbBlack == node->parent->color) { - break; - } - - // case 3 - TR_Tree uncle = TR_TREE_UNCLE(node); - - if (NULL != uncle && rbRed == uncle->color) { - node->parent->color = rbBlack; - uncle->color = rbBlack; - node->parent->parent->color = rbRed; - - node = node->parent->parent; - continue; - } - - // case 4 - if (node == node->parent->right - && node->parent == node->parent->parent->left) { - - TR_TREE_ROTATE(left, this, node->parent); - node = node->left; - - } else if (node == node->parent->left - && node->parent == node->parent->parent->right) { - - TR_TREE_ROTATE(right, this, node->parent); - node = node->right; - - } - - // case 5 - node->parent->color = rbBlack; - node->parent->parent->color = rbRed; - - if (node == node->parent->left) { - TR_TREE_ROTATE(right, this, node->parent->parent); - } else { - TR_TREE_ROTATE(left, this, node->parent->parent); - } - - break; - } + TR_TREE_BALANCE_INSERT(this, node); return new_node->data; }