Commit 895e789181141deb58ddec2dd56956869e7c716c
1 parent
21fc0713
started some optimizations, but with not much effect for now
Showing
7 changed files
with
129 additions
and
313 deletions
| @@ -25,19 +25,13 @@ | @@ -25,19 +25,13 @@ | ||
| 25 | 25 | ||
| 26 | #include "trbase.h" | 26 | #include "trbase.h" |
| 27 | 27 | ||
| 28 | -#define TR_TREE_RIGHT(node) (NULL!=(node)?(node)->right:NULL) | 28 | +#define TR_TREE_RIGHT(node) (NULL!=(node)?(node)->right:NULL) |
| 29 | #define TR_TREE_LEFT(node) (NULL!=(node)?(node)->left:NULL) | 29 | #define TR_TREE_LEFT(node) (NULL!=(node)?(node)->left:NULL) |
| 30 | #define TR_TREE_PARENT(node) (NULL!=(node)?(node)->parent:NULL) | 30 | #define TR_TREE_PARENT(node) (NULL!=(node)?(node)->parent:NULL) |
| 31 | 31 | ||
| 32 | #define TR_TREE_CHILD(node) \ | 32 | #define TR_TREE_CHILD(node) \ |
| 33 | (NULL==TR_TREE_RIGHT((node))?TR_TREE_LEFT((node)):TR_TREE_RIGHT((node))) | 33 | (NULL==TR_TREE_RIGHT((node))?TR_TREE_LEFT((node)):TR_TREE_RIGHT((node))) |
| 34 | 34 | ||
| 35 | -#define TR_TREE_RIGHT_LEFT(node) \ | ||
| 36 | - (NULL!=TR_TREE_RIGHT((node))?TR_TREE_LEFT(TR_TREE_RIGHT((node))):NULL) | ||
| 37 | - | ||
| 38 | -#define TR_TREE_LEFT_RIGHT(node) \ | ||
| 39 | - (NULL!=TR_TREE_LEFT((node))?TR_TREE_RIGHT(TR_TREE_LEFT((node))):NULL) | ||
| 40 | - | ||
| 41 | #define TR_TREE_SIBLING(node) \ | 35 | #define TR_TREE_SIBLING(node) \ |
| 42 | (NULL!=TR_TREE_PARENT((node))? \ | 36 | (NULL!=TR_TREE_PARENT((node))? \ |
| 43 | ((node)==TR_TREE_PARENT((node))->left? \ | 37 | ((node)==TR_TREE_PARENT((node))->left? \ |
| @@ -45,74 +39,67 @@ | @@ -45,74 +39,67 @@ | ||
| 45 | TR_TREE_PARENT((node))->left): \ | 39 | TR_TREE_PARENT((node))->left): \ |
| 46 | NULL) | 40 | NULL) |
| 47 | 41 | ||
| 48 | -#define TR_TREE_GRANDPARENT(node) \ | ||
| 49 | - (NULL!=TR_TREE_PARENT((node))?TR_TREE_PARENT((node))->parent:NULL) | ||
| 50 | - | ||
| 51 | -#define TR_TREE_UNCLE(node) \ | ||
| 52 | - (NULL!=TR_TREE_GRANDPARENT((node))? \ | ||
| 53 | - (TR_TREE_PARENT((node))==TR_TREE_GRANDPARENT((node))->left? \ | ||
| 54 | - TR_TREE_GRANDPARENT((node))->right: \ | ||
| 55 | - TR_TREE_GRANDPARENT((node))->left): \ | ||
| 56 | - NULL) | 42 | +#define TR_TREE_GRANDPARENT(node) (TR_TREE_PARENT((node))->parent) |
| 43 | + | ||
| 44 | +#define TR_TREE_UNCLE(node) \ | ||
| 45 | + ((node)->parent == (node)->parent->parent->left? \ | ||
| 46 | + (node)->parent->parent->right: \ | ||
| 47 | + (node)->parent->parent->left) | ||
| 48 | + | ||
| 49 | +#define TR_TREE_REPLACE_NODE(root, node1, node2) \ | ||
| 50 | + if (NULL != (node1)->parent) { \ | ||
| 51 | + if ((node1) == (node1)->parent->left) { \ | ||
| 52 | + (node1)->parent->left = (node2); \ | ||
| 53 | + } else { \ | ||
| 54 | + (node1)->parent->right = (node2); \ | ||
| 55 | + } \ | ||
| 56 | + } else { \ | ||
| 57 | + *(root) = (node2); \ | ||
| 58 | + } \ | ||
| 59 | + if (NULL != (node2)) { \ | ||
| 60 | + (node2)->parent = (node1)->parent; \ | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | +#define TR_TREE_ROT_RELCHILD_right(node) ((node)->left) | ||
| 64 | +#define TR_TREE_ROT_RELCHILD_CHILD_right (relChild->right) | ||
| 65 | +#define TR_TREE_ROT_RELCHILD_left(node) ((node)->right) | ||
| 66 | +#define TR_TREE_ROT_RELCHILD_CHILD_left (relChild->left) | ||
| 67 | + | ||
| 68 | +#define TR_TREE_ROTATE(lr, _this, node) \ | ||
| 69 | + if (NULL != (node)) { \ | ||
| 70 | + TR_Tree _node = (node); \ | ||
| 71 | + TR_Tree relChild = TR_TREE_ROT_RELCHILD_##lr(_node); \ | ||
| 72 | + TR_Tree relChildLvl2 = TR_TREE_ROT_RELCHILD_##lr(_node)->lr; \ | ||
| 73 | + relChild->lr = _node; \ | ||
| 74 | + relChild->parent = _node->parent; \ | ||
| 75 | + TR_TREE_ROT_RELCHILD_##lr(_node) = relChildLvl2; \ | ||
| 76 | + if (NULL != relChildLvl2) { \ | ||
| 77 | + relChildLvl2->parent = _node; \ | ||
| 78 | + } \ | ||
| 79 | + if (NULL != _node->parent) { \ | ||
| 80 | + if (_node->parent->left == _node) { \ | ||
| 81 | + _node->parent->left = relChild; \ | ||
| 82 | + } else { \ | ||
| 83 | + _node->parent->right = relChild; \ | ||
| 84 | + } \ | ||
| 85 | + } else { \ | ||
| 86 | + *(_this) = relChild; \ | ||
| 87 | + } \ | ||
| 88 | + _node->parent = relChild; \ | ||
| 89 | + } | ||
| 57 | 90 | ||
| 58 | -#define TR_TREE_ROTATE_LEFT(root, node) \ | ||
| 59 | - do { \ | ||
| 60 | - if (NULL != TR_TREE_RIGHT_LEFT((node))) { \ | ||
| 61 | - TR_TREE_RIGHT_LEFT((node))->parent = (node); \ | ||
| 62 | - } \ | ||
| 63 | - TR_TREE_RIGHT((node))->left = (node); \ | ||
| 64 | - if (NULL != TR_TREE_PARENT((node))) { \ | ||
| 65 | - if (TR_TREE_PARENT((node))->left==(node)) { \ | ||
| 66 | - TR_TREE_PARENT((node))->left = (node)->right; \ | ||
| 67 | - } else { \ | ||
| 68 | - TR_TREE_PARENT((node))->right = (node)->right; \ | ||
| 69 | - } \ | ||
| 70 | - } else { \ | ||
| 71 | - *(root) = (node)->right; \ | ||
| 72 | - } \ | ||
| 73 | - (node)->right = TR_TREE_RIGHT_LEFT((node)); \ | ||
| 74 | - (node)->parent = (node)->right; \ | ||
| 75 | - TR_TREE_RIGHT((node))->parent = (node)->parent; \ | ||
| 76 | - } while(0) | ||
| 77 | - | ||
| 78 | -#define TR_TREE_ROTATE_RIGHT(root, node) \ | ||
| 79 | - do { \ | ||
| 80 | - if (NULL != TR_TREE_LEFT_RIGHT((node))) { \ | ||
| 81 | - TR_TREE_LEFT_RIGHT((node))->parent = (node); \ | ||
| 82 | - } \ | ||
| 83 | - TR_TREE_LEFT((node))->right = (node); \ | ||
| 84 | - if (NULL != TR_TREE_PARENT((node))) { \ | ||
| 85 | - if (TR_TREE_PARENT((node))->left==(node)) { \ | ||
| 86 | - TR_TREE_PARENT((node))->left = (node)->left; \ | ||
| 87 | - } else { \ | ||
| 88 | - TR_TREE_PARENT((node))->right = (node)->left; \ | ||
| 89 | - } \ | ||
| 90 | - } else { \ | ||
| 91 | - *(root) = (node)->left; \ | ||
| 92 | - } \ | ||
| 93 | - TR_TREE_LEFT((node))->parent = (node)->parent; \ | ||
| 94 | - (node)->left = TR_TREE_LEFT_RIGHT((node)); \ | ||
| 95 | - (node)->parent = (node)->right; \ | ||
| 96 | - } while(0) | ||
| 97 | - | ||
| 98 | -#define TR_TREE_REPLACE_NODE(root, node1, node2) \ | ||
| 99 | - do { \ | ||
| 100 | - if (NULL != TR_TREE_PARENT((node1))) { \ | ||
| 101 | - if ((node1) == TR_TREE_PARENT((node1))->left) { \ | ||
| 102 | - TR_TREE_PARENT((node1))->left = (node2); \ | ||
| 103 | - } else { \ | ||
| 104 | - TR_TREE_PARENT((node1))->right = (node2); \ | ||
| 105 | - } \ | ||
| 106 | - } else { \ | ||
| 107 | - *(root) = (node2); \ | ||
| 108 | - } \ | ||
| 109 | - if (NULL != (node2)) { \ | ||
| 110 | - (node2)->parent = (node1)->parent; \ | ||
| 111 | - } \ | ||
| 112 | - } while(0) | 91 | +typedef enum {rbBlack=1, rbRed=2} TR_rbColor; |
| 113 | 92 | ||
| 93 | +#define TR_TREE_NODE_BLACK(node) (NULL == (node) || rbBlack == (node)->color) | ||
| 94 | +#define TR_TREE_NODE_RED(node) (NULL == (node) || rbRed == (node)->color) | ||
| 95 | +#define TR_TREE_NODE_STRICT_BLACK(node) (NULL != (node) && rbBlack == (node)->color) | ||
| 96 | +#define TR_TREE_NODE_STRICT_RED(node) (NULL != (node) && rbRed == (node)->color) | ||
| 114 | 97 | ||
| 115 | -typedef enum {rbBlack=1, rbRed=2} TR_rbColor; | 98 | +#define TR_TREE_INORDER_SUCC(node, succ) \ |
| 99 | + succ = TR_TREE_RIGHT((node)); \ | ||
| 100 | + while (NULL != succ->left) { \ | ||
| 101 | + succ = succ->left; \ | ||
| 102 | + } | ||
| 116 | 103 | ||
| 117 | TR_CLASS(TR_Tree) { | 104 | TR_CLASS(TR_Tree) { |
| 118 | void * data; | 105 | void * data; |
| 1 | ACLOCAL_AMFLAGS = -I m4 | 1 | ACLOCAL_AMFLAGS = -I m4 |
| 2 | AUTOMAKE_OPTIONS = subdir-objects | 2 | AUTOMAKE_OPTIONS = subdir-objects |
| 3 | 3 | ||
| 4 | -TREE = tree.c find.c insert.c inOrderSuccessor.c delete.c walk.c \ | ||
| 5 | - rotateLeft.c rotateRight.c destroy.c | 4 | +TREE = tree.c \ |
| 5 | + find.c \ | ||
| 6 | + insert.c \ | ||
| 7 | + inOrderSuccessor.c \ | ||
| 8 | + delete.c walk.c \ | ||
| 9 | + destroy.c | ||
| 6 | 10 | ||
| 7 | AM_CFLAGS += -I../../include/ | 11 | AM_CFLAGS += -I../../include/ |
| 8 | 12 |
| @@ -23,10 +23,6 @@ | @@ -23,10 +23,6 @@ | ||
| 23 | #include "trbase.h" | 23 | #include "trbase.h" |
| 24 | #include "tr/tree.h" | 24 | #include "tr/tree.h" |
| 25 | 25 | ||
| 26 | -TR_Tree TR_inOrderSuccessor(TR_Tree); | ||
| 27 | -void TR_treeRotateLeft(TR_Tree *, TR_Tree); | ||
| 28 | -void TR_treeRotateRight(TR_Tree *, TR_Tree); | ||
| 29 | - | ||
| 30 | void * | 26 | void * |
| 31 | TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) | 27 | TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) |
| 32 | { | 28 | { |
| @@ -79,7 +75,9 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -79,7 +75,9 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 79 | * out inOrderSuccessor and remove the inOrderSuccessor. | 75 | * out inOrderSuccessor and remove the inOrderSuccessor. |
| 80 | */ | 76 | */ |
| 81 | if (NULL != TR_TREE_LEFT(node) && NULL != TR_TREE_RIGHT(node)) { | 77 | if (NULL != TR_TREE_LEFT(node) && NULL != TR_TREE_RIGHT(node)) { |
| 82 | - TR_Tree successor = TR_inOrderSuccessor(node); | 78 | + TR_Tree successor; |
| 79 | + | ||
| 80 | + TR_TREE_INORDER_SUCC(node, successor); | ||
| 83 | 81 | ||
| 84 | node->data = successor->data; | 82 | node->data = successor->data; |
| 85 | node = successor; | 83 | node = successor; |
| @@ -126,113 +124,91 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -126,113 +124,91 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 126 | * to be honest I don't know now.) | 124 | * to be honest I don't know now.) |
| 127 | */ | 125 | */ |
| 128 | while(1) { | 126 | while(1) { |
| 127 | + TR_Tree sibling; | ||
| 128 | + | ||
| 129 | // case 1 | 129 | // case 1 |
| 130 | if (NULL == TR_TREE_PARENT(node)) { | 130 | if (NULL == TR_TREE_PARENT(node)) { |
| 131 | break; | 131 | break; |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | - // case 2 | ||
| 135 | - if (NULL != TR_TREE_SIBLING(node) | ||
| 136 | - && rbRed == TR_TREE_SIBLING(node)->color) { | ||
| 137 | - | ||
| 138 | - TR_TREE_PARENT(node)->color = rbRed; | ||
| 139 | - TR_TREE_SIBLING(node)->color = rbBlack; | ||
| 140 | - | ||
| 141 | - if (NULL != TR_TREE_PARENT(node)->right && | ||
| 142 | - node != TR_TREE_PARENT(node)->right) { | ||
| 143 | - | ||
| 144 | - //TREE_ROTATE_LEFT(this, TREE_PARENT(node)); | ||
| 145 | - TR_treeRotateLeft(this, TR_TREE_PARENT(node)); | 134 | + sibling = TR_TREE_SIBLING(node); |
| 135 | + if (NULL != sibling && rbRed == sibling->color) { | ||
| 136 | + // case 2 | ||
| 137 | + node->parent->color = rbRed; | ||
| 138 | + sibling->color = rbBlack; | ||
| 146 | 139 | ||
| 140 | + if (NULL != node->parent->right && node != node->parent->right) { | ||
| 141 | + TR_TREE_ROTATE(left, this, node->parent); | ||
| 147 | } else { | 142 | } else { |
| 148 | - | ||
| 149 | - //TREE_ROTATE_RIGHT(this, TREE_PARENT(node)); | ||
| 150 | - TR_treeRotateRight(this, TR_TREE_PARENT(node)); | ||
| 151 | - | 143 | + TR_TREE_ROTATE(right, this, node->parent); |
| 152 | } | 144 | } |
| 153 | - } | ||
| 154 | - | ||
| 155 | - // case 3 / 4 | ||
| 156 | - if (NULL == TR_TREE_SIBLING(node) | ||
| 157 | - || (rbBlack == TR_TREE_SIBLING(node)->color | ||
| 158 | - && (NULL == TR_TREE_SIBLING(node)->left | ||
| 159 | - || rbBlack == TR_TREE_SIBLING(node)->left->color) | ||
| 160 | - && (NULL == TR_TREE_SIBLING(node)->right | ||
| 161 | - || rbBlack == TR_TREE_SIBLING(node)->right->color))) { | ||
| 162 | - | ||
| 163 | - if (NULL != TR_TREE_SIBLING(node)) { | ||
| 164 | - TR_TREE_SIBLING(node)->color = rbRed; | 145 | + } else { |
| 146 | + // case 3/4 | ||
| 147 | + if (NULL != sibling | ||
| 148 | + && TR_TREE_NODE_BLACK(sibling->left) | ||
| 149 | + && TR_TREE_NODE_BLACK(sibling->right)) { | ||
| 150 | + sibling->color = rbRed; | ||
| 165 | } | 151 | } |
| 166 | 152 | ||
| 167 | - | ||
| 168 | /* | 153 | /* |
| 169 | * this is the point where during the balancing our tree | 154 | * this is the point where during the balancing our tree |
| 170 | * node can be finally deleted. | 155 | * node can be finally deleted. |
| 171 | */ | 156 | */ |
| 172 | - if (rbBlack == TR_TREE_PARENT(node)->color) { | 157 | + if (rbBlack == node->parent->color) { |
| 173 | // case 3 | 158 | // case 3 |
| 174 | - TR_Tree parent = node->parent; | ||
| 175 | - node = parent; | 159 | + node = node->parent; |
| 176 | continue; | 160 | continue; |
| 177 | } else { | 161 | } else { |
| 178 | // case 4 | 162 | // case 4 |
| 179 | - TR_TREE_PARENT(node)->color = rbBlack; | 163 | + node->parent->color = rbBlack; |
| 180 | break; | 164 | break; |
| 181 | } | 165 | } |
| 182 | } | 166 | } |
| 183 | 167 | ||
| 184 | // case 5 | 168 | // case 5 |
| 185 | - if (NULL != TR_TREE_SIBLING(node) | ||
| 186 | - && rbBlack == TR_TREE_SIBLING(node)->color) { | 169 | + if (NULL != sibling && rbBlack == sibling->color) { |
| 187 | 170 | ||
| 188 | - if (node == TR_TREE_PARENT(node)->left | ||
| 189 | - && (NULL == TR_TREE_SIBLING(node)->right | ||
| 190 | - || rbBlack == TR_TREE_SIBLING(node)->right->color) | ||
| 191 | - && (NULL != TR_TREE_SIBLING(node)->left | ||
| 192 | - && rbRed == TR_TREE_SIBLING(node)->left->color)) { | 171 | + if (node == node->parent->left |
| 172 | + && TR_TREE_NODE_BLACK(sibling->right) | ||
| 173 | + && TR_TREE_NODE_STRICT_RED(sibling->left)) { | ||
| 193 | 174 | ||
| 194 | - TR_TREE_SIBLING(node)->color = rbRed; | ||
| 195 | - TR_TREE_SIBLING(node)->left->color = rbBlack; | 175 | + sibling->color = rbRed; |
| 176 | + sibling->left->color = rbBlack; | ||
| 196 | 177 | ||
| 197 | - //TREE_ROTATE_RIGHT(this, TREE_SIBLING(node)); | ||
| 198 | - TR_treeRotateRight(this, TR_TREE_SIBLING(node)); | 178 | + TR_TREE_ROTATE(right, this, sibling); |
| 199 | 179 | ||
| 200 | - } else if (node == TR_TREE_PARENT(node)->right | ||
| 201 | - && (NULL == TR_TREE_SIBLING(node)->left | ||
| 202 | - || rbBlack == TR_TREE_SIBLING(node)->left->color) | ||
| 203 | - && (NULL != TR_TREE_SIBLING(node)->right | ||
| 204 | - && rbRed == TR_TREE_SIBLING(node)->right->color)) { | 180 | + } else if (node == node->parent->right |
| 181 | + && TR_TREE_NODE_BLACK(sibling->left) | ||
| 182 | + && TR_TREE_NODE_STRICT_RED(sibling->right)) { | ||
| 205 | 183 | ||
| 206 | - TR_TREE_SIBLING(node)->color = rbRed; | ||
| 207 | - TR_TREE_SIBLING(node)->right->color = rbBlack; | 184 | + sibling->color = rbRed; |
| 185 | + sibling->right->color = rbBlack; | ||
| 208 | 186 | ||
| 209 | - //TREE_ROTATE_LEFT(this, TREE_SIBLING(node)); | ||
| 210 | - TR_treeRotateLeft(this, TR_TREE_SIBLING(node)); | 187 | + TR_TREE_ROTATE(left, this, sibling); |
| 211 | } | 188 | } |
| 212 | } | 189 | } |
| 213 | 190 | ||
| 214 | // case 6 | 191 | // case 6 |
| 215 | - if (NULL != TR_TREE_SIBLING(node)) { | ||
| 216 | - TR_TREE_SIBLING(node)->color = TR_TREE_PARENT(node)->color; | 192 | + // do we need to reinitialize sibling here? |
| 193 | + if (NULL != sibling) { | ||
| 194 | + sibling->color = node->parent->color; | ||
| 217 | } | 195 | } |
| 218 | 196 | ||
| 219 | - if (NULL != node && NULL != TR_TREE_PARENT(node)) { | ||
| 220 | - TR_TREE_PARENT(node)->color = rbBlack; | 197 | + if (NULL != node && NULL != node->parent) { |
| 198 | + node->parent->color = rbBlack; | ||
| 221 | 199 | ||
| 222 | - if (NULL != TR_TREE_PARENT(node)->right | ||
| 223 | - && node != TR_TREE_PARENT(node)->right) { | 200 | + if (NULL != node->parent->right |
| 201 | + && node != node->parent->right) { | ||
| 224 | 202 | ||
| 225 | - if (NULL != TR_TREE_SIBLING(node)->right) { | ||
| 226 | - TR_TREE_SIBLING(node)->right->color = rbBlack; | 203 | + if (NULL != sibling->right) { |
| 204 | + sibling->right->color = rbBlack; | ||
| 227 | } | 205 | } |
| 228 | - //TREE_ROTATE_LEFT(this, TREE_PARENT(node)); | ||
| 229 | - TR_treeRotateLeft(this, TR_TREE_PARENT(node)); | 206 | + TR_TREE_ROTATE(left, this, node->parent); |
| 230 | } else { | 207 | } else { |
| 231 | - if (NULL != TR_TREE_SIBLING(node)->left) { | ||
| 232 | - TR_TREE_SIBLING(node)->left->color = rbBlack; | 208 | + if (NULL != sibling->left) { |
| 209 | + sibling->left->color = rbBlack; | ||
| 233 | } | 210 | } |
| 234 | - //TREE_ROTATE_RIGHT(this, TREE_PARENT(node)); | ||
| 235 | - TR_treeRotateRight(this, TR_TREE_PARENT(node)); | 211 | + TR_TREE_ROTATE(right, this, node->parent); |
| 236 | } | 212 | } |
| 237 | } | 213 | } |
| 238 | 214 |
src/tree/inOrderSuccessor.c
deleted
100644 → 0
| 1 | -/** | ||
| 2 | - * \file | ||
| 3 | - * | ||
| 4 | - * \author Georg Hopp | ||
| 5 | - * | ||
| 6 | - * \copyright | ||
| 7 | - * Copyright © 2012 Georg Hopp | ||
| 8 | - * | ||
| 9 | - * This program is free software: you can redistribute it and/or modify | ||
| 10 | - * it under the terms of the GNU General Public License as published by | ||
| 11 | - * the Free Software Foundation, either version 3 of the License, or | ||
| 12 | - * (at your option) any later version. | ||
| 13 | - * | ||
| 14 | - * This program is distributed in the hope that it will be useful, | ||
| 15 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | - * GNU General Public License for more details. | ||
| 18 | - * | ||
| 19 | - * You should have received a copy of the GNU General Public License | ||
| 20 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | - */ | ||
| 22 | - | ||
| 23 | -#include "tr/tree.h" | ||
| 24 | - | ||
| 25 | -TR_Tree | ||
| 26 | -TR_inOrderSuccessor(TR_Tree this) | ||
| 27 | -{ | ||
| 28 | - this = TR_TREE_RIGHT(this); | ||
| 29 | - | ||
| 30 | - while (NULL != TR_TREE_LEFT(this)) { | ||
| 31 | - this = TR_TREE_LEFT(this); | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - return this; | ||
| 35 | -} | ||
| 36 | - | ||
| 37 | -// vim: set ts=4 sw=4: |
| @@ -23,9 +23,6 @@ | @@ -23,9 +23,6 @@ | ||
| 23 | #include "trbase.h" | 23 | #include "trbase.h" |
| 24 | #include "tr/tree.h" | 24 | #include "tr/tree.h" |
| 25 | 25 | ||
| 26 | -void TR_treeRotateLeft(TR_Tree *, TR_Tree); | ||
| 27 | -void TR_treeRotateRight(TR_Tree *, TR_Tree); | ||
| 28 | - | ||
| 29 | void * | 26 | void * |
| 30 | TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | 27 | TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) |
| 31 | { | 28 | { |
| @@ -47,16 +44,17 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -47,16 +44,17 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 47 | * first search for it and if its found return the data | 44 | * first search for it and if its found return the data |
| 48 | * and we are done... | 45 | * and we are done... |
| 49 | */ | 46 | */ |
| 50 | - int comparison; | ||
| 51 | - | ||
| 52 | while (NULL != node) { | 47 | while (NULL != node) { |
| 53 | - comparison = comp(node->data, search); | 48 | + int comparison = comp(node->data, search); |
| 54 | 49 | ||
| 55 | if (0 < comparison) { | 50 | if (0 < comparison) { |
| 56 | if (NULL != TR_TREE_LEFT(node)) { | 51 | if (NULL != TR_TREE_LEFT(node)) { |
| 57 | node = TR_TREE_LEFT(node); | 52 | node = TR_TREE_LEFT(node); |
| 58 | continue; | 53 | continue; |
| 59 | } else { | 54 | } else { |
| 55 | + node->left = TR_new(TR_Tree, search); | ||
| 56 | + node->left->parent = node; | ||
| 57 | + node = new_node = node->left; | ||
| 60 | break; | 58 | break; |
| 61 | } | 59 | } |
| 62 | } | 60 | } |
| @@ -66,26 +64,20 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -66,26 +64,20 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 66 | node = TR_TREE_RIGHT(node); | 64 | node = TR_TREE_RIGHT(node); |
| 67 | continue; | 65 | continue; |
| 68 | } else { | 66 | } else { |
| 67 | + node->right = TR_new(TR_Tree, search); | ||
| 68 | + node->right->parent = node; | ||
| 69 | + node = new_node = node->right; | ||
| 69 | break; | 70 | break; |
| 70 | } | 71 | } |
| 71 | } | 72 | } |
| 72 | 73 | ||
| 73 | if (0 == comparison) { | 74 | if (0 == comparison) { |
| 74 | - return node->data; | 75 | + // as this is insert and not find this will overwrite an existing value, |
| 76 | + // but return the previos one so that it can be freed if neccessary. | ||
| 77 | + void * data = node->data; | ||
| 78 | + node->data = (void *)search; | ||
| 79 | + return data; | ||
| 75 | } | 80 | } |
| 76 | - } | ||
| 77 | - | ||
| 78 | - /* | ||
| 79 | - * as we have not found it now add a new element. | ||
| 80 | - */ | ||
| 81 | - if (0 < comparison) { | ||
| 82 | - node->left = TR_new(TR_Tree, search); | ||
| 83 | - TR_TREE_LEFT(node)->parent = node; | ||
| 84 | - node = new_node = TR_TREE_LEFT(node); | ||
| 85 | - } else { | ||
| 86 | - node->right = TR_new(TR_Tree, search); | ||
| 87 | - TR_TREE_RIGHT(node)->parent = node; | ||
| 88 | - node = new_node = TR_TREE_RIGHT(node); | ||
| 89 | } | 81 | } |
| 90 | } | 82 | } |
| 91 | 83 | ||
| @@ -119,15 +111,13 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -119,15 +111,13 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 119 | if (node == TR_TREE_PARENT(node)->right | 111 | if (node == TR_TREE_PARENT(node)->right |
| 120 | && TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->left) { | 112 | && TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->left) { |
| 121 | 113 | ||
| 122 | - //TREE_ROTATE_LEFT(this, TREE_PARENT(node)); | ||
| 123 | - TR_treeRotateLeft(this, TR_TREE_PARENT(node)); | 114 | + TR_TREE_ROTATE(left, this, TR_TREE_PARENT(node)); |
| 124 | node = TR_TREE_LEFT(node); | 115 | node = TR_TREE_LEFT(node); |
| 125 | 116 | ||
| 126 | } else if (node == TR_TREE_PARENT(node)->left | 117 | } else if (node == TR_TREE_PARENT(node)->left |
| 127 | && TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->right) { | 118 | && TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->right) { |
| 128 | 119 | ||
| 129 | - //TREE_ROTATE_RIGHT(this, TREE_PARENT(node)); | ||
| 130 | - TR_treeRotateRight(this, TR_TREE_PARENT(node)); | 120 | + TR_TREE_ROTATE(right, this, TR_TREE_PARENT(node)); |
| 131 | node = TR_TREE_RIGHT(node); | 121 | node = TR_TREE_RIGHT(node); |
| 132 | 122 | ||
| 133 | } | 123 | } |
| @@ -137,11 +127,9 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -137,11 +127,9 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
| 137 | TR_TREE_GRANDPARENT(node)->color = rbRed; | 127 | TR_TREE_GRANDPARENT(node)->color = rbRed; |
| 138 | 128 | ||
| 139 | if (node == TR_TREE_PARENT(node)->left) { | 129 | if (node == TR_TREE_PARENT(node)->left) { |
| 140 | - //TREE_ROTATE_RIGHT(this, TREE_GRANDPARENT(node)); | ||
| 141 | - TR_treeRotateRight(this, TR_TREE_GRANDPARENT(node)); | 130 | + TR_TREE_ROTATE(right, this, TR_TREE_GRANDPARENT(node)); |
| 142 | } else { | 131 | } else { |
| 143 | - //TREE_ROTATE_LEFT(this, TREE_GRANDPARENT(node)); | ||
| 144 | - TR_treeRotateLeft(this, TR_TREE_GRANDPARENT(node)); | 132 | + TR_TREE_ROTATE(left, this, TR_TREE_GRANDPARENT(node)); |
| 145 | } | 133 | } |
| 146 | 134 | ||
| 147 | break; | 135 | break; |
src/tree/rotateLeft.c
deleted
100644 → 0
| 1 | -/** | ||
| 2 | - * \file | ||
| 3 | - * | ||
| 4 | - * \author Georg Hopp | ||
| 5 | - * | ||
| 6 | - * \copyright | ||
| 7 | - * Copyright © 2012 Georg Hopp | ||
| 8 | - * | ||
| 9 | - * This program is free software: you can redistribute it and/or modify | ||
| 10 | - * it under the terms of the GNU General Public License as published by | ||
| 11 | - * the Free Software Foundation, either version 3 of the License, or | ||
| 12 | - * (at your option) any later version. | ||
| 13 | - * | ||
| 14 | - * This program is distributed in the hope that it will be useful, | ||
| 15 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | - * GNU General Public License for more details. | ||
| 18 | - * | ||
| 19 | - * You should have received a copy of the GNU General Public License | ||
| 20 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | - */ | ||
| 22 | - | ||
| 23 | -#include "tr/tree.h" | ||
| 24 | - | ||
| 25 | -void | ||
| 26 | -TR_treeRotateLeft(TR_Tree * this, TR_Tree node) | ||
| 27 | -{ | ||
| 28 | - TR_Tree rightChild = TR_TREE_RIGHT(node); | ||
| 29 | - TR_Tree rcLeftSub = TR_TREE_RIGHT_LEFT(node); | ||
| 30 | - | ||
| 31 | - rightChild->left = node; | ||
| 32 | - rightChild->parent = TR_TREE_PARENT(node); | ||
| 33 | - node->right = rcLeftSub; | ||
| 34 | - if (NULL != rcLeftSub) { | ||
| 35 | - rcLeftSub->parent = node; | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - if (NULL != TR_TREE_PARENT(node)) { | ||
| 39 | - if (TR_TREE_PARENT(node)->left == node) { | ||
| 40 | - TR_TREE_PARENT(node)->left = rightChild; | ||
| 41 | - } else { | ||
| 42 | - TR_TREE_PARENT(node)->right = rightChild; | ||
| 43 | - } | ||
| 44 | - } else { | ||
| 45 | - *this = rightChild; | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - node->parent = rightChild; | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | -// vim: set ts=4 sw=4: |
src/tree/rotateRight.c
deleted
100644 → 0
| 1 | -/** | ||
| 2 | - * \file | ||
| 3 | - * | ||
| 4 | - * \author Georg Hopp | ||
| 5 | - * | ||
| 6 | - * \copyright | ||
| 7 | - * Copyright © 2012 Georg Hopp | ||
| 8 | - * | ||
| 9 | - * This program is free software: you can redistribute it and/or modify | ||
| 10 | - * it under the terms of the GNU General Public License as published by | ||
| 11 | - * the Free Software Foundation, either version 3 of the License, or | ||
| 12 | - * (at your option) any later version. | ||
| 13 | - * | ||
| 14 | - * This program is distributed in the hope that it will be useful, | ||
| 15 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | - * GNU General Public License for more details. | ||
| 18 | - * | ||
| 19 | - * You should have received a copy of the GNU General Public License | ||
| 20 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | - */ | ||
| 22 | - | ||
| 23 | -#include "tr/tree.h" | ||
| 24 | - | ||
| 25 | -void | ||
| 26 | -TR_treeRotateRight(TR_Tree * this, TR_Tree node) | ||
| 27 | -{ | ||
| 28 | - TR_Tree leftChild = TR_TREE_LEFT(node); | ||
| 29 | - TR_Tree lcRightSub = TR_TREE_LEFT_RIGHT(node); | ||
| 30 | - | ||
| 31 | - leftChild->right = node; | ||
| 32 | - leftChild->parent = TR_TREE_PARENT(node); | ||
| 33 | - node->left = lcRightSub; | ||
| 34 | - if (NULL != lcRightSub) { | ||
| 35 | - lcRightSub->parent = node; | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - if (NULL != TR_TREE_PARENT(node)) { | ||
| 39 | - if (TR_TREE_PARENT(node)->left == node) { | ||
| 40 | - TR_TREE_PARENT(node)->left = leftChild; | ||
| 41 | - } else { | ||
| 42 | - TR_TREE_PARENT(node)->right = leftChild; | ||
| 43 | - } | ||
| 44 | - } else { | ||
| 45 | - *this = leftChild; | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - node->parent = leftChild; | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | -// vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment