/* * * rb_delete - deletes @node from @tree, and calls rb_delete_fixup to * restore red-black properties. * */ void rb_delete(rb_tree *tree, rb_node *node) { rb_node *x, *y, *z = node; rb_node *nil = tree->nil, *root = tree->root; y = (z->left == nil || z->right == nil) ? z : rb_tree_successor(tree, z); x = (y->left != nil) ? y->left : y->right; assert(y != root && y != nil); x->parent = y->parent; if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } bool need_fixup = !(y->red); if (y != z) { if (z == z->parent->left) { z->parent->left = y; } else { z->parent->right = y; } z->left->parent = z->right->parent = y; *y = *z; } if (need_fixup) { rb_delete_fixup(tree, x); } }
RBTree::Node *RBTree::rb_delete(Node *nd) { Node *x, *y = NIL; if(nd == NIL || nd == NULL) return NULL; if(nd->left == NIL || nd->right == NIL) y = nd; else y = rb_successor(nd); if(y->left != NIL) x = y->left; else x = y->right; x->parent = y->parent; if(y->parent == NIL) root = x; else if(y == y->parent->left) y->parent->left = x; else y->parent->right = x; if(y != nd) nd->value = y->value; if(y->color == BLACK) rb_delete_fixup(x); return y; }
void Red_Black_Tree::rb_delete(Node* z) { Node *y = z; Node *x; int y_original_color = y->color; if (z->left == _NIL) { x = z->right; rb_transplant(z, z->right); } else if (z->right == _NIL) { x = z->left; rb_transplant(z, z->left); } else { y = minimum(z->right); y_original_color = y->color; x = y->right; if (y->parent == z) x->parent = y; else { rb_transplant(y, y->right); y->right = z->right; y->right->parent = y; } rb_transplant(z, y); y->left = z->left; y->left->parent = y; y->color = z->color; } if (y_original_color == 0) rb_delete_fixup(x); }
void rb_delete(rbt *T, rbn *z) { rbn *x, *y = z; COLOR y_original_color = y->color; if (z->left == T->nil) { x = z->right; rb_transplant(T, z, z->right); } else if (z->right == T->nil) { x = z->left; rb_transplant(T, z, z->left); } else { y = rb_minimum(T, z->right); y_original_color = y->color; x = y->right; if (y->p == z) x->p = y; else { rb_transplant(T, y, y->right); y->right = z->right; y->right->p = y; } rb_transplant(T, z, y); y->left = z->left; y->left->p = y; y->color = z->color; } if (y_original_color == BLACK) rb_delete_fixup(T, x); }
struct rb_node *tree_delete(struct redblack_tree *t, struct rb_node *z) { struct rb_node *y, *x; y = (z->left == nil || z->right == nil) ? z : successor(z); x = (y->left != nil) ? y->left : y->right; x->parent = y->parent; if (y->parent == nil) t->root = x; else if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; if (y != z) { /* true <=> z has two children */ z->key = y->key; /* Copy y's satellite data into z */ } if (y->color == BLACK) rb_delete_fixup(t, x); t->size--; return y; }
void rb_delete(RB_TREE *T, RB_NODE *z) { RB_NODE *x, *y; int y_orginal_color; y = z; y_orginal_color = y->color; if (z->left == T->nil) { x = z->right; rb_transplant(T, z, z->right); } else if(z->right == T->nil) { x = z->left; rb_transplant(T, z, z->left); } else { y = rbtree_minimum(T, z->right); y_orginal_color = y->color; x = y->right; if (y->parent == z) x->parent = y; else { rb_transplant(T, y, y->right); y->right = z->right; y->right->parent = y; } rb_transplant(T, z, y); y->left = z->left; y->left->parent = y; y->color = z->color; } if (y_orginal_color == BLACK) rb_delete_fixup(T, x); }
static void node_delete(struct rbtree *tree, struct rbtree_elem *node) { struct rbtree_elem **rootp; rbnode_t root, delnode, child_delnode, parent; rootp = &tree->root; root = *rootp; /* Identify delnode. */ if (node->left == nil || node->right == nil) { delnode = node ; } else { delnode = successor(node) ; } /* Delete delnode from the tree. */ if (delnode->left != nil) { child_delnode = delnode->left ; } else { child_delnode = delnode->right ; } parent = delnode->parent ; child_delnode->parent = parent ; if (parent == nil) { *rootp = child_delnode ; } else { if (delnode == parent->left) { parent->left = child_delnode; } else { parent->right = child_delnode ; } } if (delnode->color == _BLACK_) { rb_delete_fixup(rootp, child_delnode); } /* Replace node with delnode, thus effectively deleting node. */ if (delnode != node) { /* log_printf("\nmoving delnode[%p] to node[%p]'s position. root=%p\n", delnode, node, *rootp); */ rbtree_elem_replace(rootp, delnode, node); node->parent = node->left = node->right = nil; } //rbtree_inorder_check(tree); //free(node); }
void rb_delete(tree_p tr, tnode_p node){ tnode_p current = pull_out(tr, node); tnode_p next; if(current->left != NULL) next = current->left; else next = current->right; if(rb_color(current) == BLACK) rb_delete_fixup(tr, current->parent, next); current->parent = current->left = current->right = NULL; free(current); }
void rb_delete (struct rb_tree *t, const void *key) { struct rb_node *z; assert (t != NULL); assert (t->root != NULL); assert (key != NULL); z = rb_search (t, key); if (z != &rb_null) { struct rb_node *x, *y, *parent; if (z->left == &rb_null || z->right == &rb_null) y = z; else y = rb_next (z); if (y->left != &rb_null) x = y->left; else x = y->right; parent = y->parent; if (x != &rb_null) x->parent = parent; if (y->parent == &rb_null) t->root = x; else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y != z) z->data = y->data; if (y->color == RB_BLACK) rb_delete_fixup (&t->root, x, parent); free (y); } }
void tree_delete(Node* &T, Node *x) { if (NIL == T || NIL == x) return; // y是要删除的节点 Node *y = NULL; if ((NIL == LEFT(x)) || (NIL == RIGHT(x))) y = x; else y = tree_successor(x); if (y != x) KEY(x) = KEY(y); // y肯定只有一个孩子 Node * z = (NIL != LEFT(y))? LEFT(y): RIGHT(y); // 即使z是NIL,也给挂上,否则会影响rb_delete_fixup PARENT(z) = PARENT(y); if (NIL == PARENT(y)) { // 根节点发生变化 T = z; } else if (IS_LEFT(y)){ LEFT(PARENT(y)) = z; } else { RIGHT(PARENT(y)) = z; } // 调整路径上节点的max值 Node *p = PARENT(z); while (NIL != p) { MAX(p) = max(MAX(LEFT(p)), MAX(RIGHT(p)), HIGH(KEY(p))); p = PARENT(p); } // 如果y是黑色,说明破坏红黑树的规则;如果y是红色,则不会破坏 if (IS_BLACK(y)) { rb_delete_fixup(T, z); } free_node(y); }
void tree_delete(Node* &T, Node *x) { if (NIL == T || NIL == x) return; // y是要删除的节点 Node *y = NULL; if ((NIL == LEFT(x)) || (NIL == RIGHT(x))) y = x; else y = tree_successor(x); if (y != x) KEY(x) = KEY(y); // y肯定只有一个孩子 Node * z = (NIL != LEFT(y))? LEFT(y): RIGHT(y); // 即使z是NIL,也给挂上,否则会影响rb_delete_fixup PARENT(z) = PARENT(y); if (NIL == PARENT(y)) { // 根节点发生变化 T = z; } else if (IS_LEFT(y)){ LEFT(PARENT(y)) = z; } else { RIGHT(PARENT(y)) = z; } // 沿着y节点向上,更新路径上每个节点的size Node *p = y; while (NIL != (p=PARENT(p))) { SIZE(p)--; } // 如果y是黑色,说明破坏红黑树的规则;如果y是红色,则不会破坏 if (IS_BLACK(y)) { rb_delete_fixup(T, z); } free_node(y); }
rb_node_t *rb_delete(rb_tree *tree, rb_node_t *z) { rb_node_t *x; rb_node_t *y; if (z->left == tree->nil || z->right == tree->nil) { y = z; } else { y = rb_successor(tree, z); } if (y->left != tree->nil) { x = y->left; } else { x = y->right; } x->parent = y->parent; if (y->parent == tree->nil) { tree->root = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y != z) { z->key = y->key; z->data = y->data; } if (y->color == BLACK) { rb_delete_fixup(tree, x); } return y; }
rb_node_t* rb_delete(rb_node_t* root,rb_node_t* nodez) { rb_node_t* nodey; rb_node_t* nodex; if(nodez->lchild == &nil || nodez->rchild == &nil) { nodey = nodez; } else nodey = rb_successor(nodez); if(nodez->lchild != &nil) nodex = nodey->lchild; else nodex = nodey->rchild; nodex->parent = nodey->parent; if(nodey->parent == &nil) root = nodex; else { if(nodey == nodey->parent->lchild) nodey->parent->lchild = nodex; else nodey->parent->rchild = nodex; } if(nodey != nodez) { nodez->key = nodey->key; } if(nodey->color == BLACK) { root = rb_delete_fixup(root,nodex); } free(nodey); return root; }
void *rb_tree_delete (RbTree *t, void *data) { TreeNode *x; /* y's not SENTINEL child, or SENTINEL if y don't have child */ TreeNode *y; /* the node actually delete */ TreeNode *z; /* the node contain data */ TreeNode *py; /* parent of y */ assert (t != NULL); z = _bin_tree_search (tree_node_get_right (t->sent), t->sent, t->cmp_f, data); if (z == NULL) { fprintf (stderr, "warning!, return an NULL\n"); return NULL; } if ((tree_node_get_left (z) == t->sent) || (tree_node_get_right (z) == t->sent)) { y = z; } else { y = _bin_tree_successor (z, t->sent); } if (tree_node_get_left (y) != t->sent) { x = tree_node_get_left (y); } else { x = tree_node_get_right (y); } py = tree_node_get_parent (y); tree_node_set_parent (x, py); if (py == t->sent) { tree_node_set_right (t->sent, x); } else { if (y == tree_node_get_left (py)) { tree_node_set_left (py, x); } else { tree_node_set_right (py, x); } } if (y != z) { tree_node_set_content (z, tree_node_get_content (y)); } if (tree_node_is_black (y)) { rb_delete_fixup (t, x); } tree_node_free (y); (t->card)--; return data; }
/* * Delete node z from tree. */ static void rb_delete_node(RBTree *rb, RBNode *z) { RBNode *x, *y; if (!z || z == RBNIL) return; /* * y is the node that will actually be removed from the tree. This will * be z if z has fewer than two children, or the tree successor of z * otherwise. */ if (z->left == RBNIL || z->right == RBNIL) { /* y has a RBNIL node as a child */ y = z; } else { /* find tree successor */ y = z->right; while (y->left != RBNIL) y = y->left; } /* x is y's only child */ if (y->left != RBNIL) x = y->left; else x = y->right; /* Remove y from the tree. */ x->parent = y->parent; if (y->parent) { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } else { rb->root = x; } /* * If we removed the tree successor of z rather than z itself, then attach * the data for the removed node to the one we were supposed to remove. */ if (y != z) z->data = y->data; /* * Removing a black node might make some paths from root to leaf contain * fewer black nodes than others, or it might make two red nodes adjacent. */ if (y->color == RBBLACK) rb_delete_fixup(rb, x); pfree(y); }