void tree_delete(struct node **root,int data) { struct node *y=NULL; struct node *root_temp=*root; struct node *z=*root; while(z->left!=NULL&& z->right!=NULL) { if(data==z->data) break; else if(data<z->data) z=z->left; else z=z->right; } if(z->left==NULL) transplant(root_temp,z,z->right); else if(z->right==NULL) transplant(root_temp,z,z->left); else y=tree_minimum(z->right); if(y->parent!=z) { transplant(root_temp,y,y->right); y->right = z->right; (z->right)->parent=y; } transplant(root_temp,z,y); y->left=z->left; (y->left)->parent=y; }
bool remove(BinarySearchTree * tree, int key) { if (search(tree, key) == NULL) { return false; } TreeNode * deleteNode = search(tree, key); if (deleteNode->left == NULL) { transplant(tree, deleteNode, deleteNode->right); } else if (deleteNode->right == NULL) { transplant(tree, deleteNode, deleteNode->left); } else { TreeNode * sucNode = successor(tree, deleteNode->key); if (sucNode->parent != deleteNode) { transplant(tree, sucNode, sucNode->right); sucNode->right = deleteNode->right; sucNode->right->parent = sucNode; } transplant(tree, deleteNode, sucNode); sucNode->left = deleteNode->left; sucNode->left->parent = sucNode; } return true; }
void balanced_pst::erase(const point &v) { node* z = find(v); node* y = z; node* x; bool y_original_color = y->color; if (z->left == sentinel) { x = z->right; transplant(z,z->right); } else if (z->right == sentinel) { x = z->left; transplant(z,z->left); } else { y = min(z->right); y_original_color = y->color; x = y->right; if (y->p == z) x->p = y; else { transplant(y,y->right); y->right = z->right; y->right->p = y; } transplant(z,y); y->left = z->left; y->left->p = y; y->color = z->color; } if (y_original_color == BLACK) erase_fixup(x); _size--; delete z; }
node* tree_delete() { node* z = tree_maximum(T); if(z->lchild == NULL) transplant(z,z->rchild); else if(z->rchild == NULL) transplant(z,z->lchild); else { node* y = tree_minimum(z->rchild); if(y->parent != z) { transplant(y,y->rchild); y->rchild = z->rchild; y->rchild->parent = y; } transplant(z,y); y->lchild = z->lchild; y->lchild->parent = y; } z->lchild = z->rchild = z->parent = NULL; return z; }
void erase(node *z) { if(z == nil) return; sz--; node *y = z, *x; int prevy = y->red; if(z->ch[0] == nil) { x = z->ch[1]; transplant(z,z->ch[1]); } else if(z->ch[1] == nil) { x = z->ch[0]; transplant(z,z->ch[0]); } else { y = minimum(z->ch[1]); prevy = y->red; x = y->ch[1]; if(y->par == z) x->par = y; else { transplant(y,y->ch[1]); y->ch[1] = z->ch[1]; y->ch[1]->par = y; } transplant(z,y); y->ch[0] = z->ch[0]; y->ch[0]->par = y; y->red = z->red; } nil->par = nil->ch[0] = nil->ch[1] = nil; if(!prevy) erase_update(x); delete z; nil->par = nil->ch[0] = nil->ch[1] = nil; }
node_t* tree_delete(node_t* root, int key) { node_t* z = tree_search(root, key); if (!z) { return root; } if (z->left == NULL) { root = transplant(root, z, z->right); } else if (z->right == NULL) { root = transplant(root, z, z->left); } else { node_t* y = tree_minimum(z->right); if (y->parent != z) { root = transplant(root, y, y->right); y->right = z->right; y->right->parent = y; } root = transplant(root, z, y); y->left = z->left; y->left->parent = y; } free(z); return root; }
/** * Removes a node from the RB tree. * * @param[in] table the table on which this operation is performed * @param[in] z the node that is being removed */ static void remove_node(TreeTable *table, RBNode *z) { RBNode *x; RBNode *y = z; int y_color = y->color; if (z->left == table->sentinel) { x = z->right; transplant(table, z, z->right); } else if (z->right == table->sentinel) { x = z->left; transplant(table, z, z->left); } else { y = tree_min(table, z->right); y_color = y->color; x = y->right; if (y->parent == z) { x->parent = y; } else { transplant(table, y, y->right); y->right = z->right; y->right->parent = y; } transplant(table, z, y); y->left = z->left; y->left->parent = y; y->color = z->color; } if (y_color == RB_BLACK) rebalance_after_delete(table, x); table->mem_free(z); table->size--; }
void treeRemove(Elem* root, Elem* node) { if (node->left == NULL) { transplant(root, node, node->right); } else if (node->right == NULL) { transplant(root, node, node->left); } else { Elem* y = treeMinimum(node->right); if (y->parent != node) { transplant(root, y, y->right); y->right = node->right; y->right->parent = y; } transplant(root, node, y); y->left = node->left; y->left->parent = y; } }
void nodedelete (node* target) { if (target != NULL) { node* y; if (target->left == NULL) { transplant(target,target->right); } else { if (target->right == NULL) { transplant(target,target->left); } else { y = minvalue(target->right); if (y->up != target) { transplant(y,y->right); y->right = target->right; y->right->up = y; } transplant(target,y); y->left = target->left; y->left->up = y; } } free(target); } }
void deleteFromAVLTree(Tree *tree, Node *node) { if (node == NULL) return; Node *d = node->parent; Node *n = node; while (n->parent != NULL) { n = n->parent; n->size--; } if (node->left == NULL) { transplant(tree, node, node->right); } else if (node->right == NULL) { transplant(tree, node, node->left); } else { Tree t; t.root = node->right; Node *min = minimum(&t); Node *end = min->right; min->size = node->size - 1; if (node == tree->root) tree->root = min; if (node->right != min) { transplant(tree, min, min->right); min->right = node->right; node->right->parent = min; d = min->parent; } else { d = min; } min->left = node->left; node->left->parent = min; transplant(tree, node, min); n = min->right; while (n != end) { n->size--; n = n->left; } } AVLDeleteFixup(tree, d); free(node); }
void Foam::binaryTree<CompType, ThermoType>::deleteLeaf(chP*& phi0) { if (size_ == 1) // only one point is stored { deleteDemandDrivenData(phi0); deleteDemandDrivenData(root_); } else if (size_ > 1) { bn* z = phi0->node(); bn* x; chP* siblingPhi0 = chemPSibling(phi0); if (siblingPhi0 != nullptr)// the sibling of phi0 is a chemPoint { // z was root (only two chemPoints in the tree) if (z->parent() == nullptr) { root_ = new bn(); root_->leafLeft()=siblingPhi0; siblingPhi0->node()=root_; } else if (z == z->parent()->nodeLeft()) { z->parent()->leafLeft() = siblingPhi0; z->parent()->nodeLeft() = nullptr; siblingPhi0->node() = z->parent(); } else if (z == z->parent()->nodeRight()) { z->parent()->leafRight() = siblingPhi0; z->parent()->nodeRight() = nullptr; siblingPhi0->node() = z->parent(); } else { FatalErrorInFunction << "wrong addressing of the initial leaf" << exit(FatalError); } } else { x = nodeSibling(phi0); if (x !=nullptr) { transplant(z, x); } else { FatalErrorInFunction << "inconsistent structure of the tree, no leaf and no node" << exit(FatalError); } } deleteDemandDrivenData(phi0); deleteDemandDrivenData(z); } size_--; }
void * rbtree_delete(struct rbtree * tree, void * key) { struct rbtree_node * node = find_node(tree, key); if(node == NULL) return NULL; void * retval = node->data; struct rbtree_node * z = node; struct rbtree_node * y = z; struct rbtree_node * x; int y_orig_color = y->color; if(z->left == tree->nil) { x = z->right; transplant(tree, z, z->right); } else if(z->right == tree->nil) { x = z->left; transplant(tree, z, z->left); } else { y = tree_minimum(tree, z->right); y_orig_color = y->color; x = y->right; if(y->parent == z) x->parent = y; else { transplant(tree, y, y->right); y->right = z->right; y->right->parent = y; } transplant(tree, z, y); y->left = z->left; y->left->parent = y; y->color = z->color; } if(y_orig_color == RBTREE_BLACK) delete_fixup(tree, x); if(tree->free_key) tree->free_key(node->key); free(node); return retval; }
void BinaryTree::deleteNode(TreeNode* node) { if(node->left == 0){ transplant(node,node->right); }else if(node->right == 0){ transplant(node,node->left); }else{ TreeNode* y = minimum(node->right); if(y->parent != node){ transplant(y,y->right); y->right = node->right; y->right->parent = y; } transplant(node,y); y->left = node->left; y->left->parent = y; } }
void tree_delete(BSTHead T,BSTree z) { BSTree y; if(z->left == NULL){//z没有左孩子 transplant(T,z,z->right); }else if(z->right == NULL){//z有一个左孩子,但没有右孩子 transplant(T,z,z->left); }else{//有两个孩子的情况 y = tree_minimum(z->right);//查找z的后继,z的右子树非空 } if(y->p != z){//如果y不是z的左孩子,那么用y的右孩子替换y并成为y的双亲的一个孩子 transplant(T,y,y->right); y->right = z->right; y->right->p = y; } transplant(T,z,y); y->left = z->left; y->left->p = y; }
/* Movie *MovieTree::deleteMovie(Movie*) Description: Private function used by deleteMovie to delete a movie. Example: MovieTree tree; tree.deleteMovie("Back to the Future"); Precondition: The ends of the tree are nil. The tree is arranged correctly. Postcondition: The movie will be deleted from the tree, and the tree will be rebalanced. */ void MovieTree::deleteMovie(Movie *z) { Movie *x = nil; Movie *y = z; bool wasRed = y->isRed; if (z->left == nil) { x = z->right; transplant(z, z->right); } else if (z->right == nil) { x = z->left; transplant(z, z->left); } else { y = z->right; while (y->left != nil) { y = y->left; } wasRed = y->isRed; x = y->right; if (y->parent == z) { x->parent = y; } else { transplant(y, y->right); y->right = z->right; y->right->parent = y; } transplant(z, y); y->left = z->left; y->left->parent = y; y->isRed = z->isRed; } if (!wasRed) { fixUpDelete(x); } }
iterator eraser(iterator iter){ rb_node* z=iter.pointer(); rb_node* y=z; RB_Color y_color=y->_color; rb_node *x=NULL; if(z->_left==_nil ){ //case1: z's left child is nil x=z->_right; transplant(z, z->_right); } else{ if(z->_right==_nil){// case2: z's right child is nil x=z->_left; transplant(z, z->_left); } else{//case3: both children of z are not nil y=sub_min(z->_right).pointer(); y_color=y->_color; x=y->_right; if(y->_parent==z) x->_parent=y; else{ transplant(y, y->_right); //link z's right subtree into y, only y isn't z's child; y->_right=z->_right; y->_right->_parent=y; } transplant(z, y); //link z's subtree into y. y->_left=z->_left; y->_left->_parent=y; y->_color=z->_color; } } iterator result = ++iterator(z); delete z; if(y_color==black) eraser_fixup(x); return result; };
struct t_node *delete_node(struct bs_tree *tree, struct t_node *key, comp eq) { if(!key->left) { transplant(tree, key, key->right); } else if(!key->right) { transplant(tree, key, key->left); } else { struct t_node *y = min(key->right); if(eq(y->parent, key)) { transplant(tree, y, y->right); y->right = key->right; y->right->parent = y; } transplant(tree, key, y); y->left = key->left; y->left->parent = y; } --(tree->size); return key; }
/* since this operation may change the root of this tree, the parameter is a pointer to the structure searchTree, rather than a pointer of the root node. * insert an node into tree t, return 0 if SUCCESS, -1 on error. */ void tree_delete(searchTree_t *t, tnode_t *node) { tnode_t *y; if(node->lchild == NULL) transplant(t,node,node->rchild); else if(node->rchild == NULL) transplant(t,node,node->lchild); else { y = tree_min(node->rchild); if(y->parent != node) { transplant(t,y,y->rchild); y->rchild = node->rchild; y->rchild->parent = y; } transplant(t,node,y); y->lchild = node->lchild; y->lchild->parent = y; } }
//红黑树删除操作 void RBTree::TreeDelete(int key) { Node* z = &TreeSearch(key); Node* y = z; Node* x; bool y_original_colour = y->getColour(); if (z->getLeft() == NIL) { x = z->getRight(); transplant(z, z->getRight()); } else if (z->getRight() == NIL) { x = z->getLeft(); transplant(z, z->getLeft()); } else { y = &(TreeMinimum(z->getRight(), 0)); y_original_colour = y->getColour(); x = y->getRight(); if (y->getParent() == z) x->setParent(y); else { transplant(y, y->getRight()); y->setRight(z->getRight()); y->getRight()->setParent(y); } transplant(z, y); y->setLeft(z->getLeft()); y->getLeft()->setParent(y); y->setColour(z->getColour()); } if (y_original_colour == BLACK) deleteFixup(x); delete z; }
void remove(const Key &key) { auto node = find(key); if (key) { if (!node->left) { transplant(node, node->right); } else if (!node->right) { transplant(node, node->left); } else { auto y = node->right->minimum(); if (y->parent != node) { transplant(y, y->right); y->right = node->right; y->right->parent = y; } transplant(node, y); y->left = node->left; y->left->parent = y; } } }
void rb_tree_delete (RBTREE *T , NODE *z) { NODE *y = z; NODE *x; int y_original_color = y->color; if (z->left == T->nil) { x = z->right; transplant (T , z , z->right); free (z); }else if (z->right == T->nil) { x = z->right; transplant (T , z , z->left); free (z); }else { y = minimum_rb_tree (T , z->right); y_original_color = y->color; x = y->right; if (y->parent == z) { x->parent = y; transplant (T , z , y); y->left = z->left; z->left->parent = y; y->color = z->color; free (z); }else { transplant (T , y , y->right); y->right = z->right; y->right->parent = y; transplant (T , z , y); y->left = z->left; y->left->parent = y; y->color = z->color; free (z); } } if (y_original_color == BLACK) rb_tree_delete_fixup (T , x); }
Node * Tree::delete_node(int key) { Node * z = search(key); if (z->left == nullptr) { transplant(z, z->right); } else if (z->right == nullptr) { transplant(z, z->left); } else { // two children Node * y = z->right->minimum(); // if y is not z successor, do extra stuff here if (y->parent != z) { transplant(y, y->right); z->right->parent = y; y->right = z->right; } transplant(z, y); y->left = z->left; y->left->parent = y; } z->unlink(); return z; }
void treeDelete(struct node **root, struct node *target) { struct node *y; if (target->left == NULL) transplant(root, target, target->right); else if (target->right == NULL) transplant(root, target, target->left); else { y = treeMinimum(target->right); if (y->p != target) { transplant(root, y, y->right); y->right = target->right; y->right->p = y; } transplant(root, target, y); y->left = target->left; y->left->p = y; } }
int tree_delete(bstnode **pphead, bstnode *pdel) { assert(pphead && pdel); if (NULL == pdel->pleft) { transplant(pphead, pdel, pdel->pright); } else if (NULL == pdel->pright) { transplant(pphead, pdel, pdel->pleft); } else { bstnode *py = NULL; // py is the successor of pdel. tree_minimum(pdel->pright, &py); if (py != pdel->pright) { transplant(pphead, py, py->pright); py->pright = pdel->pright; pdel->pright->pparent = py; } transplant(pphead, pdel, py); py->pleft = pdel->pleft; pdel->pleft->pparent = py; } free(pdel); return 0; }
/** * Method remove * Removes a node based in the provided key, * and rebalances the tree if necessary. * @parameter key * @see Cormen, Leiserson, Rivest and Stein - Introduction to * Algorithms, 3rd Edition, page 298 */ void AVLTree::remove(int key) { node *x = root; while (x) { if (key < x->key) { x = x->leftChild; } else if (key > x->key) { x = x->rightChild; } else { break; } } if (x && x->key == key) { node *toRebalance; //Case a: no left child if (!x->leftChild) { transplant(x, x->rightChild); if (x->rightChild) { toRebalance = x->rightChild; } else { //hack! since the rebalance method immediately //points to the parent of the given node, this works //well, particularly when the unbalanced node is the //root (x parent) toRebalance = x; } } //Case b: no right child else if (!x->rightChild) { transplant(x, x->leftChild); if (x->leftChild) { toRebalance = x->leftChild; } else { //hack! since the rebalance method immediately //points to the parent of the given node, this works //well, particularly when the unbalanced node is the //root (x parent) toRebalance = x; } } //Cases c and d: else { node *y = minimum(x->rightChild); //Case d specific: if (y->parent != x) { transplant(y, y->rightChild); y->rightChild = x->rightChild; y->rightChild->parent = y; toRebalance = minimum(y->rightChild); } else { toRebalance = x->leftChild; } transplant(x, y); y->leftChild = x->leftChild; y->leftChild->parent = y; } delete x; rebalance(toRebalance); size -= 1; } }
void deleteFromRBTree(Tree *tree, Node *node) { if (node == tree->nil) return; int node_original_color = node->color; Node *changeNode = node; Node *n = node; while (n->parent != tree->nil) { n = n->parent; n->size--; } if (node->left == tree->nil) { changeNode = node->right; transplant(tree, node, node->right); } else if (node->right == tree->nil) { changeNode = node->left; transplant(tree, node, node->left); } else { Tree t; t.root = node->right; t.nil = tree->nil; Node *min = RBMinimum(&t); Node *end = min->right; node_original_color = min->color; changeNode = end; min->size = node->size - 1; if (node == tree->root) tree->root = min; if (node->right != min) { RBTransplant(tree, min, min->right); min->right = node->right; node->right->parent = min; } min->left = node->left; node->left->parent = min; RBTransplant(tree, node, min); n = min->right; while (n != end) { n->size--; n = n->left; } min->color = node->color; } free(node); if (node_original_color == BLACK) { RBDeleteFixup(tree, changeNode); } }
bool rbDelete(int key) { Node* z = search(root, key); if (z == NIL) { return false; } Node* y = z; Node* x; bool original = y->color; if (z->left == NIL) { x = z->right; transplant(z, z->right); } else { if (z->right == NIL) { x = z->left; transplant(z, z->left); } else { y = minimum(z->right); original = y->color; x = y->right; if (y->parent == z) { x->parent = y; } else { transplant(y, y->right); y->right = z->right; y->right->parent = y; } transplant(z, y); y->left = z->left; y->left->parent = y; y->color = z->color; } } if (original == false) { while ((x != root) && (x->color == false)) { if (x == x->parent->left) { Node* w = x->parent->right; if (w->color == true) { w->color = false; x->parent->color = true; leftRotate(x->parent); w = x->parent->right; } if ((w->left->color == false) && (w->right->color == false)) { w->color = true; x = x->parent; } else { if (w->right->color == false) { w->left->color = false; w->color = true; rightRotate(w); w = x->parent->right; } w->color = x->parent->color; x->parent->color = false; w->right->color = false; leftRotate(x->parent); x = root; } } else { Node* w = x->parent->left; if (w->color == true) { w->color = false; x->parent->color = true; rightRotate(x->parent); w = x->parent->left; } if ((w->right->color == false) && (w->left->color == false)) { w->color = true; x = x->parent; } else { if (w->left->color == false) { w->right->color = false; w->color = true; leftRotate(w); w = x->parent->left; } w->color = x->parent->color; x->parent->color = false; w->left->color = false; rightRotate(x->parent); x = root; } } } } delete z; return true; }