// 有两个子节点, 会先尝试替换前驱和后继,在判断是否需要调整 if (node->r_node != nullptrand node->l_node != nullptr) { auto prev_node = [&]() -> details::RBPtr<K, V> { for (auto i = node->l_node; i != nullptr; i = i->r_node) { if (i->r_node == nullptr) return i; } returnnullptr; }(); auto suf_node = [&]() -> details::RBPtr<K, V> { for (auto i = node->r_node; i != nullptr; i = i->l_node) { if (i->l_node == nullptr) return i; } returnnullptr; }();
auto swap_node = [&](details::RBPtr<K, V> &left, details::RBPtr<K, V> &right) { left.swap(right); swap_color(left, right); if (left->r_node != nullptr) left->r_node->f_node = left; if (left->l_node != nullptr) left->l_node->f_node = left; if (right->r_node != nullptr) right->r_node->f_node = right; if (right->l_node != nullptr) right->l_node->f_node = right; }; // 被删除的节点是红色 if ((prev_node->color == details::Color::RED or suf_node->color == details::Color::RED) and node->color == details::Color::RED) { swap_node(node, (prev_node->color == details::Color::RED ? prev_node : suf_node)); switch_remove(node); }
// 需要调整的情况 swap_node(node, prev_node); }
// 下面是调整的步骤 auto sibling = get_sibling(node); auto close_nephew = get_close_nephew(node); auto distant_nephw = get_distant_nephew(node); father = get_father(node);
auto remove_node = [&](details::RBPtr<K, V> &d_node) { assert(d_node->l_node == nullptror d_node->r_node == nullptr); assert(d_node->f_node.lock() != nullptr); auto father = d_node->f_node.lock(); switch (d_node->attribute) { case details::Attribute::LEFT_CHILD: { father->l_node = change_node( father, (d_node->r_node == nullptr ? d_node->l_node : d_node->r_node), details::Attribute::LEFT_CHILD); return; } case details::Attribute::RIGHT_CHILD: { father->r_node = change_node( father, (d_node->r_node == nullptr ? d_node->l_node : d_node->r_node), details::Attribute::RIGHT_CHILD); return; } } }; // Case 1 if (all_exist and equal_color(details::Color::BLACK, father, close_nephew, distant_nephw) and equal_color(details::Color::RED, sibling)) { swap_color(sibling, father); switch (node->attribute) { case details::Attribute::LEFT_CHILD: { rotate_left(father); break; } case details::Attribute::RIGHT_CHILD: { rotate_right(father); break; } } if (op) remove_node(node); return; }
// Case 2 if (all_exist and equal_color(details::Color::BLACK, sibling, close_nephew, distant_nephw) and equal_color(details::Color::RED, father)) { swap_color(sibling, father); if (op) remove_node(node); return; }
// Case 3 并不需要所有节点都存在,侄子节点如果是黑色的,可以是空节点。 constbool case3_node_exist = (father != nullptrand sibling != nullptr); constbool case3_nephew = ((close_nephew == nullptrand distant_nephw == nullptr) or// 两个侄子节点都不存在, 也是为黑色 (close_nephew != nullptrand distant_nephw == nullptrand close_nephew->color == details::Color::BLACK) or (close_nephew == nullptrand distant_nephw != nullptrand distant_nephw->color == details::Color::BLACK) or (all_exist and equal_color(details::Color::BLACK, close_nephew, distant_nephw))); ; if (case3_node_exist and equal_color(details::Color::BLACK, father, sibling) and case3_nephew) { sibling->color = details::Color::RED; if (op) remove_node(node); switch_remove(father, false); return; }
// Case 4 if (all_exist and equal_color(details::Color::BLACK, sibling, distant_nephw) and equal_color(details::Color::RED, close_nephew)) { switch (node->attribute) { case details::Attribute::LEFT_CHILD: rotate_right(sibling); case details::Attribute::RIGHT_CHILD: rotate_left(sibling); } swap_color(sibling, close_nephew); }
// Case 5 sibling = get_sibling(node); distant_nephw = get_distant_nephew(node); father = get_father(node); constbool case5_node_exist = (father != nullptrand sibling != nullptrand distant_nephw != nullptr); if (case5_node_exist andequal_color(details::Color::BLACK, sibling) and equal_color(details::Color::RED, distant_nephw)) { switch (node->attribute) { case details::Attribute::LEFT_CHILD: { rotate_left(father); break; } case details::Attribute::RIGHT_CHILD: {