繁体   English   中英

二进制搜索树删除功能将节点设置为零而不是删除

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

我在弄清楚为什么我的树的删除功能为什么“有点”删除节点时遇到了麻烦。 当我打印出结果时,“已删除”节点显示为零。

例如:添加节点7,3,9,然后删除节点9

输入:添加7,3,9删除3

输出: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;}                
    }
}

在c ++中调用delete时,您正在为该给定对象分配内存。 但是,您实际上并没有“取消声明”它。 例如,如果删除根,它仍然知道根是一个节点。 与删除温度相同。 因此,似乎您已成功删除它,删除了它的值并重新分配了它的内存。 但是,该对象仍然存在,但实例化为null或以字节形式实例为二进制0('\\ 0')。 当它打印\\ 0时,结果显示为常规0。

我假设您希望答案打印为7、9正确? 如果您的问题解释错误,请告诉我。

最好

考虑有3个节点,

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

我们要删除节点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, 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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