Commit 3266c80841d1bff2ea3638b8133dcd24efe7d444
1 parent
895e7891
some more optimizations by preventing NULL checks
Showing
3 changed files
with
24 additions
and
22 deletions
@@ -70,8 +70,8 @@ | @@ -70,8 +70,8 @@ | ||
70 | TR_Tree _node = (node); \ | 70 | TR_Tree _node = (node); \ |
71 | TR_Tree relChild = TR_TREE_ROT_RELCHILD_##lr(_node); \ | 71 | TR_Tree relChild = TR_TREE_ROT_RELCHILD_##lr(_node); \ |
72 | TR_Tree relChildLvl2 = TR_TREE_ROT_RELCHILD_##lr(_node)->lr; \ | 72 | TR_Tree relChildLvl2 = TR_TREE_ROT_RELCHILD_##lr(_node)->lr; \ |
73 | - relChild->lr = _node; \ | ||
74 | - relChild->parent = _node->parent; \ | 73 | + relChild->lr = _node; \ |
74 | + relChild->parent = _node->parent; \ | ||
75 | TR_TREE_ROT_RELCHILD_##lr(_node) = relChildLvl2; \ | 75 | TR_TREE_ROT_RELCHILD_##lr(_node) = relChildLvl2; \ |
76 | if (NULL != relChildLvl2) { \ | 76 | if (NULL != relChildLvl2) { \ |
77 | relChildLvl2->parent = _node; \ | 77 | relChildLvl2->parent = _node; \ |
@@ -96,7 +96,7 @@ typedef enum {rbBlack=1, rbRed=2} TR_rbColor; | @@ -96,7 +96,7 @@ typedef enum {rbBlack=1, rbRed=2} TR_rbColor; | ||
96 | #define TR_TREE_NODE_STRICT_RED(node) (NULL != (node) && rbRed == (node)->color) | 96 | #define TR_TREE_NODE_STRICT_RED(node) (NULL != (node) && rbRed == (node)->color) |
97 | 97 | ||
98 | #define TR_TREE_INORDER_SUCC(node, succ) \ | 98 | #define TR_TREE_INORDER_SUCC(node, succ) \ |
99 | - succ = TR_TREE_RIGHT((node)); \ | 99 | + succ = (node)->right; \ |
100 | while (NULL != succ->left) { \ | 100 | while (NULL != succ->left) { \ |
101 | succ = succ->left; \ | 101 | succ = succ->left; \ |
102 | } | 102 | } |
@@ -4,8 +4,8 @@ AUTOMAKE_OPTIONS = subdir-objects | @@ -4,8 +4,8 @@ AUTOMAKE_OPTIONS = subdir-objects | ||
4 | TREE = tree.c \ | 4 | TREE = tree.c \ |
5 | find.c \ | 5 | find.c \ |
6 | insert.c \ | 6 | insert.c \ |
7 | - inOrderSuccessor.c \ | ||
8 | - delete.c walk.c \ | 7 | + delete.c \ |
8 | + walk.c \ | ||
9 | destroy.c | 9 | destroy.c |
10 | 10 | ||
11 | AM_CFLAGS += -I../../include/ | 11 | AM_CFLAGS += -I../../include/ |
@@ -93,43 +93,45 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | @@ -93,43 +93,45 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) | ||
93 | } | 93 | } |
94 | 94 | ||
95 | // case 2 | 95 | // case 2 |
96 | - if (rbBlack == TR_TREE_PARENT(node)->color) { | 96 | + if (rbBlack == node->parent->color) { |
97 | break; | 97 | break; |
98 | } | 98 | } |
99 | 99 | ||
100 | // case 3 | 100 | // case 3 |
101 | - if (NULL != TR_TREE_UNCLE(node) && rbRed == TR_TREE_UNCLE(node)->color) { | ||
102 | - TR_TREE_PARENT(node)->color = rbBlack; | ||
103 | - TR_TREE_UNCLE(node)->color = rbBlack; | ||
104 | - TR_TREE_GRANDPARENT(node)->color = rbRed; | 101 | + TR_Tree uncle = TR_TREE_UNCLE(node); |
105 | 102 | ||
106 | - node = TR_TREE_GRANDPARENT(node); | 103 | + if (NULL != uncle && rbRed == uncle->color) { |
104 | + node->parent->color = rbBlack; | ||
105 | + uncle->color = rbBlack; | ||
106 | + node->parent->parent->color = rbRed; | ||
107 | + | ||
108 | + node = node->parent->parent; | ||
107 | continue; | 109 | continue; |
108 | } | 110 | } |
109 | 111 | ||
110 | // case 4 | 112 | // case 4 |
111 | - if (node == TR_TREE_PARENT(node)->right | ||
112 | - && TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->left) { | 113 | + if (node == node->parent->right |
114 | + && node->parent == node->parent->parent->left) { | ||
113 | 115 | ||
114 | - TR_TREE_ROTATE(left, this, TR_TREE_PARENT(node)); | 116 | + TR_TREE_ROTATE(left, this, node->parent); |
115 | node = TR_TREE_LEFT(node); | 117 | node = TR_TREE_LEFT(node); |
116 | 118 | ||
117 | - } else if (node == TR_TREE_PARENT(node)->left | ||
118 | - && TR_TREE_PARENT(node) == TR_TREE_GRANDPARENT(node)->right) { | 119 | + } else if (node == node->parent->left |
120 | + && node->parent == node->parent->parent->right) { | ||
119 | 121 | ||
120 | - TR_TREE_ROTATE(right, this, TR_TREE_PARENT(node)); | 122 | + TR_TREE_ROTATE(right, this, node->parent); |
121 | node = TR_TREE_RIGHT(node); | 123 | node = TR_TREE_RIGHT(node); |
122 | 124 | ||
123 | } | 125 | } |
124 | 126 | ||
125 | // case 5 | 127 | // case 5 |
126 | - TR_TREE_PARENT(node)->color = rbBlack; | ||
127 | - TR_TREE_GRANDPARENT(node)->color = rbRed; | 128 | + node->parent->color = rbBlack; |
129 | + node->parent->parent->color = rbRed; | ||
128 | 130 | ||
129 | - if (node == TR_TREE_PARENT(node)->left) { | ||
130 | - TR_TREE_ROTATE(right, this, TR_TREE_GRANDPARENT(node)); | 131 | + if (node == node->parent->left) { |
132 | + TR_TREE_ROTATE(right, this, node->parent->parent); | ||
131 | } else { | 133 | } else { |
132 | - TR_TREE_ROTATE(left, this, TR_TREE_GRANDPARENT(node)); | 134 | + TR_TREE_ROTATE(left, this, node->parent->parent); |
133 | } | 135 | } |
134 | 136 | ||
135 | break; | 137 | break; |
Please
register
or
login
to post a comment