Showing
2 changed files
with
4 additions
and
142 deletions
| @@ -104,97 +104,9 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -104,97 +104,9 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 104 | * rebalancing process...(this does not make much sense, but | 104 | * rebalancing process...(this does not make much sense, but |
| 105 | * to be honest I don't know now.) | 105 | * to be honest I don't know now.) |
| 106 | */ | 106 | */ |
| 107 | - while(1) { | ||
| 108 | - TR_Tree sibling; | ||
| 109 | - | ||
| 110 | - // case 1 | ||
| 111 | - if (NULL == TR_TREE_PARENT(node)) { | ||
| 112 | - break; | ||
| 113 | - } | ||
| 114 | - | ||
| 115 | - sibling = TR_TREE_SIBLING(node); | ||
| 116 | - if (NULL != sibling && rbRed == sibling->color) { | ||
| 117 | - // case 2 | ||
| 118 | - node->parent->color = rbRed; | ||
| 119 | - sibling->color = rbBlack; | ||
| 120 | - | ||
| 121 | - if (NULL != node->parent->right && node != node->parent->right) { | ||
| 122 | - TR_TREE_ROTATE(left, this, node->parent); | ||
| 123 | - } else { | ||
| 124 | - TR_TREE_ROTATE(right, this, node->parent); | ||
| 125 | - } | ||
| 126 | - sibling = TR_TREE_SIBLING(node); | ||
| 127 | - } | ||
| 128 | - | ||
| 129 | - if (NULL == sibling | ||
| 130 | - || (rbBlack == sibling->color | ||
| 131 | - && TR_TREE_NODE_BLACK(sibling->left) | ||
| 132 | - && TR_TREE_NODE_BLACK(sibling->right))) { | ||
| 133 | - // case 3/4 | ||
| 134 | - if (NULL != sibling) { | ||
| 135 | - sibling->color = rbRed; | ||
| 136 | - } | ||
| 137 | - | ||
| 138 | - if (rbBlack == node->parent->color) { | ||
| 139 | - // case 3 | ||
| 140 | - node = node->parent; | ||
| 141 | - continue; | ||
| 142 | - } else { | ||
| 143 | - // case 4 | ||
| 144 | - node->parent->color = rbBlack; | ||
| 145 | - break; | ||
| 146 | - } | ||
| 147 | - } | ||
| 148 | - | ||
| 149 | - // case 5 | ||
| 150 | - if (NULL != sibling && rbBlack == sibling->color) { | ||
| 151 | - | ||
| 152 | - if (node == node->parent->left | ||
| 153 | - && TR_TREE_NODE_BLACK(sibling->right) | ||
| 154 | - && TR_TREE_NODE_STRICT_RED(sibling->left)) { | ||
| 155 | - | ||
| 156 | - sibling->color = rbRed; | ||
| 157 | - sibling->left->color = rbBlack; | ||
| 158 | - | ||
| 159 | - TR_TREE_ROTATE(right, this, sibling); | ||
| 160 | - | ||
| 161 | - } else if (node == node->parent->right | ||
| 162 | - && TR_TREE_NODE_BLACK(sibling->left) | ||
| 163 | - && TR_TREE_NODE_STRICT_RED(sibling->right)) { | ||
| 164 | - | ||
| 165 | - sibling->color = rbRed; | ||
| 166 | - sibling->right->color = rbBlack; | ||
| 167 | - | ||
| 168 | - TR_TREE_ROTATE(left, this, sibling); | ||
| 169 | - } | ||
| 170 | - sibling = TR_TREE_SIBLING(node); | ||
| 171 | - } | ||
| 172 | - | ||
| 173 | - // case 6 | ||
| 174 | - // do we need to reinitialize sibling here? | ||
| 175 | - if (NULL != sibling) { | ||
| 176 | - sibling->color = node->parent->color; | ||
| 177 | - } | ||
| 178 | - | ||
| 179 | - if (NULL != node && NULL != node->parent) { | ||
| 180 | - node->parent->color = rbBlack; | ||
| 181 | - | ||
| 182 | - if (NULL != node->parent->right | ||
| 183 | - && node != node->parent->right) { | ||
| 184 | - | ||
| 185 | - if (NULL != sibling->right) { | ||
| 186 | - sibling->right->color = rbBlack; | ||
| 187 | - } | ||
| 188 | - TR_TREE_ROTATE(left, this, node->parent); | ||
| 189 | - } else { | ||
| 190 | - if (NULL != sibling->left) { | ||
| 191 | - sibling->left->color = rbBlack; | ||
| 192 | - } | ||
| 193 | - TR_TREE_ROTATE(right, this, node->parent); | ||
| 194 | - } | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - break; | 107 | + { |
| 108 | + TR_Tree sibling = TR_TREE_SIBLING(node); | ||
| 109 | + TR_TREE_BALANCE_DELETE(this, node, sibling); | ||
| 198 | } | 110 | } |
| 199 | 111 | ||
| 200 | TR_delete(del_node); | 112 | TR_delete(del_node); |
| @@ -69,57 +69,7 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -69,57 +69,7 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 69 | * we expect node not to be NULL and pointing to our | 69 | * we expect node not to be NULL and pointing to our |
| 70 | * new node at this point...now rabalance the tree | 70 | * new node at this point...now rabalance the tree |
| 71 | */ | 71 | */ |
| 72 | - while (1) { | ||
| 73 | - // case 1 | ||
| 74 | - if (NULL == TR_TREE_PARENT(node)) { | ||
| 75 | - node->color = rbBlack; | ||
| 76 | - break; | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - // case 2 | ||
| 80 | - if (rbBlack == node->parent->color) { | ||
| 81 | - break; | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - // case 3 | ||
| 85 | - TR_Tree uncle = TR_TREE_UNCLE(node); | ||
| 86 | - | ||
| 87 | - if (NULL != uncle && rbRed == uncle->color) { | ||
| 88 | - node->parent->color = rbBlack; | ||
| 89 | - uncle->color = rbBlack; | ||
| 90 | - node->parent->parent->color = rbRed; | ||
| 91 | - | ||
| 92 | - node = node->parent->parent; | ||
| 93 | - continue; | ||
| 94 | - } | ||
| 95 | - | ||
| 96 | - // case 4 | ||
| 97 | - if (node == node->parent->right | ||
| 98 | - && node->parent == node->parent->parent->left) { | ||
| 99 | - | ||
| 100 | - TR_TREE_ROTATE(left, this, node->parent); | ||
| 101 | - node = node->left; | ||
| 102 | - | ||
| 103 | - } else if (node == node->parent->left | ||
| 104 | - && node->parent == node->parent->parent->right) { | ||
| 105 | - | ||
| 106 | - TR_TREE_ROTATE(right, this, node->parent); | ||
| 107 | - node = node->right; | ||
| 108 | - | ||
| 109 | - } | ||
| 110 | - | ||
| 111 | - // case 5 | ||
| 112 | - node->parent->color = rbBlack; | ||
| 113 | - node->parent->parent->color = rbRed; | ||
| 114 | - | ||
| 115 | - if (node == node->parent->left) { | ||
| 116 | - TR_TREE_ROTATE(right, this, node->parent->parent); | ||
| 117 | - } else { | ||
| 118 | - TR_TREE_ROTATE(left, this, node->parent->parent); | ||
| 119 | - } | ||
| 120 | - | ||
| 121 | - break; | ||
| 122 | - } | 72 | + TR_TREE_BALANCE_INSERT(this, node); |
| 123 | 73 | ||
| 124 | return new_node->data; | 74 | return new_node->data; |
| 125 | } | 75 | } |
Please
register
or
login
to post a comment