Node *par = rt->parent;
bool c = par->isRightChild();
Node *brother = par->ch[c ^ 1];
/** * * B (B) * / \ / \ * (B) B -----> B R * / \ / \ * B B B B*/if (par->color == BLACK && brother->color == BLACK && brother->ch[0] == BLACK
&& brother->ch[1] == BLACK) {
brother->color = RED;
balance(par);
return;
}
/** * B1 B3 * / \ ----> / * (B)2 R3 R1 * / * (B)2 * * grantee that brother is not red in the following*/if (brother->color == RED) {
std::swap(brother->color, par->color);
rotate(brother);
}
/** * R (B) * / \ / \ * (B) B -----> B R * / \ / \ * B B B B*/assert(brother->color == BLACK);
brother = par->ch[c ^ 1];
if (par->color == RED && brother->color == BLACK && brother->ch[0] == BLACK
&& brother->ch[1] == BLACK) {
std::swap(par->color, brother->color);
balance(par);
return;
}