Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. void remove(const key_type &key) {
  2.             Node *temp = findNode(key);
  3.             Node *parent = temp->getParent();
  4.             Node *leftChild = temp->getLeftChild();
  5.             Node *rightChild = temp->getRightChild();
  6.  
  7.             if (root == nullptr) throw std::out_of_range("");
  8.  
  9.             // if node has no children
  10.             if (!temp->hasChildren()) {
  11.                 if (parent == nullptr) {
  12.                     root = nullptr;
  13.                 } else {
  14.                     if (temp == parent->getLeftChild()) {
  15.                         parent->setLeftChild(nullptr);
  16.                     } else {
  17.                         parent->setRightChild(nullptr);
  18.                     }
  19.                 }
  20.             } // has Left child only
  21.             else if (leftChild != nullptr && rightChild == nullptr) {
  22.                 if (parent == nullptr) {
  23.                     root = leftChild;
  24.                     root->setParent(nullptr);
  25.                 } else {
  26.                     if (temp == parent->getLeftChild()) {
  27.                         parent->setLeftChild(leftChild);
  28.                     } else {
  29.                         parent->setRightChild(leftChild);
  30.                     }
  31.                     leftChild->setParent(parent);
  32.                 }
  33.             } // has Right child only
  34.             else if (rightChild != nullptr && leftChild == nullptr) {
  35.                 if (parent == nullptr) {
  36.                     root = rightChild;
  37.                     root->setParent(nullptr);
  38.                 } else {
  39.                     if (temp == parent->getLeftChild()) {
  40.                         parent->setLeftChild(rightChild);
  41.                     } else {
  42.                         parent->setRightChild(rightChild);
  43.                     }
  44.                     rightChild->setParent(parent);
  45.                 }
  46.             } else {
  47.                 temp->setValue(rightChild->getValueType());
  48.                
  49.                 remove(rightChild);
  50.                
  51.                 --length;
  52.                 return;
  53.             }
  54.  
  55.             --length;
  56.             delete temp;
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement