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 | 70 | TR_Tree _node = (node); \ |
| 71 | 71 | TR_Tree relChild = TR_TREE_ROT_RELCHILD_##lr(_node); \ |
| 72 | 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 | 75 | TR_TREE_ROT_RELCHILD_##lr(_node) = relChildLvl2; \ |
| 76 | 76 | if (NULL != relChildLvl2) { \ |
| 77 | 77 | relChildLvl2->parent = _node; \ |
| ... | ... | @@ -96,7 +96,7 @@ typedef enum {rbBlack=1, rbRed=2} TR_rbColor; |
| 96 | 96 | #define TR_TREE_NODE_STRICT_RED(node) (NULL != (node) && rbRed == (node)->color) |
| 97 | 97 | |
| 98 | 98 | #define TR_TREE_INORDER_SUCC(node, succ) \ |
| 99 | - succ = TR_TREE_RIGHT((node)); \ | |
| 99 | + succ = (node)->right; \ | |
| 100 | 100 | while (NULL != succ->left) { \ |
| 101 | 101 | succ = succ->left; \ |
| 102 | 102 | } | ... | ... |
| ... | ... | @@ -93,43 +93,45 @@ TR_treeInsert(TR_Tree * this, const void * search, TR_TreeComp comp) |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // case 2 |
| 96 | - if (rbBlack == TR_TREE_PARENT(node)->color) { | |
| 96 | + if (rbBlack == node->parent->color) { | |
| 97 | 97 | break; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 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 | 109 | continue; |
| 108 | 110 | } |
| 109 | 111 | |
| 110 | 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 | 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 | 123 | node = TR_TREE_RIGHT(node); |
| 122 | 124 | |
| 123 | 125 | } |
| 124 | 126 | |
| 125 | 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 | 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 | 137 | break; | ... | ... |
Please
register
or
login
to post a comment