erase draft implemented
This commit is contained in:
parent
2f323d8561
commit
3736ad2384
1 changed files with 43 additions and 5 deletions
46
Map.hpp
46
Map.hpp
|
@ -558,15 +558,53 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// TODO: erase via iterator
|
// TODO: check that the way of reconnecting next and prev works
|
||||||
void erase(Iterator pos) {
|
void erase(Iterator pos) {
|
||||||
// simple cases
|
// simple cases
|
||||||
Node *ref = pos.ref;
|
Node *ref = pos.ref;
|
||||||
|
// 2 children
|
||||||
// 2 children just copy over the in order successor and remove successor
|
if (ref->left != nullptr && ref->right != nullptr) {
|
||||||
|
Node *next = ref->next;
|
||||||
|
Node *prev = ref->prev;
|
||||||
|
*ref = *next;
|
||||||
|
prev->next = next;
|
||||||
|
next->prev = prev;
|
||||||
|
this->erase(Iterator{next});
|
||||||
|
}
|
||||||
|
// single child which is left
|
||||||
|
else if (ref->left != nullptr && ref->right == nullptr) {
|
||||||
|
Node *next = ref->next;
|
||||||
|
Node *prev = ref->prev;
|
||||||
|
*ref = *ref->left;
|
||||||
|
prev->next = next;
|
||||||
|
next->prev = prev;
|
||||||
|
}
|
||||||
|
// single child which is right
|
||||||
|
else if (ref->left == nullptr && ref->right != nullptr) {
|
||||||
|
Node *next = ref->next;
|
||||||
|
Node *prev = ref->prev;
|
||||||
|
*ref = *ref->right;
|
||||||
|
prev->next = next;
|
||||||
|
next->prev = prev;
|
||||||
|
}
|
||||||
|
// no children and root
|
||||||
|
else if (ref->left == nullptr && ref->right == nullptr) {
|
||||||
|
this->root = nullptr;
|
||||||
|
this->nodes.erase(ref->value);
|
||||||
|
}
|
||||||
|
// no children and red
|
||||||
|
else if (ref->left == nodes.end() && ref->right == nodes.end()) {
|
||||||
|
Node *next = ref->next;
|
||||||
|
Node *prev = ref->prev;
|
||||||
|
prev->next = next;
|
||||||
|
next->prev = prev;
|
||||||
|
this->nodes.erase(ref->value);
|
||||||
|
}
|
||||||
|
// complicated case of black node with no kids
|
||||||
|
else {
|
||||||
this->complex_erase(pos);
|
this->complex_erase(pos);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
void erase(const Key_T &key) { this->erase(this->find(key)); }
|
void erase(const Key_T &key) { this->erase(this->find(key)); }
|
||||||
void clear() {
|
void clear() {
|
||||||
this->root = nullptr;
|
this->root = nullptr;
|
||||||
|
|
Loading…
Reference in a new issue