daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest May 17th, 2018 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template <class bstdata>
  2. void BST<bstdata>::remove(bstdata data)
  3. {
  4.     assert(!isEmpty());
  5.     removeHelper(root, data);
  6. }
  7. template <class bstdata>
  8. BST::Node* BST<bstdata>::removeHelper(BST::Node* root, bstdata data)
  9. {
  10.     if (root == NULL)
  11.     {
  12.         return root;
  13.     }
  14.     else if (data < root->data)
  15.     {
  16.         root->left = removeHelper(root->left,data);
  17.     }
  18.     else if (data > root->data)
  19.     {
  20.         root->right =removeHelper(root->right,data);
  21.     }
  22.     else
  23.     {
  24.         if(root->left == NULL && root->right == NULL)
  25.         {
  26.             delete root;
  27.             root = NULL;
  28.         }
  29.         else if(root->right == NULL && root->left != NULL)
  30.         {
  31.             root->left = root; //not sure if this is correct or if
  32.             delete root;
  33.         }
  34.         else if(root->right != NULL && root->left == NULL)
  35.         {
  36.             root->right = root;
  37.             delete root;
  38.         }
  39.         else
  40.         {
  41.             int i = root->right.minimum();
  42.             root->data = i;
  43.             root->right = removeHelper(root->right, i);
  44.         }
  45.         return root;
  46.     }
  47. }
RAW Paste Data
Top