Interval * IntervalTree::DeleteNode(IntervalTreeNode * z){ IntervalTreeNode* y; IntervalTreeNode* x; Interval * returnValue = z->storedInterval; y= ((z->left == nil) || (z->right == nil)) ? z : GetSuccessorOf(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!=nil),"y is nil in DeleteNode \n"); #endif /* y is the node to splice out and x is its child */ y->maxHigh = MIN_INT; 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; #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS CheckAssumptions(); #elif defined(DEBUG_ASSERT) Assert(!nil->red,"nil not black in ITDelete"); Assert((nil->maxHigh=MIN_INT),"nil->maxHigh != MIN_INT in ITDelete"); #endif } else { FixUpMaxHigh(x->parent); if (!(y->red)) DeleteFixUp(x); delete y; #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS CheckAssumptions(); #elif defined(DEBUG_ASSERT) Assert(!nil->red,"nil not black in ITDelete"); Assert((nil->maxHigh=MIN_INT),"nil->maxHigh != MIN_INT in ITDelete"); #endif } return returnValue; }
rb_node* RB::DeleteNode(int data) { rb_node *position = find_node(data); rb_node *y = 0, *x = 0; if (!position) { cout << "Not Found" << endl; return 0; } if (position->left == 0 || position->right == 0) y = position; else y = TreeSuccessor(position); if (!y->left) x = y->right; else if (!y->right) x = y->left; if (!y->parent) root = x; else if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; if (x) x->parent = y->parent; if (position != y) { position->data = y->data; } if (y->color == rb_black) DeleteFixUp(x); return y; }
bool RBTree<KEY>::Delete(const KEY& key) { RBNode* pDelete = find(m_root,key); if(pDelete == m_null) return false; RBNode* pSuccessor; if(pDelete->left == m_null ) pSuccessor = pDelete->right; else if(pDelete->right == m_null) pSuccessor = pDelete->left; else { RBNode* pMin = findMin(pDelete->right); pDelete->key = pMin->key; pDelete = pMin; pSuccessor = pMin->right; } pSuccessor->parent = pDelete->parent; if(pDelete == m_root) m_root = pSuccessor; else { if(pDelete == pDelete->parent->left) pDelete->parent->left = pSuccessor; else pDelete->parent->right = pSuccessor; } if(pDelete->color == BLACK) { DeleteFixUp(pSuccessor); } delete pDelete; return true; }