74 #include "allheaders.h" 86 static void destroy_helper(
node *n);
87 static void count_helper(
node *n, l_int32 *pcount);
88 static void print_tree_helper(FILE *fp,
node *n, l_int32 keytype,
91 static l_int32 compareKeys(l_int32 keytype,
RB_TYPE left,
RB_TYPE right);
96 static l_int32 node_color(
node *n);
108 static node *maximum_node(
node *root);
115 static void verify_properties(
L_RBTREE *t);
117 #ifndef NO_CONSOLE_IO 118 #define VERIFY_RBTREE 0 134 PROCNAME(
"l_rbtreeCreate");
136 if (keytype != L_INT_TYPE && keytype != L_UINT_TYPE &&
137 keytype != L_FLOAT_TYPE && keytype)
138 return (
L_RBTREE *)ERROR_PTR(
"invalid keytype", procName, NULL);
141 t->keytype = keytype;
142 verify_properties(t);
157 PROCNAME(
"l_rbtreeLookup");
160 return (
RB_TYPE *)ERROR_PTR(
"tree is null\n", procName, NULL);
162 node *n = lookup_node(t, key);
163 return n == NULL ? NULL : &n->value;
185 node *n, *inserted_node;
187 PROCNAME(
"l_rbtreeInsert");
190 L_ERROR(
"tree is null\n", procName);
194 inserted_node = new_node(key, value, L_RED_NODE, NULL, NULL);
195 if (t->root == NULL) {
196 t->root = inserted_node;
200 int comp_result = compareKeys(t->keytype, key, n->key);
201 if (comp_result == 0) {
203 LEPT_FREE(inserted_node);
205 }
else if (comp_result < 0) {
206 if (n->left == NULL) {
207 n->left = inserted_node;
213 if (n->right == NULL) {
214 n->right = inserted_node;
221 inserted_node->parent = n;
223 insert_case1(t, inserted_node);
224 verify_properties(t);
240 PROCNAME(
"l_rbtreeDelete");
243 L_ERROR(
"tree is null\n", procName);
247 n = lookup_node(t, key);
248 if (n == NULL)
return;
249 if (n->left != NULL && n->right != NULL) {
251 node *pred = maximum_node(n->left);
253 n->value = pred->value;
258 child = n->right == NULL ? n->left : n->right;
259 if (node_color(n) == L_BLACK_NODE) {
260 n->color = node_color(child);
263 replace_node(t, n, child);
264 if (n->parent == NULL && child != NULL)
265 child->color = L_BLACK_NODE;
268 verify_properties(t);
288 if (*pt == NULL)
return;
298 destroy_helper(
node *n)
301 destroy_helper(n->left);
302 destroy_helper(n->right);
322 PROCNAME(
"l_rbtreeGetFirst");
325 return (
L_RBTREE_NODE *)ERROR_PTR(
"tree is null", procName, NULL);
326 if (t->root == NULL) {
327 L_INFO(
"tree is empty\n", procName);
355 PROCNAME(
"l_rbtreeGetNext");
358 return (
L_RBTREE_NODE *)ERROR_PTR(
"n not defined", procName, NULL);
370 while (n->parent && n->parent->right == n)
392 PROCNAME(
"l_rbtreeGetLast");
395 return (
L_RBTREE_NODE *)ERROR_PTR(
"tree is null", procName, NULL);
396 if (t->root == NULL) {
397 L_INFO(
"tree is empty\n", procName);
403 while (n && n->right)
425 PROCNAME(
"l_rbtreeGetPrev");
428 return (
L_RBTREE_NODE *)ERROR_PTR(
"n not defined", procName, NULL);
440 while (n->parent && n->parent->left == n)
460 count_helper(n, &count);
466 count_helper(
node *n, l_int32 *pcount)
473 count_helper(n->left, pcount);
474 count_helper(n->right, pcount);
489 PROCNAME(
"l_rbtreePrint");
491 L_ERROR(
"stream not defined\n", procName);
495 L_ERROR(
"tree not defined\n", procName);
499 print_tree_helper(fp, t->root, t->keytype, 0);
503 #define INDENT_STEP 4 506 print_tree_helper(FILE *fp,
514 fprintf(fp,
"<empty tree>");
517 if (n->right != NULL) {
518 print_tree_helper(fp, n->right, keytype, indent + INDENT_STEP);
520 for (i = 0; i < indent; i++)
522 if (n->color == L_BLACK_NODE) {
523 if (keytype == L_INT_TYPE)
524 fprintf(fp,
"%lld\n", n->key.itype);
525 else if (keytype == L_UINT_TYPE)
526 fprintf(fp,
"%llx\n", n->key.utype);
527 else if (keytype == L_FLOAT_TYPE)
528 fprintf(fp,
"%f\n", n->key.ftype);
530 if (keytype == L_INT_TYPE)
531 fprintf(fp,
"<%lld>\n", n->key.itype);
532 else if (keytype == L_UINT_TYPE)
533 fprintf(fp,
"<%llx>\n", n->key.utype);
534 else if (keytype == L_FLOAT_TYPE)
535 fprintf(fp,
"<%f>\n", n->key.ftype);
537 if (n->left != NULL) {
538 print_tree_helper(fp, n->left, keytype, indent + INDENT_STEP);
547 compareKeys(l_int32 keytype,
551 static char procName[] =
"compareKeys";
553 if (keytype == L_INT_TYPE) {
554 if (left.itype < right.itype)
556 else if (left.itype > right.itype)
561 }
else if (keytype == L_UINT_TYPE) {
562 if (left.utype < right.utype)
564 else if (left.utype > right.utype)
569 }
else if (keytype == L_FLOAT_TYPE) {
570 if (left.ftype < right.ftype)
572 else if (left.ftype > right.ftype)
578 L_ERROR(
"unknown keytype %d\n", procName, keytype);
588 if (!n || !n->parent || !n->parent->parent) {
589 L_ERROR(
"root and child of root have no grandparent\n",
"grandparent");
592 return n->parent->parent;
596 if (!n || !n->parent) {
597 L_ERROR(
"root has no sibling\n",
"sibling");
600 if (n == n->parent->left)
601 return n->parent->right;
603 return n->parent->left;
607 if (!n || !n->parent || !n->parent->parent) {
608 L_ERROR(
"root and child of root have no uncle\n",
"uncle");
611 return sibling(n->parent);
614 static l_int32 node_color(
node *n) {
615 return n == NULL ? L_BLACK_NODE : n->color;
623 result->value = value;
624 result->color = node_color;
626 result->right = right;
627 if (left != NULL) left->parent = result;
628 if (right != NULL) right->parent = result;
629 result->parent = NULL;
636 int comp_result = compareKeys(t->keytype, key, n->key);
637 if (comp_result == 0) {
639 }
else if (comp_result < 0) {
650 replace_node(t, n, r);
652 if (r->left != NULL) {
661 replace_node(t, n, L);
663 if (L->right != NULL) {
664 L->right->parent = n;
671 if (oldn->parent == NULL) {
674 if (oldn == oldn->parent->left)
675 oldn->parent->left = newn;
677 oldn->parent->right = newn;
680 newn->parent = oldn->parent;
685 if (n->parent == NULL)
686 n->color = L_BLACK_NODE;
692 if (node_color(n->parent) == L_BLACK_NODE)
699 if (node_color(uncle(n)) == L_RED_NODE) {
700 n->parent->color = L_BLACK_NODE;
701 uncle(n)->color = L_BLACK_NODE;
702 grandparent(n)->color = L_RED_NODE;
703 insert_case1(t, grandparent(n));
710 if (n == n->parent->right && n->parent == grandparent(n)->left) {
711 rotate_left(t, n->parent);
713 }
else if (n == n->parent->left && n->parent == grandparent(n)->right) {
714 rotate_right(t, n->parent);
721 n->parent->color = L_BLACK_NODE;
722 grandparent(n)->color = L_RED_NODE;
723 if (n == n->parent->left && n->parent == grandparent(n)->left) {
724 rotate_right(t, grandparent(n));
725 }
else if (n == n->parent->right && n->parent == grandparent(n)->right) {
726 rotate_left(t, grandparent(n));
728 L_ERROR(
"identity confusion\n",
"insert_case5");
732 static node *maximum_node(
node *n) {
734 L_ERROR(
"n not defined\n",
"maximum_node");
737 while (n->right != NULL) {
744 if (n->parent == NULL)
751 if (node_color(sibling(n)) == L_RED_NODE) {
752 n->parent->color = L_RED_NODE;
753 sibling(n)->color = L_BLACK_NODE;
754 if (n == n->parent->left)
755 rotate_left(t, n->parent);
757 rotate_right(t, n->parent);
763 if (node_color(n->parent) == L_BLACK_NODE &&
764 node_color(sibling(n)) == L_BLACK_NODE &&
765 node_color(sibling(n)->left) == L_BLACK_NODE &&
766 node_color(sibling(n)->right) == L_BLACK_NODE) {
767 sibling(n)->color = L_RED_NODE;
768 delete_case1(t, n->parent);
775 if (node_color(n->parent) == L_RED_NODE &&
776 node_color(sibling(n)) == L_BLACK_NODE &&
777 node_color(sibling(n)->left) == L_BLACK_NODE &&
778 node_color(sibling(n)->right) == L_BLACK_NODE) {
779 sibling(n)->color = L_RED_NODE;
780 n->parent->color = L_BLACK_NODE;
787 if (n == n->parent->left &&
788 node_color(sibling(n)) == L_BLACK_NODE &&
789 node_color(sibling(n)->left) == L_RED_NODE &&
790 node_color(sibling(n)->right) == L_BLACK_NODE) {
791 sibling(n)->color = L_RED_NODE;
792 sibling(n)->left->color = L_BLACK_NODE;
793 rotate_right(t, sibling(n));
794 }
else if (n == n->parent->right &&
795 node_color(sibling(n)) == L_BLACK_NODE &&
796 node_color(sibling(n)->right) == L_RED_NODE &&
797 node_color(sibling(n)->left) == L_BLACK_NODE) {
798 sibling(n)->color = L_RED_NODE;
799 sibling(n)->right->color = L_BLACK_NODE;
800 rotate_left(t, sibling(n));
806 sibling(n)->color = node_color(n->parent);
807 n->parent->color = L_BLACK_NODE;
808 if (n == n->parent->left) {
809 if (node_color(sibling(n)->right) != L_RED_NODE) {
810 L_ERROR(
"right sibling is not RED",
"delete_case6");
813 sibling(n)->right->color = L_BLACK_NODE;
814 rotate_left(t, n->parent);
816 if (node_color(sibling(n)->left) != L_RED_NODE) {
817 L_ERROR(
"left sibling is not RED",
"delete_case6");
820 sibling(n)->left->color = L_BLACK_NODE;
821 rotate_right(t, n->parent);
830 static void verify_property_1(
node *root);
831 static void verify_property_2(
node *root);
832 static void verify_property_4(
node *root);
833 static void verify_property_5(
node *root);
834 static void verify_property_5_helper(
node *n,
int black_count,
835 int* black_count_path);
838 static void verify_properties(
L_RBTREE *t) {
840 verify_property_1(t->root);
841 verify_property_2(t->root);
843 verify_property_4(t->root);
844 verify_property_5(t->root);
849 static void verify_property_1(
node *n) {
850 if (node_color(n) != L_RED_NODE && node_color(n) != L_BLACK_NODE) {
851 L_ERROR(
"color neither RED nor BLACK\n",
"verify_property_1");
854 if (n == NULL)
return;
855 verify_property_1(n->left);
856 verify_property_1(n->right);
859 static void verify_property_2(
node *root) {
860 if (node_color(root) != L_BLACK_NODE)
861 L_ERROR(
"root is not black!\n",
"verify_property_2");
864 static void verify_property_4(
node *n) {
865 if (node_color(n) == L_RED_NODE) {
866 if (node_color(n->left) != L_BLACK_NODE ||
867 node_color(n->right) != L_BLACK_NODE ||
868 node_color(n->parent) != L_BLACK_NODE) {
869 L_ERROR(
"children & parent not all BLACK",
"verify_property_4");
873 if (n == NULL)
return;
874 verify_property_4(n->left);
875 verify_property_4(n->right);
878 static void verify_property_5(
node *root) {
879 int black_count_path = -1;
880 verify_property_5_helper(root, 0, &black_count_path);
883 static void verify_property_5_helper(
node *n,
int black_count,
884 int* path_black_count) {
885 if (node_color(n) == L_BLACK_NODE) {
889 if (*path_black_count == -1) {
890 *path_black_count = black_count;
891 }
else if (*path_black_count != black_count) {
892 L_ERROR(
"incorrect black count",
"verify_property_5_helper");
896 verify_property_5_helper(n->left, black_count, path_black_count);
897 verify_property_5_helper(n->right, black_count, path_black_count);
void l_rbtreeDestroy(L_RBTREE **pt)
l_rbtreeDestroy()
void l_rbtreePrint(FILE *fp, L_RBTREE *t)
l_rbtreePrint()
L_RBTREE_NODE * l_rbtreeGetLast(L_RBTREE *t)
l_rbtreeGetLast()
L_RBTREE * l_rbtreeCreate(l_int32 keytype)
l_rbtreeCreate()
L_RBTREE_NODE * l_rbtreeGetFirst(L_RBTREE *t)
l_rbtreeGetFirst()
L_RBTREE_NODE * l_rbtreeGetPrev(L_RBTREE_NODE *n)
l_rbtreeGetPrev()
void l_rbtreeDelete(L_RBTREE *t, RB_TYPE key)
l_rbtreeDelete()
void l_rbtreeInsert(L_RBTREE *t, RB_TYPE key, RB_TYPE value)
l_rbtreeInsert()
RB_TYPE * l_rbtreeLookup(L_RBTREE *t, RB_TYPE key)
l_rbtreeLookup()
L_RBTREE_NODE * l_rbtreeGetNext(L_RBTREE_NODE *n)
l_rbtreeGetNext()
l_int32 l_rbtreeGetCount(L_RBTREE *t)
l_rbtreeGetCount()