SimpleInterval* IntervalTree::deleteNode(IntervalTreeNode* z) { IntervalTreeNode* y; IntervalTreeNode* x; SimpleInterval* node_to_delete = z->stored_interval; y= ((z->left == nil) || (z->right == nil)) ? z : getSuccessor(z); 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; } } /// @brief y should not be nil in this case /// y is the node to splice out and x is its child if(y != z) { y->max_high = -std::numeric_limits<double>::max(); y->left = z->left; y->right = z->right; y->parent = z->parent; z->left->parent = z->right->parent = y; if(z == z->parent->left) z->parent->left = y; else z->parent->right = y; fixupMaxHigh(x->parent); if(!(y->red)) { y->red = z->red; deleteFixup(x); } else y->red = z->red; delete z; } else { fixupMaxHigh(x->parent); if(!(y->red)) deleteFixup(x); delete y; } return node_to_delete; }
void RBTree :: RBdelete( NodePtr z ) { NodePtr x ; NodePtr y = z ; char y_original_color = y->color ; if(z->left == nil){ x = z->right; RBTransplant(z, z->right); } else if(z->right == nil){ x = z->left; RBTransplant(z, z->left); } else{ y = Tree_minimum(z->right); y_original_color = y->color; x = y->right; if(y->p == z){ x->p = y; }else{ RBTransplant(y, y->right); y->right = z->right; y->right->p = y; } RBTransplant(z,y); y->left = z->left; y->left->p = y; y->color = z->color; } if ( y_original_color == 'B' ) deleteFixup( x ) ; }
void rbt_delete(rbt_node *z, rbt_node **root) { rbt_node *x, *y; /***************************** * delete node z from tree * *****************************/ if (!z || z == NIL) return; if (z->left == NIL || z->right == NIL) { /* y has a NIL node as a child */ y = z; } else { /* find tree successor with a NIL node as a child */ y = z->right; while (y->left != NIL) y = y->left; } /* x is y's only child */ if (y->left != NIL) x = y->left; else x = y->right; /* remove y from the parent chain */ x->parent = y->parent; if (y->parent) if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; else { *root = x; } if (y != z) { z->key = y->key; //CHANGE z->associated_LRU_elem = y->associated_LRU_elem; } if (y->color == BLACK) deleteFixup (x, root); y->key = NULL; y->associated_LRU_elem = NULL; y->left = NULL; y->right = NULL; y->parent = NULL; free (y); }
void deleteRBNode(ppRBNode root, int val) { // If z is the node to be deleted, then the original color of z's successor is: original_color char original_color; // 1. Perform standard binary-search-tree deletion pRBNode x = BSTdelete(*root, val, &original_color); if(x == NULL) return ; // 2. Fix the RB-tree if necessary if(original_color == 'B') deleteFixup(root, x); }
bool RedBlackTree<Key, Data>::killNode(RedBlackNode<Key, Data> * z) { RedBlackNode<Key, Data> *x, *y; if (!valid(z->left) || !valid(z->right)) { /* y has a null node as a child */ y = z; } else { /* find tree successor with a null node as a child */ y = z->right; while (valid(y->left)) y = y->left; } /* x is y's only child */ if (valid(y->left)) x = y->left; else x = y->right; /* remove y from the parent chain */ if (valid(x)) x->parent = y->parent; if (valid(y->parent)) { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } else rootNode = x; if (y != z) { Dealloc(z->id); z->id = Duplicate(y->id); z->data = y->data; } else { Dealloc(y->id); } if (y->color == BLACK) deleteFixup(x); m_cachedSize--; y->left = NULL; y->right = NULL; delete y; return true; }
// delete node RbtStatus rbtErase(RBTreeType *tree, NodeType * z) { NodeType **root = &tree->root; NodeType *x, *y; if (z->left == SENTINEL || z->right == SENTINEL) { // y has a SENTINEL node as a child y = z; } else { // find tree successor with a SENTINEL node as a child y = z->right; while (y->left != SENTINEL) y = y->left; } // x is y's only child if (y->left != SENTINEL) x = y->left; else x = y->right; // remove y from the parent chain x->parent = y->parent; if (y->parent) if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; else *root = x; if (y != z) { z->key = y->key; z->val = y->val; } if (y->color == BLACK) deleteFixup (root, x, SENTINEL); free (y); return RBT_STATUS_OK; }
void deleteNode(Node *z) { Node *x, *y; /***************************** * delete node z from tree * *****************************/ if (!z || z == NIL) return; if (z->left == NIL || z->right == NIL) { /* y has a NIL node as a child */ y = z; } else { /* find tree successor with a NIL node as a child */ y = z->right; while (y->left != NIL) y = y->left; } /* x is y's only child */ if (y->left != NIL) x = y->left; else x = y->right; /* remove y from the parent chain */ x->parent = y->parent; if (y->parent) if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; else root = x; if (y != z) z->data = y->data; if (y->color == BLACK) deleteFixup (x); free (y); }
RbtStatus rbtErase(RbtHandle h, RbtIterator i) { NodeType *x, *y; RbtType *rbt = h; NodeType *z = i; if (z->left == SENTINEL || z->right == SENTINEL) { /* y has a SENTINEL node as a child */ y = z; } else { /* find tree successor with a SENTINEL node as a child */ y = z->right; while (y->left != SENTINEL) y = y->left; } /* x is y's only child */ if (y->left != SENTINEL) x = y->left; else x = y->right; /* remove y from the parent chain */ x->parent = y->parent; if (y->parent) if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; else rbt->root = x; if (y != z) { z->key = y->key; z->val = y->val; } if (y->color == BLACK) deleteFixup (rbt, x); xfree(y); return RBT_STATUS_OK; }
void deleteNode(RBTree *tree, Node *z) { Node *x, *y; if (!z || z == NIL) return; if (z->left == NIL || z->right == NIL) { /* y has a NIL node as a child */ y = z; } else { /* find tree successor with a NIL node as a child */ y = z->right; while (y->left != NIL) y = y->left; } /* x is y's only child */ if (y->left != NIL) x = y->left; else x = y->right; /* remove y from the parent chain */ x->parent = y->parent; if (y->parent) if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; else tree->root = x; /* free data pointed to by the node */ freeRBdata(z->data); if (y != z) z->data = y->data; if (y->color == BLACK) deleteFixup (tree, x); free(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; }
//----------------------------------------------------------------------------- /// detach node from tree /// \param[in] z -- the node to detach /// \param[in] p_root - the root of the tree //----------------------------------------------------------------------------- void detachNode(Node *z, Node**p_root) { Node *x, *y, *x_par; int f; if(!*p_root)return; if (!z || z == NIL) return; if (z->left == NIL || z->right == NIL) { /* y has a NIL node as a child */ y = z; } else { /* find tree successor with a NIL node as a child */ y = z->right; while (y->left != NIL) y = y->left; } /* x is y's only child */ if (y->left != NIL) x = y->left; else x = y->right; /* remove y from the parent chain */ x_par = y->parent; if(x!=NIL) x->parent = x_par; if (y->parent) if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; else *p_root = x; f = (y->color == BLACK); if (y != z) { /* replace z with y */ y->color = z->color ; y->left = z->left; y->right = z->right; y->parent = z->parent; if (y->parent) if (z == y->parent->left) y->parent->left = y; else y->parent->right = y; else *p_root = y; if(y->left != NIL) y->left->parent = y; if(y->right != NIL) y->right->parent = y; if(x_par==z) x_par=y; } if(f) deleteFixup (x, x_par, p_root); }
int deleteSymbol(char * key) { SYMBOL *x, *y, *z; int color, c; /* find node in tree */ z = (root == NULL) ? NIL_SYM : root; while(z != NIL_SYM) { if( ((c = str2cmp(key, z->name)) == 0) && ((c = strcmp(key, z->name)) == 0) ) break; else z = (c < 0) ? z->left : z->right; } if (z == NIL_SYM) return(0); /* key to delete not found */ if (z->left == NIL_SYM || z->right == NIL_SYM) { /* y has a NIL_SYM node as a child */ y = z; } else { /* find tree successor with a NIL_SYM node as a child */ y = z->right; while (y->left != NIL_SYM) y = y->left; } /* x is y's only child */ if (y->left != NIL_SYM) x = y->left; else x = y->right; /* remove y from the parent chain */ x->parent = y->parent; if (y->parent) { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } else root = x; color = y->color; if (y != z) { /* swap y and z */ y->left = z->left; y->right = z->right; y->parent = z->parent; if(z->parent) { if(z->parent->left == z) z->parent->left = y; else z->parent->right = y; } else root = y; y->right->parent = y; y->left->parent = y; y->color = z->color; } if (color == BLACK) deleteFixup (x); --symbolCount; return TRUE; }