Commit eacc711dbf23da7361be9383000ee79f18e72534
1 parent
07cb2069
moved tree macros to trbase library, as they are also used in the memory managem…
…ent. Some small fixes
Showing
3 changed files
with
7 additions
and
103 deletions
| ... | ... | @@ -23,104 +23,9 @@ |
| 23 | 23 | #ifndef __TR_TREE_H__ |
| 24 | 24 | #define __TR_TREE_H__ |
| 25 | 25 | |
| 26 | +#include "tr/tree_macros.h" | |
| 26 | 27 | #include "trbase.h" |
| 27 | 28 | |
| 28 | -#define TR_TREE_RIGHT(node) (NULL!=(node)?(node)->right:NULL) | |
| 29 | -#define TR_TREE_LEFT(node) (NULL!=(node)?(node)->left:NULL) | |
| 30 | -#define TR_TREE_PARENT(node) (NULL!=(node)?(node)->parent:NULL) | |
| 31 | - | |
| 32 | -/* | |
| 33 | - * Find data in a tree. | |
| 34 | - * Attention: This will change node, so normally you need to copy | |
| 35 | - * it before using this macro. | |
| 36 | - * Also be aware that found needs to be a valid lvalue and an integer. | |
| 37 | - */ | |
| 38 | -#define TR_TREE_FIND(node, search, found, comp) \ | |
| 39 | - (found) = -1; \ | |
| 40 | - while((node) && ((node)->left || (node)->right)) { \ | |
| 41 | - (found) = (comp)((node)->data, (search)); \ | |
| 42 | - if (0 != (found)) { \ | |
| 43 | - if (0 < (found)) { \ | |
| 44 | - (node) = (node)->left; \ | |
| 45 | - } else { \ | |
| 46 | - (node) = (node)->right; \ | |
| 47 | - } \ | |
| 48 | - } else { \ | |
| 49 | - break; \ | |
| 50 | - } \ | |
| 51 | - } | |
| 52 | - | |
| 53 | -#define TR_TREE_CHILD(node) \ | |
| 54 | - (NULL==TR_TREE_RIGHT((node))?TR_TREE_LEFT((node)):TR_TREE_RIGHT((node))) | |
| 55 | - | |
| 56 | -#define TR_TREE_SIBLING(node) \ | |
| 57 | - (NULL!=TR_TREE_PARENT((node))? \ | |
| 58 | - ((node)==TR_TREE_PARENT((node))->left? \ | |
| 59 | - TR_TREE_PARENT((node))->right: \ | |
| 60 | - TR_TREE_PARENT((node))->left): \ | |
| 61 | - NULL) | |
| 62 | - | |
| 63 | -#define TR_TREE_GRANDPARENT(node) (TR_TREE_PARENT((node))->parent) | |
| 64 | - | |
| 65 | -#define TR_TREE_UNCLE(node) \ | |
| 66 | - ((node)->parent == (node)->parent->parent->left? \ | |
| 67 | - (node)->parent->parent->right: \ | |
| 68 | - (node)->parent->parent->left) | |
| 69 | - | |
| 70 | -#define TR_TREE_REPLACE_NODE(root, node1, node2) \ | |
| 71 | - if (NULL != (node1)->parent) { \ | |
| 72 | - if ((node1) == (node1)->parent->left) { \ | |
| 73 | - (node1)->parent->left = (node2); \ | |
| 74 | - } else { \ | |
| 75 | - (node1)->parent->right = (node2); \ | |
| 76 | - } \ | |
| 77 | - } else { \ | |
| 78 | - *(root) = (node2); \ | |
| 79 | - } \ | |
| 80 | - if (NULL != (node2)) { \ | |
| 81 | - (node2)->parent = (node1)->parent; \ | |
| 82 | - } | |
| 83 | - | |
| 84 | -#define TR_TREE_ROT_RCLD_right(node) ((node)->left) | |
| 85 | -#define TR_TREE_ROT_RCLD_left(node) ((node)->right) | |
| 86 | - | |
| 87 | -#define TR_TREE_ROTATE(lr, root, node) \ | |
| 88 | - if (NULL != (node)) { \ | |
| 89 | - void * stPar = node->parent; \ | |
| 90 | - void * relCld = TR_TREE_ROT_RCLD_##lr(node); \ | |
| 91 | - void * relCCld = TR_TREE_ROT_RCLD_##lr(node)->lr; \ | |
| 92 | - void * nLeft_p = &TR_TREE_ROT_RCLD_##lr(node); \ | |
| 93 | - if (NULL != relCCld) { \ | |
| 94 | - TR_TREE_ROT_RCLD_##lr(node)->lr->parent = node; \ | |
| 95 | - } \ | |
| 96 | - TR_TREE_ROT_RCLD_##lr(node)->lr = node; \ | |
| 97 | - if (NULL != node->parent) { \ | |
| 98 | - if (node->parent->left == node) { \ | |
| 99 | - node->parent->left = relCld; \ | |
| 100 | - } else { \ | |
| 101 | - node->parent->right = relCld; \ | |
| 102 | - } \ | |
| 103 | - } else { \ | |
| 104 | - *(root) = relCld; \ | |
| 105 | - } \ | |
| 106 | - node->parent = relCld; \ | |
| 107 | - TR_TREE_ROT_RCLD_##lr(node)->parent = stPar; \ | |
| 108 | - *(void**)nLeft_p = relCCld; \ | |
| 109 | - } | |
| 110 | - | |
| 111 | -typedef enum {rbBlack=1, rbRed=2} TR_rbColor; | |
| 112 | - | |
| 113 | -#define TR_TREE_NODE_BLACK(node) (NULL == (node) || rbBlack == (node)->color) | |
| 114 | -#define TR_TREE_NODE_RED(node) (NULL == (node) || rbRed == (node)->color) | |
| 115 | -#define TR_TREE_NODE_STRICT_BLACK(node) (NULL != (node) && rbBlack == (node)->color) | |
| 116 | -#define TR_TREE_NODE_STRICT_RED(node) (NULL != (node) && rbRed == (node)->color) | |
| 117 | - | |
| 118 | -#define TR_TREE_INORDER_SUCC(node, succ) \ | |
| 119 | - succ = (node)->right; \ | |
| 120 | - while (NULL != succ->left) { \ | |
| 121 | - succ = succ->left; \ | |
| 122 | - } | |
| 123 | - | |
| 124 | 29 | TR_CLASS(TR_Tree) { |
| 125 | 30 | void * data; |
| 126 | 31 | ... | ... |
| ... | ... | @@ -25,12 +25,11 @@ |
| 25 | 25 | void * |
| 26 | 26 | TR_treeFind(TR_Tree this, const void * search, TR_TreeComp comp) |
| 27 | 27 | { |
| 28 | - TR_Tree node = this; | |
| 29 | - int found; | |
| 28 | + int found; | |
| 30 | 29 | |
| 31 | - TR_TREE_FIND(node, search, found, comp); | |
| 30 | + TR_TREE_FIND(this, search, found, comp); | |
| 32 | 31 | |
| 33 | - return found == 0 ? node->data : NULL; | |
| 32 | + return found == 0 ? this->data : NULL; | |
| 34 | 33 | } |
| 35 | 34 | |
| 36 | 35 | // vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment