【问题标题】:What is "Read Access Violation... was nullptr"?什么是“读取访问冲突...是 nullptr”?
【发布时间】:2016-12-10 22:15:27
【问题描述】:

我需要一些帮助。我一直试图让我的删除功能正常工作,但无论我做什么,它总是给我一个“是 nullptr”的错误。我的代码有点混乱,因为我一直在恐慌并疯狂地尝试任何想到的东西。如果需要,我已经准备好重新开始。我到处寻找有关 nullptr 的信息并没有真正给出我真正理解的解释。我的理解是,当您尝试取消引用指针/节点时会出现“是 nullptr”错误,但我永远无法找到一种方法来处理对我来说有意义的问题。非常感谢任何帮助。我的代码是:

 `

BT::BT()
{
    node* root = NULL;
}

char BT::FindReplacement(node* parent, char param)
{
    if (parent == NULL) //In case someone tries to delete a node while there aren't any nodes in the Tree
    {
        return NULL;
    } 

    parent = parent->right;
    while (parent->left != NULL)
    {
        parent = parent->left;
    }

    return parent->data;
}

void BT::leafDriver()
{
    int count = 0;
    leafCount(root, count); 
}

void BT::leafCount(node* start, int count)
{


    if ((start->left == NULL) && (start->right == NULL))
    {
        count++;
    }

    if (start->left != NULL)
    {
        leafCount(start->left, count);
    }

    if(start->right != NULL)
    {
        leafCount(start->right, count);
    }
cout << " There are " << count << " number of leaves in the BST" << endl;

}


void BT::deletionDriver(char param)
{
    deletion(root, param);
}

void BT::deletion(node* parent, char param)
{

    if (parent->data < param) 
    {
        parent->left = parent->left->left;
        deletion(parent -> left, param);
        cout << "checking left node" << endl;
    }

    else if (parent->data > param)
    {
        parent->right = parent->right->right;
        deletion(parent->right, param);
        cout << "checking right node" << endl;
    }

    else if (parent->data == param)
    {
        //Case 1: No Children
        if (parent->left == NULL && parent->right == NULL)
        {
            delete parent;
            parent = NULL;

        }


        //Case 2: One Child
        else if ((parent->right == NULL) && (parent->left != NULL))
        {
            node* temp = parent;
            parent = parent->left;
            delete temp;
        }

        else if (parent->left == NULL)
        {
            node* temp = parent;
            parent - parent->right;
            delete temp;
        }

        //Case 3: Two Children
        else if ((parent->left != NULL) && (parent->right != NULL))
        {
            char tempParam;
            tempParam = FindReplacement(parent, param);
            parent->data = tempParam;
            deletion(parent->right, tempParam);
        }
    }

    else 
        cout << "Item is not found in BST" << endl;
}`

【问题讨论】:

  • 代码重复执行类似“parent->left = parent->left->left;”的操作,而不检查“parent->left”是否为 NULL。这可能是您崩溃的原因。
  • 你真的应该把你的程序放在调试器中,并验证问题出在哪里并加以防范。

标签: c++ binary-search-tree nullptr


【解决方案1】:

只要你有这样的代码:

parent = parent->right;

您需要检查您新分配给 parent 的值是否不为空。换句话说,您的代码应始终如下所示:

parent = parent->right;
if ( parent == nullptr ) {
    // handle null case
}
else {
    // handle case when there is another node
}

【讨论】:

    【解决方案2】:
    while (parent->left != NULL)
    

    改成

    while (parent != NULL)
    

    它应该可以工作

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2019-09-09
      • 1970-01-01
      相关资源
      最近更新 更多