Exemple #1
0
/*
 * Delete node 'z' from the tree and return its data pointer.
 */
void *rbtree_delete(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) == rbtree_root(tree)) {
		rbtree_first(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);

	tree->num_nodes--;
	return (data);
}
Exemple #2
0
/*
 * 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;
}