简体   繁体   English

在Visual Studio中删除二进制搜索树中的节点中的访问冲突异常

[英]Access Violation Exception in Visual Studio in Deletion of node in Binary Search Tree

I am getting Exception when running the BST Deletion . 运行BST Deletion时出现异常。 Below is my code snippet: 以下是我的代码段:

Bst::node * Bst::del(node *root, int num)
{
    if (root == NULL)
    {
        return root;
    }
    else if (num < root->data)
    {
        root->left = del(root->left, num);
    }
    else if (num > root->data)
    {
        root->right = del(root->right, num);
    }
    else
    {
        if (root->left == NULL)
        {
            node * tmp = root;
            root = root->right;
            delete tmp;
        }
        else if (root->right == NULL)
        {
            node * tmp = root;
            root = root->left;
            delete tmp;
        }
        else if (root->left == NULL && root->right == NULL)
        {
            delete root;
            root = NULL;
        }
        else
        {
            node *tmp = root;
            tmp = findMin(root->right);
            root->data = tmp->data;
            root->right = del(root->right, tmp->data);
        }
    }


    return root;
}

//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////////////

void Bst::del(int num)
{
    del(root, num);
}

Everything works fine when I am deleting the other nodes but when I delete the root node itself then the function void Bst::del(int num) gets the garbage value from the function Bst::node * Bst::del(node *root, int num) . 当我删除其他节点时,一切正常,但是当我删除根节点本身时,函数void Bst::del(int num)从函数Bst::node * Bst::del(node *root, int num) The error gets resolved when I rewrite my function as 当我将函数重写为时,错误得到解决

  void Bst::del(int num)
        {
            root = del(root, num);
        }

Question 1. Why it works when I delete the middle nodes or any other node except the root node. 问题1.为什么当我删除中间节点或除根节点之外的任何其他节点时,它可以工作。 While debugging I found that even root was getting deleted properly when the function Bst::node * Bst::del(node *root, int num) was executing but when the call returned to the void Bst::del(int num) then the value of root was not getting retained and was garbage. 在调试时,我发现当执行函数Bst::node * Bst::del(node *root, int num)时,即使root也被正确删除Bst::node * Bst::del(node *root, int num)但是当调用返回到void Bst::del(int num)根的价值没有得到保留,是垃圾。

Question 2: Why the error got fixed when I stored the returned value in variable root? 问题2:为什么将返回值存储在变量root中时,错误会得到解决?

Assuming you have a member variable named root , then the problem probably is because you shadow the member variable root with the argument root in your deletion function. 假设您有一个名为root的成员变量,那么问题可能是因为您在删除函数中用参数root屏蔽了成员变量root So when you do root = NULL in the function, you only set the argument to NULL and not the member variable. 因此,当您在函数中执行root = NULL时,仅将参数设置为NULL而不将成员变量设置为。

There is also the problem with the other assignments to root , which will just assign to the local argument and not the member variable as well. root的其他分配也存在问题,它们只会分配给局部参数,而不是成员变量。

The fix you've made (assigning to root in the calling function) is, I think, the most correct solution. 我认为,您所做的修复(在调用函数中分配为root )是最正确的解决方案。

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

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