Commit af8994b0c684db11907bfd0b4a170516c080989b

Authored by Georg Hopp
1 parent 3266c808

modified the rotate macro so that it works with any structure that has a left, r…

…ight and parent reference to variables of itself.
... ... @@ -60,32 +60,31 @@
60 60 (node2)->parent = (node1)->parent; \
61 61 }
62 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; \
  63 +#define TR_TREE_ROT_RCLD_right(node) ((node)->left)
  64 +#define TR_TREE_ROT_RCLD_left(node) ((node)->right)
  65 +
  66 +#define TR_TREE_ROTATE(lr, root, node) \
  67 + if (NULL != (node)) { \
  68 + void * stPar = node->parent; \
  69 + void * relCld = TR_TREE_ROT_RCLD_##lr(node); \
  70 + void * relCCld = TR_TREE_ROT_RCLD_##lr(node)->lr; \
  71 + void * nLeft_p = &TR_TREE_ROT_RCLD_##lr(node); \
  72 + if (NULL != relCCld) { \
  73 + TR_TREE_ROT_RCLD_##lr(node)->lr->parent = node; \
  74 + } \
  75 + TR_TREE_ROT_RCLD_##lr(node)->lr = node; \
  76 + if (NULL != node->parent) { \
  77 + if (node->parent->left == node) { \
  78 + node->parent->left = relCld; \
  79 + } else { \
  80 + node->parent->right = relCld; \
  81 + } \
  82 + } else { \
  83 + *(root) = relCld; \
  84 + } \
  85 + node->parent = relCld; \
  86 + TR_TREE_ROT_RCLD_##lr(node)->parent = stPar; \
  87 + *(void**)nLeft_p = relCCld; \
89 88 }
90 89
91 90 typedef enum {rbBlack=1, rbRed=2} TR_rbColor;
... ...
... ... @@ -150,10 +150,6 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp)
150 150 sibling->color = rbRed;
151 151 }
152 152
153   - /*
154   - * this is the point where during the balancing our tree
155   - * node can be finally deleted.
156   - */
157 153 if (rbBlack == node->parent->color) {
158 154 // case 3
159 155 node = node->parent;
... ... @@ -216,9 +212,6 @@ TR_treeDelete(TR_Tree * this, const void * search, TR_TreeComp comp)
216 212 }
217 213
218 214 TR_delete(del_node);
219   - /*
220   - * not sure if deleting here is correct.
221   - */
222 215 return data;
223 216 }
224 217
... ...
... ... @@ -11,13 +11,13 @@
11 11 #define HAVE_MEMORY_H 1
12 12
13 13 /* Define to 1 if you have the `memset' function. */
14   -#define HAVE_MEMSET 1
  14 +/* #undef HAVE_MEMSET */
15 15
16 16 /* Define to 1 if you have the <stdarg.h> header file. */
17 17 #define HAVE_STDARG_H 1
18 18
19 19 /* Define to 1 if stdbool.h conforms to C99. */
20   -#define HAVE_STDBOOL_H 1
  20 +/* #undef HAVE_STDBOOL_H */
21 21
22 22 /* Define to 1 if you have the <stdint.h> header file. */
23 23 #define HAVE_STDINT_H 1
... ...
Please register or login to post a comment