55 memset(aTree,
'\0',
sizeof(
Tree));
67 #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING) 87 #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING) 98 #define max(a, b) (a > b) ? a : b; 105 return (aNode != NULL) && (aNode->red);
111 return (aNode == NULL) || (aNode->red == 0);
115 int TreeWalk(
Node* curnode,
int depth)
119 int left = TreeWalk(curnode->child[
LEFT], depth+1);
120 int right = TreeWalk(curnode->child[
RIGHT], depth+1);
121 depth =
max(left, right);
135 int TreeMaxDepth(
Tree *aTree)
149 Node* other = curnode->child[!direction];
151 curnode->child[!direction] = other->child[direction];
152 if (other->child[direction] != NULL)
153 other->child[direction]->parent = curnode;
154 other->parent = curnode->parent;
155 if (curnode->parent == NULL)
157 else if (curnode == curnode->parent->child[direction])
158 curnode->parent->child[direction] = other;
160 curnode->parent->child[!direction] = other;
161 other->child[direction] = curnode;
162 curnode->parent = other;
168 Node* uncle = curnode->parent->parent->child[which];
172 curnode->parent->red = uncle->red = 0;
173 curnode = curnode->parent->parent;
178 if (curnode == curnode->parent->child[which])
180 curnode = curnode->parent;
183 curnode->parent->red = 0;
184 curnode->parent->parent->red = 1;
185 TreeRotate(aTree, curnode->parent->parent, which, index);
193 while (curnode &&
isRed(curnode->parent) && curnode->parent->parent)
195 if (curnode->parent == curnode->parent->parent->child[
LEFT])
212 Node* curparent = NULL;
221 result = aTree->
index[index].
compare(curnode->content, content, 1);
228 curnode = curnode->child[
left];
240 aTree->
size += (size - curnode->size);
245 #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING) 252 memset(newel,
'\0',
sizeof(
Node));
254 curparent->child[
left] = newel;
257 newel->parent = curparent;
265 newel->content = content;
279 for (i = 0; i < aTree->
indexes; ++i)
293 result = aTree->
index[index].
compare(curnode->content, key, value);
297 curnode = curnode->child[result > 0];
324 while (curnode->child[
LEFT])
325 curnode = curnode->child[
LEFT];
332 if (curnode->child[
RIGHT])
336 Node* curparent = curnode->parent;
337 while (curparent && curnode == curparent->child[
RIGHT])
340 curparent = curparent->parent;
366 Node* sibling = curnode->parent->child[which];
371 curnode->parent->red = 1;
372 TreeRotate(aTree, curnode->parent, !which, index);
373 sibling = curnode->parent->child[which];
376 curnode = curnode->parent;
377 else if (
isBlack(sibling->child[!which]) &&
isBlack(sibling->child[which]))
380 curnode = curnode->parent;
384 if (
isBlack(sibling->child[which]))
386 sibling->child[!which]->red = 0;
389 sibling = curnode->parent->child[which];
391 sibling->red = curnode->parent->red;
392 curnode->parent->red = 0;
393 sibling->child[which]->red = 0;
394 TreeRotate(aTree, curnode->parent, !which, index);
406 if (((curnode->content) ? curnode : NULL) == curnode->parent->child[
LEFT])
422 Node* redundant = curnode;
423 Node* curchild = NULL;
424 size_t size = curnode->size;
425 void* content = curnode->content;
428 if (curnode->child[
LEFT] && curnode->child[
RIGHT])
431 curchild = redundant->child[(redundant->child[
LEFT] != NULL) ?
LEFT :
RIGHT];
433 curchild->parent = redundant->parent;
435 if (redundant->parent == NULL)
439 if (redundant == redundant->parent->child[
LEFT])
440 redundant->parent->child[
LEFT] = curchild;
442 redundant->parent->child[
RIGHT] = curchild;
445 if (redundant != curnode)
447 curnode->content = redundant->content;
448 curnode->size = redundant->size;
453 if (curchild == NULL)
455 if (redundant->parent)
458 memset(&temp,
'\0',
sizeof(
Node));
459 temp.parent = redundant->parent;
468 #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING) 503 for (i = 0; i < aTree->
indexes; ++i)
513 void* content = NULL;
520 for (i = 0; i < aTree->
indexes; ++i)
541 return (i > j) ? -1 : (i == j) ? 0 : 1;
547 return (a > b) ? -1 : (a == b) ? 0 : 1;
553 return strcmp((
char*)a, (
char*)b);
557 #if defined(UNIT_TESTS) 561 Node* curnode = NULL;
567 Node* prevnode = curnode;
571 if (prevnode && curnode && (*(
int*)(curnode->content) < *(
int*)(prevnode->content)))
573 printf(
"out of order %d < %d\n", *(
int*)(curnode->content), *(
int*)(prevnode->content));
581 int traverse(
Tree *t,
int lookfor)
583 Node* curnode = NULL;
586 printf(
"Traversing\n");
591 Node* prevnode = curnode;
596 if (prevnode && curnode && (*(
int*)(curnode->content) < *(
int*)(prevnode->content)))
598 printf(
"out of order %d < %d\n", *(
int*)(curnode->content), *(
int*)(prevnode->content));
600 if (curnode && (lookfor == *(
int*)(curnode->content)))
601 printf(
"missing item %d actually found\n", lookfor);
603 printf(
"End traverse %d\n", rc);
610 int i, *ip, *todelete;
611 Node* current = NULL;
615 printf(
"Tree initialized\n");
621 TreeAdd(t, (
void*)ip,
sizeof(
int));
631 for (i = 0; i < limit; i++)
633 void* replaced = NULL;
637 replaced =
TreeAdd(t, (
void*)ip,
sizeof(
int));
647 printf(
"Tree element added %d\n", *ip);
651 printf(
"%d elements, check result %d\n", i+1, rc);
659 for (i = 0; i < limit; i++)
661 int parm = actual[i];
668 printf(
"Tree find %d %d\n", parm, *(
int*)(found->content));
671 printf(
"%d not found\n", parm);
679 for (i = limit -1; i >= 0; i--)
681 int parm = actual[i];
690 printf(
"%d Tree remove %d %d\n", i, parm, *(
int*)(found));
696 printf(
"%d %d not found\n", i, parm);
698 for (i = 0; i < limit; i++)
699 if (actual[i] == parm)
701 printf(
"%d occurs %d times\n", parm, count);
707 printf(
"%d elements, check result %d\n", i+1, rc);
712 printf(
"finished\n");
716 int main(
int argc,
char *argv[])
void * TreeAddByIndex(Tree *aTree, void *content, size_t size, int index)
enum MQTTPropertyCodes value
void TreeAddIndex(Tree *aTree, int(*compare)(void *, void *, int))
void * TreeAdd(Tree *aTree, void *content, size_t size)
int TreePtrCompare(void *a, void *b, int content)
Node * TreeSuccessor(Node *curnode)
unsigned int allow_duplicates
void * TreeRemoveKey(Tree *aTree, void *key)
Node * TreeNextElementIndex(Tree *aTree, Node *curnode, int index)
void * mymalloc(char *file, int line, size_t size)
Node * TreeMinimum(Node *curnode)
void * TreeRemoveKeyIndex(Tree *aTree, void *key, int index)
Node * TreeBAASub(Tree *aTree, Node *curnode, int which, int index)
void * TreeRemove(Tree *aTree, void *content)
int TreeIntCompare(void *a, void *b, int content)
void * TreeRemoveNodeIndex(Tree *aTree, Node *curnode, int index)
void myfree(char *file, int line, void *p)
unsigned int heap_tracking
void TreeInitializeNoMalloc(Tree *aTree, int(*compare)(void *, void *, int))
Node * TreeNextElement(Tree *aTree, Node *curnode)
struct Tree::@72 index[2]
Tree * TreeInitialize(int(*compare)(void *, void *, int))
Node * TreeFind(Tree *aTree, void *key)
static void check(LexState *ls, int c)
void TreeBalanceAfterRemove(Tree *aTree, Node *curnode, int index)
void TreeRotate(Tree *aTree, Node *curnode, int direction, int index)
Node * TreeFindIndex1(Tree *aTree, void *key, int index, int value)
void * TreeRemoveIndex(Tree *aTree, void *content, int index)
Node * TreeFindIndex(Tree *aTree, void *key, int index)
Node * TreeBARSub(Tree *aTree, Node *curnode, int which, int index)
void TreeFree(Tree *aTree)
Node * TreeFindContentIndex(Tree *aTree, void *key, int index)
void TreeBalanceAfterAdd(Tree *aTree, Node *curnode, int index)
int(* compare)(void *, void *, int)
int main(int argc, char **argv)
int TreeStringCompare(void *a, void *b, int content)