Question

// 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;
    }
}

I am writing up a BST with insert and delete functions. This is the code for my deleteNode function. When I try to delete a node, it gives me the access violation error and sends the program into debug. I'm not sure why. I read on other sites that you need to clear the node first, but I was told by my professor that you could just search the node, find it, and delete it.

Here is my main function for creating the tree, printing it, and deleting two nodes.

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();

Thank's in advance for any help you can provide!

Was it helpful?

Solution

The access violation error occurs when you corrupt the memory somewhere. Since you are trying to delete node which means you want to free your memory. Most likely, it should be the problem on your node class.

  • Did you come up with a functional destructor on your node class?

  • Check your code if it deference invalid pointers.

  • Did you initialize your object with proper malloc()?

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top