/* * Repair the tree after a node has been deleted by rotating and repainting * colors to restore the 4 properties inherent in red-black trees. */ static void rbrepair(struct rbtree *tree, struct rbnode *node) { struct rbnode *sibling; while (node->color == black && node != rbroot(tree)) { if (node == node->parent->left) { sibling = node->parent->right; if (sibling->color == red) { sibling->color = black; node->parent->color = red; rotate_left(tree, node->parent); sibling = node->parent->right; } if (sibling->right->color == black && sibling->left->color == black) { sibling->color = red; node = node->parent; } else { if (sibling->right->color == black) { sibling->left->color = black; sibling->color = red; rotate_right(tree, sibling); sibling = node->parent->right; } sibling->color = node->parent->color; node->parent->color = black; sibling->right->color = black; rotate_left(tree, node->parent); node = rbroot(tree); /* exit loop */ } } else { /* if (node == node->parent->right) */ sibling = node->parent->left; if (sibling->color == red) { sibling->color = black; node->parent->color = red; rotate_right(tree, node->parent); sibling = node->parent->left; } if (sibling->right->color == black && sibling->left->color == black) { sibling->color = red; node = node->parent; } else { if (sibling->left->color == black) { sibling->right->color = black; sibling->color = red; rotate_left(tree, sibling); sibling = node->parent->left; } sibling->color = node->parent->color; node->parent->color = black; sibling->left->color = black; rotate_right(tree, node->parent); node = rbroot(tree); /* exit loop */ } } } node->color = black; }
/* * Returns the successor of node, or nil if there is none. */ static struct rbnode * rbsuccessor(struct rbtree *tree, struct rbnode *node) { struct rbnode *succ; if ((succ = node->right) != rbnil(tree)) { while (succ->left != rbnil(tree)) succ = succ->left; } else { /* No right child, move up until we find it or hit the root */ for (succ = node->parent; node == succ->right; succ = succ->parent) node = succ; if (succ == rbroot(tree)) succ = rbnil(tree); } return succ; }
/* * Returns the successor of node, or nil if there is none. */ static struct rbnode * rbsuccessor(struct rbtree *tree, struct rbnode *node) { struct rbnode *succ; debug_decl(rbsuccessor, SUDO_DEBUG_RBTREE) if ((succ = node->right) != rbnil(tree)) { while (succ->left != rbnil(tree)) succ = succ->left; } else { /* No right child, move up until we find it or hit the root */ for (succ = node->parent; node == succ->right; succ = succ->parent) node = succ; if (succ == rbroot(tree)) succ = rbnil(tree); } debug_return_ptr(succ); }
/* * Delete node 'z' from the tree and return its data pointer. */ void *rbdelete(struct rbtree *tree, struct rbnode *z) { struct rbnode *x, *y; void *data = z->data; if (z->left == rbnil(tree) || z->right == rbnil(tree)) y = z; else y = rbsuccessor(tree, z); x = (y->left == rbnil(tree)) ? y->right : y->left; if ((x->parent = y->parent) == rbroot(tree)) { rbfirst(tree) = x; } else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y->color == black) rbrepair(tree, x); if (y != z) { y->left = z->left; y->right = z->right; y->parent = z->parent; y->color = z->color; z->left->parent = z->right->parent = y; if (z == z->parent->left) z->parent->left = y; else z->parent->right = y; } free(z); return data; }
/* * Insert data pointer into a redblack tree. * Returns a NULL pointer on success. If a node matching "data" * already exists, a pointer to the existant node is returned. */ struct rbnode * rbinsert(struct rbtree *tree, void *data) { struct rbnode *node = rbfirst(tree); struct rbnode *parent = rbroot(tree); int res; /* Find correct insertion point. */ while (node != rbnil(tree)) { parent = node; if ((res = tree->compar(data, node->data)) == 0) return node; node = res < 0 ? node->left : node->right; } node = (struct rbnode *) emalloc(sizeof(*node)); node->data = data; node->left = node->right = rbnil(tree); node->parent = parent; if (parent == rbroot(tree) || tree->compar(data, parent->data) < 0) parent->left = node; else parent->right = node; node->color = red; /* * If the parent node is black we are all set, if it is red we have * the following possible cases to deal with. We iterate through * the rest of the tree to make sure none of the required properties * is violated. * * 1) The uncle is red. We repaint both the parent and uncle black * and repaint the grandparent node red. * * 2) The uncle is black and the new node is the right child of its * parent, and the parent in turn is the left child of its parent. * We do a left rotation to switch the roles of the parent and * child, relying on further iterations to fixup the old parent. * * 3) The uncle is black and the new node is the left child of its * parent, and the parent in turn is the left child of its parent. * We switch the colors of the parent and grandparent and perform * a right rotation around the grandparent. This makes the former * parent the parent of the new node and the former grandparent. * * Note that because we use a sentinel for the root node we never * need to worry about replacing the root. */ while (node->parent->color == red) { struct rbnode *uncle; if (node->parent == node->parent->parent->left) { uncle = node->parent->parent->right; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else /* if (uncle->color == black) */ { if (node == node->parent->right) { node = node->parent; rotate_left(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_right(tree, node->parent->parent); } } else { /* if (node->parent == node->parent->parent->right) */ uncle = node->parent->parent->left; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else /* if (uncle->color == black) */ { if (node == node->parent->left) { node = node->parent; rotate_right(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_left(tree, node->parent->parent); } } } rbfirst(tree)->color = black; /* first node is always black */ return NULL; }