예제 #1
0
pNode RB_Delete(pNode root,pNode t)
{
    pNode DelNode;

    //{* get tthe node to delete
    if((t -> left == Nil) || (t -> right == Nil) )
        DelNode = t;
    else
        DelNode = Tree_Successor(t);
    //*}

    pNode PreserveNode;
    if( DelNode -> left != Nil)
        PreserveNode = DelNode -> left;
    else
        PreserveNode = DelNode -> right;

    PreserveNode -> parent = DelNode -> parent; // link the DelNode's parent to PreserveNode

    if( DelNode == Nil )   //if DelNode is the root
        root = PreserveNode;
    else {
        if(DelNode == (DelNode -> parent -> left))
            DelNode -> parent -> left = PreserveNode;
        else
            DelNode -> parent -> right = PreserveNode;
    }

    if(DelNode != t)
        t -> key = DelNode -> key;
    /*
    * if the DelNode's color is black, we must fixup RBT to keep the properties of RBT.
    * */

    if(DelNode -> color == "BLACK")
        root = RB_Delete_Fixup(root, PreserveNode);

    return PreserveNode;
}
redblack_node *
redblack_node::RB_Delete(
						 redblack_node *T,
						 redblack_node* z
						 )
{
	redblack_node *y, *x, *temp = T;
	while( temp->parent != NULL )
		temp = temp->parent; // point temp to the root of T
	if( z == NULL )
		z = new redblack_node;
	if( z->left == NULL || z->right == NULL )
		y = z;
	else
		y = static_cast<redblack_node *>(Tree_Successor( z ));
	if( y == NULL )
		y = new redblack_node();
	if( y->left != NULL )
		x = y->left;
	else
		x = y->right;
	if( x == NULL )
		x = new redblack_node;
	x->parent = y->parent;
	if( y->parent == NULL )
		temp = x;
	else if( y == y->parent->left )
		y->parent->left = x;
	else
		y->parent->right = x;
	if( y != z )
		z->data = strdup( y->data );
	if( strcmp( y->color, "BLACK" ) == 0 )
		RB_Delete_Fixup( T, x );
	return y;
}