【问题标题】:Access violation error on delete node call [closed]删除节点调用上的访问冲突错误[关闭]
【发布时间】:2014-04-07 18:35:42
【问题描述】:
// Delete a node
// (1) If leaf just delete
// (2) If only one child delete this node and replace
// with the child
// (3) If 2 children. Find the predecessor (or successor).
// Delete the predecessor (or successor). Replace the
// node to be deleted with the predecessor (or successor).
void Tree::deleteNode(int key)
{
    // Find the node.
    Node* thisKey = findNode(key, root);

    // (1)
    if ( thisKey->Left() == NULL && thisKey->Right() == NULL )
    {
        if ( thisKey->Key() > thisKey->Parent()->Key() )
            thisKey->Parent()->setRight(NULL);
        else
            thisKey->Parent()->setLeft(NULL);

        delete thisKey;
    }

    // (2)
    if ( thisKey->Left() == NULL && thisKey->Right() != NULL )
    {
        if ( thisKey->Key() > thisKey->Parent()->Key() )
            thisKey->Parent()->setRight(thisKey->Right());
        else
            thisKey->Parent()->setLeft(thisKey->Right());

        delete thisKey;
    }
    if ( thisKey->Left() != NULL && thisKey->Right() == NULL )
    {
        if ( thisKey->Key() > thisKey->Parent()->Key() )
            thisKey->Parent()->setRight(thisKey->Left());
        else
            thisKey->Parent()->setLeft(thisKey->Left());

        delete thisKey;
    }

    // (3)
    if ( thisKey->Left() != NULL && thisKey->Right() != NULL )
    {
        Node* sub = predecessor(thisKey->Key(), thisKey);
        if ( sub == NULL )
            sub = successor(thisKey->Key(), thisKey);        

        if ( sub->Parent()->Key() <= sub->Key() )
            sub->Parent()->setRight(sub->Right());
        else
            sub->Parent()->setLeft(sub->Left());

        thisKey->setKey(sub->Key());
        delete sub;
    }
}

我正在编写一个带有插入和删除功能的 BST。这是我的 deleteNode 函数的代码。当我尝试删除一个节点时,它给了我访问冲突错误并将程序发送到调试。我不确定为什么。我在其他网站上看到你需要先清除节点,但我的教授告诉我,你可以搜索节点,找到它,然后删除它。

这是我创建树、打印树和删除两个节点的主要功能。

int main()
{
    //Create and insert nodes into tree.
    Tree* tree = new Tree();
    tree->addNode(5);
    tree->addNode(8);
    tree->addNode(3);
    tree->addNode(12);
    tree->addNode(9);

    cout<<" Inserting Nodes: 5, 8, 3, 12, and 9 sequentially. "<< endl;
    cout<<" -------------------"<< endl;

    cout<<" Here are the values in the tree (in-order traversal)" << endl;
    tree->print_inorder();
    cout << endl << endl;

    cout << "Deleting 8...." << endl;
    //tree->deleteNode(8); I get an access violation for the delete node functions.
    cout << "Deleting 12...." << endl;
    //tree->deleteNode(12);
    cout << "Now, here are the nodes (in-order traversal)" << endl;
    cout << "3 5 9" << endl;
    //tree->print_inorder();

提前感谢您提供的任何帮助!

【问题讨论】:

  • 您是否尝试过单步执行代码?这样您就可以找到错误的确切行并评估内存中的对象。
  • 一旦您在此处发布了内容,并且它已经得到了一个赞成的答案,它就不再是您要删除的了。不允许您删除您的问题是有原因的,破坏它也是不可接受的。

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


【解决方案1】:

当您在某处损坏内存时会发生访问冲突错误。 由于您正在尝试删除节点,这意味着您想要释放内存。 很可能,这应该是您的节点类的问题。

  • 你是否在你的节点类上想出了一个函数式析构函数?

  • 检查您的代码是否遵循无效指针。

  • 您是否使用正确的 malloc() 初始化对象?

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多