コード例 #1
0
void RBTree<T>::Delete(T *delNode)
{
	if (delNode == NULL) {
		return;
	}
	  EventNode *realDelNode = NULL;
	
	if (delNode->left == NULL || delNode->right == NULL)
		realDelNode = delNode;
	else
		realDelNode = Successor(delNode);
	
	  EventNode *son = (realDelNode->left != NULL) ? realDelNode->left : realDelNode->right;
	if (son)
		son->parent = realDelNode->parent;
	
	if (realDelNode->parent == NULL)
		root = son;
    else if (realDelNode == realDelNode->parent->left)
		realDelNode->parent->left = son;
	else
		realDelNode->parent->right = son;
												
	if (delNode != realDelNode)
		realDelNode->SwapKey(realDelNode);

	if (realDelNode->color == BLACK && son)
		DeleteFixup(son);
}
コード例 #2
0
ファイル: rbtree.cpp プロジェクト: gccli/mylibrary
rb_node_t *RBTree::Delete(rb_node_t *z)
{
    rb_node_t *x, *y = z;
    rb_color_t orig_color = z->color;

    if (z->left == &sentry) {
        x = z->right;
        TransPlant(z, z->right);
    } else if (z->right == &sentry) {
        x = z->left;
        TransPlant(z, z->left);
    } else {
        y = Minimum(z->right);
        orig_color = y->color;
        x = y->right;
        if (p_of(y) == z) {
            p_of(x) = y;
        } else {
            TransPlant(y, y->right);
            y->right = z->right;
            p_of(y->right) = y;
        }
        TransPlant(z, y);
        y->left = z->left;
        y->left->p = y;
        y->color = z->color;
    }
    if (orig_color == BLACK) {
        DeleteFixup(x);
    }

    return y;
}
コード例 #3
0
ファイル: rbtree.cpp プロジェクト: magicdog/algorithm
void RbTree<Type>::Delete(const Type& nodevalue) {
    TreeNodePointer deletenode = Search(nodevalue);
    TreeNodePointer deletenodepointer;
    TreeNodePointer childnode;
    assert(deletenode);
    if (deletenode->m_left == &m_nil || deletenode->m_right==&m_nil)
        deletenodepointer = deletenode;
    else 
        deletenodepointer = Successor(deletenode);
    if (deletenodepointer->m_left != &m_nil)
        childnode = deletenodepointer->m_left;
    else
        childnode = deletenodepointer->m_right;
    childnode->m_parent = deletenodepointer->m_parent;
    if (deletenodepointer->m_parent == &m_nil)
        m_root = deletenodepointer;
    else if (deletenodepointer->m_parent->m_left == deletenodepointer)
        deletenodepointer->m_parent->m_left = childnode;
    else 
        deletenodepointer->m_parent->m_right = childnode;
    if (deletenodepointer != deletenode)
        deletenode->m_data = deletenodepointer->m_data;
    if (deletenode->m_color == BLACK)
        DeleteFixup(childnode);
    deletenode = NULL;
    deletenodepointer = NULL;
    childnode = NULL;
}
コード例 #4
0
ファイル: SetImpl.hpp プロジェクト: anareboucas/nanook
BOOL Set<K,ElementTraits>::Delete( Node* pZ )
{
	if (pZ == NULL)
		return false;

	Node* pY = NULL;
	Node* pX = NULL;
	if (IsNil(pZ->m_pLeft) || IsNil(pZ->m_pRight))
		pY = pZ;
	else
		pY = Successor(pZ);

	if (!IsNil(pY->m_pLeft))
		pX = pY->m_pLeft;
	else
		pX = pY->m_pRight;

	pX->m_pParent = pY->m_pParent;

	if (IsNil(pY->m_pParent))
		m_pRoot = pX;
	else if (pY == pY->m_pParent->m_pLeft)
		pY->m_pParent->m_pLeft = pX;
	else
		pY->m_pParent->m_pRight = pX;

	if (pY->m_Status == Node::Black)
		DeleteFixup(pX);

	if (pY != pZ)
		SwapNode(pY, pZ);

	if (m_pRoot != NULL)
		SetNil(&m_pRoot->m_pParent);

	delete pZ;

	return true;
}
コード例 #5
0
ファイル: _redblacktree.cpp プロジェクト: powernano/Alloc
 void _redblacktree::remove(tree *z){ // remove z
     tree *x, *y;
     if (z->left == NULL && z->right == NULL) { // if z is a leaf
         y = z; // ??
     } else {
         y = treeSuccessor(z); // find a node after Z
     }
     if (y->left != NULL) { // check if left tree is null
         x = y->left;
     } else {
         x = y->right;
     }
     if (x != NULL) {
         x->parent = y->parent;
     }
     
     if(y->parent == NULL){
         root = x;
     } else {
         if( y == y->parent->left){
             y->parent->left = x;
         } else {
             y->parent->right = x;
         }
     }
     if (y != z) {
         z->ptr = y->ptr;
         z->size = y->size;
         if (y->ptr == 0) {
             delete z;
         }
     }
     if (y->color == black && x != NULL) {
         DeleteFixup(x);
     }
 }
コード例 #6
0
/*
 *	Delete an element from the tree.
 */
void rbtree_delete(rbtree_t *tree, rbnode_t *Z)
{
	rbnode_t *X, *Y;
	rbnode_t *Parent;

	/*****************************
	 *  delete node Z from tree  *
	 *****************************/

	if (!Z || Z == NIL) return;

	if (Z->Left == NIL || Z->Right == NIL) {
		/* Y has a NIL node as a child */
		Y = Z;
	} else {
		/* find tree successor with a NIL node as a child */
		Y = Z->Right;
		while (Y->Left != NIL) Y = Y->Left;
	}

	/* X is Y's only child */
	if (Y->Left != NIL)
		X = Y->Left;
	else
		X = Y->Right;	/* may be NIL! */

	/* remove Y from the parent chain */
	Parent = Y->Parent;
	if (X != NIL) X->Parent = Parent;

	if (Parent)
		if (Y == Parent->Left)
			Parent->Left = X;
		else
			Parent->Right = X;
	else
		tree->Root = X;

	if (Y != Z) {
		if (tree->freeNode) tree->freeNode(Z->Data);
		Z->Data = Y->Data;
		Y->Data = NULL;

		if (Y->Color == Black)
			DeleteFixup(tree, X, Parent);

		/*
		 *	The user structure in Y->Data MAY include a
		 *	pointer to Y.  In that case, we CANNOT delete
		 *	Y.  Instead, we copy Z (which is now in the
		 *	tree) to Y, and fix up the parent/child
		 *	pointers.
		 */
		memcpy(Y, Z, sizeof(*Y));

		if (!Y->Parent) {
			tree->Root = Y;
		} else {
			if (Y->Parent->Left == Z) Y->Parent->Left = Y;
			if (Y->Parent->Right == Z) Y->Parent->Right = Y;
		}
		if (Y->Left->Parent == Z) Y->Left->Parent = Y;
		if (Y->Right->Parent == Z) Y->Right->Parent = Y;

		free(Z);

	} else {
		if (tree->freeNode) tree->freeNode(Y->Data);

		if (Y->Color == Black)
			DeleteFixup(tree, X, Parent);

		free(Y);
	}

	tree->num_elements--;
}