In rbrepair, re-color the root or the first non-block node we find to

be black.  Re-coloring the root is probably not needed but won't hurt.
This commit is contained in:
Todd C. Miller
2009-06-29 13:36:20 +00:00
parent 4764730f79
commit 02dd5d998d

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004-2005, 2007 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2004-2005, 2007,2009 Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -77,10 +77,11 @@ static void _rbdestroy __P((struct rbtree *, struct rbnode *,
* In addition to the ordinary requirements imposed on binary search * In addition to the ordinary requirements imposed on binary search
* trees, we make the following additional requirements of any valid * trees, we make the following additional requirements of any valid
* red-black tree: * red-black tree:
* 1) The root is black. * 1) Every node is either red or black.
* 2) All leaves are black. * 2) The root is black.
* 3) Both children of each red node are black. * 3) All leaves are black.
* 4) The paths from each leaf up to the root each contain the same * 4) Both children of each red node are black.
* 5) The paths from each leaf up to the root each contain the same
* number of black nodes. * number of black nodes.
*/ */
@@ -372,8 +373,8 @@ rbdestroy(tree, destroy)
* Delete node 'z' from the tree and return its data pointer. * Delete node 'z' from the tree and return its data pointer.
*/ */
void *rbdelete(tree, z) void *rbdelete(tree, z)
struct rbtree* tree; struct rbtree *tree;
struct rbnode* z; struct rbnode *z;
{ {
struct rbnode *x, *y; struct rbnode *x, *y;
void *data = z->data; void *data = z->data;
@@ -444,7 +445,7 @@ rbrepair(tree, node)
node->parent->color = black; node->parent->color = black;
sibling->right->color = black; sibling->right->color = black;
rotate_left(tree, node->parent); rotate_left(tree, node->parent);
break; node = rbroot(tree); /* exit loop */
} }
} else { /* if (node == node->parent->right) */ } else { /* if (node == node->parent->right) */
sibling = node->parent->left; sibling = node->parent->left;
@@ -468,8 +469,9 @@ rbrepair(tree, node)
node->parent->color = black; node->parent->color = black;
sibling->left->color = black; sibling->left->color = black;
rotate_right(tree, node->parent); rotate_right(tree, node->parent);
break; node = rbroot(tree); /* exit loop */
} }
} }
} }
node->color = black;
} }