LLRB RInsert
Node * RInsert(Node* nptr, const T& tval)
{
if (nptr == 0) // insert new node a bottom
{
return NewNode(tval, RED);
}
// enforce Height Limiting Property on way down
if (nptr->lchild_ && nptr->lchild_->IsRed())
if (nptr->lchild_->lchild_ && nptr->lchild_->lchild_->IsRed())
{
nptr = RotateRight(nptr);
nptr->lchild_->SetBlack();
}
if (pred_(tval,nptr->value_)) // go left
{
nptr->lchild_ = RInsert(nptr->lchild_, tval);
}
else if (pred_(nptr->value_,tval)) // go right
{
nptr->rchild_ = RInsert(nptr->rchild_, tval);
}
else // found
{
nptr->value_ = tval;
}
// enforce Left-Leaning Property on way up
if (nptr->rchild_ && nptr->rchild_->IsRed())
{
nptr = RotateLeft(nptr);
if (nptr->lchild_->IsBlack())
{
nptr->SetBlack();
nptr->lchild_->SetRed();
}
}
return nptr;
}