void rbDelete(rbTree tree, void* key, compareFunc compare) { rbTreeNode child; rbTreeNode node = rbLookup(tree, key, compare); if (node == NULL) return; /* Key not found, do nothing */ if (node->left != NULL && node->right != NULL) { /* Copy key/value from predecessor and then delete it instead */ rbTreeNode pred = maximumNode(node->left); node->key = pred->key; node->value = pred->value; node = pred; } assert(node->left == NULL || node->right == NULL); child = node->right == NULL ? node->left : node->right; if (nodeColor(node) == 0) { // BLACK node->color = nodeColor(child); deleteCase1(tree, node); } rbReplaceNode(tree, node, child); if (node->parent == NULL && child != NULL) child->color = 0; // BLACK free(node); }
void *ptree_Maximum(pwr_tStatus *sts, ptree_sTable *tp) { ptree_sNode *np; pool_tRef nr; nr = maximumNode(tp, tp->g->root); if (nr == tp->g->null) pwr_Return(NULL, sts, TREE__NOTFOUND); np = pool_Address(sts, tp->php, nr); pwr_Return((void *)np, sts, TREE__FOUND); }
static tree_sNode* predecessorNode(tree_sTable *tp, tree_sNode *np) { tree_sNode *p; if (np->left != tp->null) return maximumNode(tp, np->left); for (p = np->parent; p != tp->null && np == p->left; np = p, p = p->parent) ; return p; }
void * tree_Maximum(pwr_tStatus *sts, tree_sTable *tp) { tree_sNode *np; np = maximumNode(tp, tp->root); if (np == tp->null) { pwr_Return(NULL, sts, TREE__NOTFOUND); } else { pwr_Return((void *)np, sts, TREE__FOUND); } }
static pool_tRef predecessorNode(ptree_sTable* tp, pool_tRef nr) { pwr_tStatus sts; ptree_sNode* p; pool_tRef pr; ptree_sNode* np = pool_Address(&sts, tp->php, nr); if (np->left != tp->g->null) return maximumNode(tp, np->left); for (pr = np->parent, p = pool_Address(&sts, tp->php, pr); pr != tp->g->null && nr == p->left; np = p, nr = pr, pr = p->parent, p = pool_Address(&sts, tp->php, pr)) ; return pr; }