简体   繁体   中英

Binary Search Tree Remove Function setting node to zero instead of deleting

I'm having trouble figuring out why my remove function for my tree is "somewhat" removing the nodes. When I print out the results, the "deleted" node appears as zero.

For example: Add nodes 7,3,9 and then delete node 9

Input: add 7,3,9 delete 3

Output: 0,7,9

void Tree::remove(int val) {
Node* temp;
if (root == nullptr)
    {return;}
if (val == root->val)
{
   if (root->left == nullptr && root->right == nullptr){ //leaf node 
       delete root;
      }


   else{
        temp = root;
        if (root->left != nullptr && root->right != nullptr){ //left and right children exist

            if (root->left->val - val <= root->right->val - val){//left child is closer to value than right
                int val_to_save = root->left->val;
                root = root->left;
                remove(val_to_save);
                temp->val = val_to_save;//replace value with deleted node
                root = temp;}

            else{
                int val_to_save = root->right->val;
                root = root->right;
                std::cout << val_to_save << std::endl;
                remove(val_to_save);
                temp->val = val_to_save;//replace value with deleted node
                root = temp;}
        }   

       else{ // only one child, either left or right
           if(root->left != nullptr){ //left child
               temp->left = root->left;
               delete temp;}

           else{ //right child
               temp->right = root->right;
               delete temp;}

       }
   }        
}

else{ //value does not match
    temp = root;
    if (val < root->val)
        {
         temp = temp->left;
         remove(val);
         root = temp;
         }

    else{
         root = root->right; 
         remove(val);
         root = temp;}                
    }
}

When you call delete in c++ you're deallocating the memory for that given object. However, you're not actually "undeclaring" it. For example, if you delete root it still knows that root is a node. Same as if you were to delete temp. So it seems like you're successfully deleting it, removing its value and deallocating its memory. However, the object is still there but instantiated to null or in byte form a binary 0 ('\\0'). When it prints a \\0, the result is shown as a regular 0.

I'm assuming you want your answer to print as 7, 9 correct? If interpreted your question wrong let me know.

Best

Consider there is 3 node,

[0][7][node3]-><-[node7][3][node9]-><-[node3][9][0]

and we want to delete the node 3,

void Tree::remove(int val) 
{
    Node* root = head; // head is head node 
    if (root)
    { 
        if (val == root->val)
        {
            if(root->left)
                root->left->right = root->right; // making the right of 7 node as the right of 3 node,
            if(root->right)
                root->right->left = root->left; //making the left of 9 node as the left of 3 node,
            delete root;
            root = 0;
        }
    }
}
 when we are deleting 7, if(root->right) will execute only, so the 0 (value of 7node->left) is assigned to 3node->left, when we are deleting 3, if(root->left) will execute , so the node9 (value of 3node->left) is assigned to 3node->left, when we are deleting 3, if(root->right) will execute , so the node7 (value of 3node->left) is assigned to 9node->left, when we are deleting 7, if(root->left) will execute only, so the 0 (value of 9node->right) is assigned to 3node->right, 

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