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; }
IntervalTreeNode * IntervalTree::Insert(Interval * newInterval) { IntervalTreeNode * y; IntervalTreeNode * x; IntervalTreeNode * newNode; x = new IntervalTreeNode(newInterval); TreeInsertHelp(x); FixUpMaxHigh(x->parent); newNode = x; x->red=1; while(x->parent->red) { /* use sentinel instead of checking for root */ if (x->parent == x->parent->parent->left) { y=x->parent->parent->right; if (y->red) { x->parent->red=0; y->red=0; x->parent->parent->red=1; x=x->parent->parent; } else { if (x == x->parent->right) { x=x->parent; LeftRotate(x); } x->parent->red=0; x->parent->parent->red=1; RightRotate(x->parent->parent); } } else { /* case for x->parent == x->parent->parent->right */ /* this part is just like the section above with */ /* left and right interchanged */ y=x->parent->parent->left; if (y->red) { x->parent->red=0; y->red=0; x->parent->parent->red=1; x=x->parent->parent; } else { if (x == x->parent->left) { x=x->parent; RightRotate(x); } x->parent->red=0; x->parent->parent->red=1; LeftRotate(x->parent->parent); } } } root->left->red=0; return(newNode); #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS CheckAssumptions(); #elif defined(DEBUG_ASSERT) Assert(!nil->red,"nil not red in ITTreeInsert"); Assert(!root->red,"root not red in ITTreeInsert"); Assert((nil->maxHigh=MIN_INT), "nil->maxHigh != MIN_INT in ITTreeInsert"); #endif }
IntervalTreeNode * IT_insert(IntervalTree *it, long low, long high, void *data) { IntervalTreeNode *x, *y, *newNode; x = ITN_create(low, high, data); TreeInsertHelp(it, x); FixUpMaxHigh(it, x->parent); newNode = x; x->red=1; while(x->parent->red) { /* use sentinel instead of checking for root */ if (x->parent == x->parent->parent->left) { y=x->parent->parent->right; if (y->red) { x->parent->red=0; y->red=0; x->parent->parent->red=1; x=x->parent->parent; } else { if (x == x->parent->right) { x=x->parent; LeftRotate(it, x); } x->parent->red=0; x->parent->parent->red=1; RightRotate(it, x->parent->parent); } } else { /* case for x->parent == x->parent->parent->right */ /* this part is just like the section above with */ /* left and right interchanged */ y=x->parent->parent->left; if (y->red) { x->parent->red=0; y->red=0; x->parent->parent->red=1; x=x->parent->parent; } else { if (x == x->parent->left) { x=x->parent; RightRotate(it, x); } x->parent->red=0; x->parent->parent->red=1; LeftRotate(it, x->parent->parent); } } } it->root->left->red=0; #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS IT_CheckAssumptions(it); #elif defined(DEBUG_ASSERT) Assert(!it->nil->red,"nil not red in ITTreeInsert"); Assert(!it->root->red,"root not red in ITTreeInsert"); Assert((it->nil->maxHigh=LONG_MIN), "nil->maxHigh != LONG_MIN in ITTreeInsert"); #endif return newNode; }