/* Delete the node z, and free up the space */ static void rb_delete(struct rbnode **rootp, struct rbnode *z) { struct rbnode *x, *y, *tmp; const void *max; if (z->left == RBNULL || z->right == RBNULL) y=z; else y=rb_successor(z); if (y->left != RBNULL) x=y->left; else x=y->right; x->up = y->up; if (y->up == RBNULL) { *rootp=x; } else { if (y==y->up->left) y->up->left = x; else y->up->right = x; } if (y!=z) { z->key = y->key; z->high=y->high; z->object=y->object; } tmp=y->up; while(tmp!=RBNULL) { max=NULL; if (tmp->left!=RBNULL) max=tmp->left->max; if (tmp->right!=RBNULL&& tmp->right->max>max) max=tmp->right->max; if (tmp->high>max) max=tmp->high; tmp->max=max; tmp=tmp->up; } if (y->colour == BLACK) rb_delete_fix(rootp, x); rb_free(y); }
/* -------------------------------------- * brief : delete pdata from rb tree * para t: rb tree pointer * para pdata: data pointer * return : success 0, failed -1 * -------------------------------------- */ int rb_delete(RBTree *t, void *pdata) { if (t->rb_count == 0){ return -1; } RBNode *z = rb_find(t, pdata); if (!z) return -1; RBNode *y = z, *x = NULL; RB_NODE_COLOR color = y->color; if (z->child[0] == t->nil) { x = z->child[1]; rb_trans(t, z, x); } else if (z->child[1] == t->nil) { x = z->child[0]; rb_trans(t, z, x); } else { y = rb_min(t, z->child[1]); color = y->color; x = y->child[1]; if (y->p == z) { x->p = y; } else { rb_trans(t, y, x); y->child[1] = z->child[1]; y->child[1]->p = y; } rb_trans(t, z, y); y->child[0] = z->child[0]; y->child[0]->p = y; y->color = z->color; } //free(z); //z = NULL; ((RBLANode*)z)->next = t->p; t->p = (RBLANode*)z; if (color == RB_BLACK) { rb_delete_fix(t, x); } t->rb_count -= 1; return 0; }