简体   繁体   中英

Remove fcn for Binary Search Tree

Does this remove function for a Binary Search Tree look correct? When I try to delete a node, it runs and brings back the switch menu it's called from, but doesn't actually delete it when you reprint the tree. I'm not sure if I have a return out of place, possibly?

My question is: Does this remove statement actually remove the item passed, or does it just play around with it in a cruel fashion giving me a headache?

void BinarySearchTree::remove(int d)
    {
       //Locate the element
       bool found = false;
       if(isEmpty())
       {
          cout<<" This Tree is empty! "<<endl;
          return;
       }

       tree_node* curr;
       tree_node* parent;
       curr = root;

       while(curr != NULL)
       {
          if(curr->data == d)
          {
             found = true;
             break;
          }
          else
          {
             parent = curr;
             if(d>curr->data) curr = curr->right;
             else curr = curr->left;
          }
        }
        if(!found)
        {
          cout<<" Data not found! "<<endl;
          return;
        }


        // 3 cases :
        // 1. We're removing a leaf node
        // 2. We're removing a node with a single child
        // 3. we're removing a node with 2 children

        // Node with single child
            // Node with single child
if((curr->left == NULL && curr->right != NULL) || (curr->left != NULL && curr->right == NULL))
{
  if(curr->left == NULL && curr->right != NULL)
  {
     if(parent->left == curr)
     {
        parent->left = curr->right;
        delete curr;
     }
     else
     {
        parent->right = curr->right;
        delete curr;
     }
   }
   else // left child present, no right child
   {
     if(parent->left == curr)
     {
        parent->left = curr->left;
        delete curr;
     }
     else
     {
        parent->right = curr->left;
        delete curr;
     }
   }
   return;
}

        //We're looking at a leaf node
        if( curr->left == NULL && curr->right == NULL)
        {
          if(parent->left == curr)
          {
             parent->left = NULL;
          }
          else
          {
             parent->right = NULL;
          }
          delete curr;
          return;
        }


        //Node with 2 children
        // replace node with smallest value in right subtree
        if (curr->left != NULL && curr->right != NULL)
        {
           tree_node* chkr;
           chkr = curr->right;
           if((chkr->left == NULL) && (chkr->right == NULL))
           {
             curr = chkr;
             delete chkr;
             curr->right = NULL;
           }
           else // right child has children
           {
             //if the node's right child has a left child
             // Move all the way down left to locate smallest element

             if((curr->right)->left != NULL)
             {
                tree_node* lcurr;
                tree_node* lcurrp;
                lcurrp = curr->right;
                lcurr = (curr->right)->left;
                while(lcurr->left != NULL)
                {
                   lcurrp = lcurr;
                   lcurr = lcurr->left;
                }
                curr->data = lcurr->data;
                delete lcurr;
                lcurrp->left = NULL;
             }
             else
             {
                tree_node* tmp;
                tmp = curr->right;
                curr->data = tmp->data;
                curr->right = tmp->right;
                delete tmp;
             }

          }
          return;
       }

    }

I read ur code and I write different situations on a paper and I think in that case which the node has 2 children the code should be something like this

 //Node with 2 children
    if (curr->left != NULL && curr->right != NULL)
    {
        tree_node* chkr; 
        if(parent==NULL || parent->left==curr){  //if parent==NULL it means that the node that we want to delete is root
            chkr=curr->right;
            while(chkr->left!=NULL)
                chkr=chkr->left;
            if(parent!=NULL)
                parent->left=curr->right;
            else
                root=curr->right;
            chkr->left=curr->left;
            curr->left=curr->right=NULL;
            delete curr;
        }else if(parent->right==curr){
            chkr=curr->left;
            while(chkr->right!=NULL)
                chkr=chkr->right;
            parent->right=curr->left;
            chkr->right=curr->right;
            curr->left=curr->right=NULL;
            delete curr;
        }
        return;
   }

I didn't compile or test it but I think its gonna work, plz let me know about it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM