static HRESULT RBDelete(std_map* self, std_map_node* z) { std_map_node* y; std_map_node* x; std_map_node* nil=self->m_pNil; std_map_node* root=self->m_pRoot; y= ((z->pLeft == nil) || (z->pRight == nil)) ? z : TreeSuccessor(self,z); x= (y->pLeft == nil) ? y->pRight : y->pLeft; if (root == (x->pParent = y->pParent)) { /* assignment of y->p to x->p is intentional */ root->pLeft=x; } else { if (y == y->pParent->pLeft) { y->pParent->pLeft=x; } else { y->pParent->pRight=x; } } if (y != z) { /* y should not be nil in this case */ /* y is the node to splice out and x is its child */ if (!(y->bRed)) RBDeleteFixUp(self,x); KeyDtor(self, z->pKey); DataDtor(self, z->pData); y->pLeft=z->pLeft; y->pRight=z->pRight; y->pParent=z->pParent; y->bRed=z->bRed; z->pLeft->pParent=z->pRight->pParent=y; if (z == z->pParent->pLeft) { z->pParent->pLeft=y; } else { z->pParent->pRight=y; } GaloisFree(z); } else { KeyDtor(self, y->pKey); DataDtor(self, y->pData); if (!(y->bRed)) RBDeleteFixUp(self,x); GaloisFree(y); } return S_OK; }
void rb_tree_delete(rb_red_blk_tree* tree, rb_red_blk_node* z){ rb_red_blk_node* y; rb_red_blk_node* x; rb_red_blk_node* nil=tree->nil; rb_red_blk_node* root=tree->root; y= ((z->left == nil) || (z->right == nil)) ? z : rb_tree_successor(tree,z); x= (y->left == nil) ? y->right : y->left; if (root == (x->parent = y->parent)) { /* assignment of y->p to x->p is intentional */ root->left=x; } else { if (y == y->parent->left) { y->parent->left=x; } else { y->parent->right=x; } } if (y != z) { /* y should not be nil in this case */ #ifdef DEBUG_ASSERT Assert( (y!=tree->nil),"y is nil in RBDelete\n"); #endif /* y is the node to splice out and x is its child */ if (!(y->red)) RBDeleteFixUp(tree,x); tree->key_dtor_func(z->key); tree->info_dtor_func(z->info); y->left=z->left; y->right=z->right; y->parent=z->parent; y->red=z->red; z->left->parent=z->right->parent=y; if (z == z->parent->left) { z->parent->left=y; } else { z->parent->right=y; } free(z); } else { tree->key_dtor_func(y->key); tree->info_dtor_func(y->info); if (!(y->red)) RBDeleteFixUp(tree,x); free(y); } #ifdef DEBUG_ASSERT Assert(!tree->nil->red,"nil not black in RBDelete"); #endif }
void RBTree::RBDelete(int key) { rb_red_blk_node* y; rb_red_blk_node* x; rb_red_blk_node* nil=tree->nil; rb_red_blk_node* root=tree->root; rb_red_blk_node* z = RBExactQuery(key); if (!z) { return; } y= ((z->left == nil) || (z->right == nil)) ? z : TreeSuccessor(z); /* XXX: original code was : x= (y->left == nil) ? y->right : y->left; */ x= (y->left > nil) ? y->right : y->left; if (root == (x->parent = y->parent)) { root->left=x; } else { if (y == y->parent->left) { y->parent->left=x; } else { y->parent->right=x; } } if (y != z) { if (!(y->red)) RBDeleteFixUp(x); y->left=z->left; y->right=z->right; y->parent=z->parent; y->red=z->red; z->left->parent=z->right->parent=y; if (z == z->parent->left) { z->parent->left=y; } else { z->parent->right=y; } free(z); } else { if (!(y->red)) RBDeleteFixUp(x); free(y); } }
/* * This function is used to delete a node z from the tree * specified by root * * @param Root reference to the main tree * @param z node to delete to the main tree * @return N/A */ void RBDelete(RBTNode **Root, RBTNode *z) { RBTNode *yNode; RBTNode *xNode; RBTNode *parentNode; BOOL leftNode = false; //if the node has less than two children, we can use it. //otherwise we have to find the successor if (z->leftChild == NULL || z->rightChild == NULL) { yNode = z; } else { yNode = Successor(z); } //if y has a left child, use it //otherwise just assign right child even if its NULL if (yNode->leftChild != NULL) { xNode = yNode->leftChild; } else { xNode = yNode->rightChild; } //assign xNode's parent as yNode if not NULL if (xNode != NULL) { xNode->parent = yNode->parent; } parentNode = yNode->parent; //if parent is false, we have the root if (yNode->parent == NULL) { *Root = xNode; } //if y's parent leftChild, then we have the left child //to delete else if (yNode == yNode->parent->leftChild) { yNode->parent->leftChild = xNode; leftNode = TRUE; } else { yNode->parent->rightChild = xNode; leftNode = FALSE; } //make sure y and z are matching before the call //to fixup if (yNode != z) { z->key = yNode->key; } //if the node we deleted was black, call fix up. //for a red node it doesnt matter if (yNode->color == BLACK) { RBDeleteFixUp(Root, xNode, parentNode, leftNode); } }
bool RedBlackTree<T>::Remove(T item) { Node<T>* x = NULL; Node<T>* y = NULL; Node<T>* z = getNodeFromTree(root, item); // The node to be removed (it's value // will be gone, and it is going to be replaced // by the predecessor's value if a predecessor exists // for this node, and the predecessor's node will be // deleted, or delete this node if no predecessor exists). if (z == NULL) { // If no such item was found in the tree, then return false. return false; } if (z->left == NULL || z->right == NULL) { // If z has at most one child. y = z; } else { // z has two children. y = Predecessor(z); } if (y != NULL && y->left != NULL) { // The two conditions below are to // find whether is y's only child // is left or right. x = y->left; } else { if (y != NULL) { x = y->right; } } bool xIsLeft = false; Node<T>* xParent = NULL; if (x != NULL && y != NULL) { // If x is not NULL, detach x from y. x->p = y->p; xParent = y->p; } if (y != NULL && y->p == NULL) { // Check if y is root (i.e. it has no parent). root = x; } else { // Attach x to y's parent. if (y == y->p->left) { // y is a left child. y->p->left = x; xIsLeft = true; } else { // y is a right child. y->p->right = x; xIsLeft = false; } } if (y != NULL && z != NULL && y != z) { // Check to see if y has been moved up. z->data = y->data; } if (y != NULL && y->is_black == true) { if (x == NULL) { if (xParent == NULL) { if (root != NULL && root->right != NULL && root->left != NULL && CalculateHeight(root->right) > CalculateHeight(root->left)) { x = root->right; xParent = root; xIsLeft = false; } else if (root != NULL && root->right != NULL && root->left != NULL && CalculateHeight(root->left) > CalculateHeight(root->right)) { x = root->left; xParent = root; xIsLeft = true; } else if (root != NULL && root->left == NULL) { x = root->right; xParent = root; xIsLeft = false; } else if (root != NULL && root->right == NULL) { x = root->left; xParent = root; xIsLeft = true; } } } RBDeleteFixUp(x, xParent, xIsLeft); } delete y; // It can be the original predecessor's node, since its // value has been moved up, or it can be z itself. --size; // Decrement the size counter. if (root != NULL && root->left != NULL && root->right == NULL) { if (root->left->left == NULL && root->left->right == NULL) { root->is_black = true; root->left->is_black = false; } } else if (root != NULL && root->right != NULL && root->left == NULL) { if (root->right->left == NULL && root->right->right == NULL) { root->is_black = true; root->right->is_black = false; } } return true; }
bool RedBlackTree<T>::Remove(T item) { //if item not found return false if (Search(item) == false) { return false; } //Find the node z that has the value item Node<T>*z = root; while (z != NULL) { if (item == z->data) { break; } else if (item < z->data) { z = z->left; } else { z = z->right; } } //item has at most one child Node<T>* y; Node<T>* x; if (z->left == NULL || z->right == NULL) { y = z; } else { y = Predecessor(z); } if (y->left != NULL) { x = y->left; } else { x = y->right; } //x->p = y->p; cant do this because x could be NULL //check if y is root, replace it by x if (y->p == NULL) { root = x; } else { if (y == y->p->left) { y->p->left = x; } else { y->p->right = x; } //need to check x is not null ptr if (x != NULL) { x->p = y->p; } } if (y != z) { z->data = y->data; } if (y->is_black == true) { if (x == y->p->left) { RBDeleteFixUp(x, y->p, true); } //x is rightchold else { RBDeleteFixUp(x, y->p, false); } } delete y; size--; return true; }