erase draft implemented

This commit is contained in:
Pagwin 2024-11-21 18:49:40 -05:00
parent 2f323d8561
commit 3736ad2384
No known key found for this signature in database
GPG key ID: 81137023740CA260

46
Map.hpp
View file

@ -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;