removal from the tree is screwed idk if I'm getting this done tonight
This commit is contained in:
parent
f73c68a980
commit
3206bfbd39
1 changed files with 2 additions and 70 deletions
72
Map.hpp
72
Map.hpp
|
@ -661,76 +661,8 @@ private:
|
|||
parent->color = Color::Black;
|
||||
distant_nephew->color = Color::Black;
|
||||
}
|
||||
// heavily referring to
|
||||
// https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal_of_a_black_non-root_leaf
|
||||
void complex_erase(Iterator pos) {
|
||||
|
||||
Node *to_delete = &this->nodes[pos.ref.value()];
|
||||
Node *parent = to_delete->par();
|
||||
assert(parent != nullptr);
|
||||
|
||||
Direction dir =
|
||||
parent->r() == to_delete ? Direction::Right : Direction::Left;
|
||||
|
||||
Node *sibling;
|
||||
;
|
||||
Node *close_nephew;
|
||||
Node *distant_nephew;
|
||||
|
||||
parent->c_trans(dir, nullptr);
|
||||
|
||||
do {
|
||||
dir = parent->r() == to_delete ? Direction::Right : Direction::Left;
|
||||
|
||||
sibling = parent->child(!dir);
|
||||
distant_nephew = sibling->child(!dir);
|
||||
close_nephew = sibling->child(dir);
|
||||
|
||||
if (sibling->color == Color::Red) {
|
||||
// case 3
|
||||
parent->rotate(dir);
|
||||
parent->color = Color::Red;
|
||||
sibling->color = Color::Black;
|
||||
sibling = close_nephew;
|
||||
// redundant?
|
||||
distant_nephew = sibling->child(!dir);
|
||||
if (distant_nephew != nullptr && distant_nephew->color == Color::Red) {
|
||||
case6(parent, sibling, distant_nephew, dir);
|
||||
return;
|
||||
}
|
||||
close_nephew = sibling->child(dir);
|
||||
if (close_nephew != nullptr && close_nephew->color == Color::Red) {
|
||||
case5(parent, sibling, close_nephew, distant_nephew, dir);
|
||||
return;
|
||||
}
|
||||
sibling->color = Color::Red;
|
||||
parent->color = Color::Black;
|
||||
return;
|
||||
}
|
||||
|
||||
if (distant_nephew != nullptr && distant_nephew->color == Color::Red) {
|
||||
case6(parent, sibling, distant_nephew, dir);
|
||||
return;
|
||||
}
|
||||
|
||||
if (close_nephew != nullptr && close_nephew->color == Color::Red) {
|
||||
case5(parent, sibling, close_nephew, distant_nephew, dir);
|
||||
return;
|
||||
}
|
||||
|
||||
if (parent->color == Color::Red) {
|
||||
// case 4
|
||||
sibling->color = Color::Red;
|
||||
parent->color = Color::Black;
|
||||
return;
|
||||
}
|
||||
|
||||
// case 2
|
||||
sibling->color = Color::Red;
|
||||
to_delete = parent;
|
||||
parent = to_delete->par();
|
||||
} while (parent != nullptr);
|
||||
}
|
||||
// black leaf node with no kids
|
||||
void complex_erase(Iterator pos) {}
|
||||
|
||||
public:
|
||||
void erase(Iterator pos) {
|
||||
|
|
Loading…
Reference in a new issue