Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void remove(const key_type &key) {
- Node *temp = findNode(key);
- Node *parent = temp->getParent();
- Node *leftChild = temp->getLeftChild();
- Node *rightChild = temp->getRightChild();
- if (root == nullptr) throw std::out_of_range("");
- // if node has no children
- if (!temp->hasChildren()) {
- if (parent == nullptr) {
- root = nullptr;
- } else {
- if (temp == parent->getLeftChild()) {
- parent->setLeftChild(nullptr);
- } else {
- parent->setRightChild(nullptr);
- }
- }
- } // has Left child only
- else if (leftChild != nullptr && rightChild == nullptr) {
- if (parent == nullptr) {
- root = leftChild;
- root->setParent(nullptr);
- } else {
- if (temp == parent->getLeftChild()) {
- parent->setLeftChild(leftChild);
- } else {
- parent->setRightChild(leftChild);
- }
- leftChild->setParent(parent);
- }
- } // has Right child only
- else if (rightChild != nullptr && leftChild == nullptr) {
- if (parent == nullptr) {
- root = rightChild;
- root->setParent(nullptr);
- } else {
- if (temp == parent->getLeftChild()) {
- parent->setLeftChild(rightChild);
- } else {
- parent->setRightChild(rightChild);
- }
- rightChild->setParent(parent);
- }
- } else {
- temp->setValue(rightChild->getValueType());
- remove(rightChild);
- --length;
- return;
- }
- --length;
- delete temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement