/* z is deleted node; y is the node which position on the tree will be replaced(Note that y not must be z); x is the node that replacing y's position on the tree; */ void RBTreeDelete(struct rb_tree *rb, rb_key_t key) { struct rb_node *x, *y, *z; int yOriginColor; z = RBTreeSearch(rb, key); if (z == rb->nil) return ; rb->size--; y = z; yOriginColor = y->color; if (z->left == rb->nil) { x = z->right; RBTreeTransplant(rb, z, z->right); } else if (z->right == rb->nil) { x = z->left; RBTreeTransplant(rb, z, z->left); } else { y = RBTreeMin(rb, z->right); yOriginColor = y->color; x = y->right; if (y->p != z) { RBTreeTransplant(rb, y, y->right); y->right = z->right; y->right->p = y; } RBTreeTransplant(rb, z, y); y->left = z->left; y->left->p = y; y->color = z->color; } if (yOriginColor == BLACK && x != rb->nil) RBTreeDeleteFixup(rb, x); free(z); }
void RBTreeDel(RBTree_t *tree, PrimaryType_t num) { RBData_t check; check.mPrimaryVal = num; struct RBNode_t *node = RBTreeSearch(tree->mTreeRoot, &check, &tree->mSentinelLeaf); if(node == &tree->mSentinelLeaf) { puts("Error"); return; } RBTreeDelete(tree, node); free(node); }