Chinmay Nerurkar
Chinmay Nerurkar

Reputation: 495

Passing Pointer by Reference not working as intended

I was writing code to delete a node from a BST recursively mainly as a study exercise to implement recurion and to verify my understanding of certain coding elements I have used. In this case the issue seems to be with passing the pointer to my BST node by reference.

A psrt of my BST code is as follows. (This is not code for something I would implement or use. Just an exercise. I chose BST as an example to implement some things I wanted to try out in the language)

Header -

//BST.h
class TNode
{
public:
    TNode():data(0), left(0), right(0) {}
    int data;
    TNode* left;
    TNode* right;
};

class BST
{
private:
    TNode* Head;

public:
    BST();

    void InsertData(int data);
    void InsertNode(TNode* node);
    void DeleteData(int data);

private:
    void InsertDataPrivate(int data, TNode* &root); 
    void InsertNodePrivate(TNode* node, TNode* &root);
    void DeleteDataPrivate(int data, TNode* &root);
};

CPP -

//BST.cpp
#include "BST.h"

BST::BST(): Head(0)
{

}

void BST::InsertData(int data)
{
    InsertDataPrivate(data, Head);
}

void BST::InsertNode(TNode* node)
{
    InsertNodePrivate(node, Head);
}

void BST::DeleteData(int data)
{
    DeleteDataPrivate(data, Head);
}


void BST::InsertDataPrivate(int data, TNode* &root) 
{
    if(root == 0)
    {
        root = new TNode();
        root->data = data;
    }
    else if(data < root->data) InsertDataPrivate(data, root->left);
    else if(data > root->data) InsertDataPrivate(data, root->right); 
}

void BST::InsertNodePrivate(TNode* node, TNode* &root) 
{
    if(root == 0) // Deep Copy
    {
        root = new TNode();
        root->data = node->data;
    }

    else if(node->data < root->data) InsertNodePrivate(node, root->left);
    else if(node->data > root->data) InsertNodePrivate(node, root->right); 
}

void BST::DeleteDataPrivate(int data, TNode* &root)
{
    if( 0 == root ) return;

    if( root->data == data )
    {
        if(0 == root->left && 0 == root->right)
        {
            delete root;
            root = 0;
        }
        else if(0 == root->left)
        {
            TNode* current = root;
            root = root->right;
            delete current;
            current = 0;
        }
        else if(0 == root->right)
        {
            TNode* current = root;
            root = root->left;
            delete current;
            current = 0;
        }
        else
        {
            TNode* biggestOnLeft = root->left;
            TNode* smallestOnRight = root->right;
            int i = 0;
            while (biggestOnLeft->right) // check if left subtree is longer than right subtree
            {
                biggestOnLeft = biggestOnLeft->right;
                --i;
            }
            while (smallestOnRight->left)
            {
                smallestOnRight = smallestOnRight->left;
                ++i;
            }
            if(i < 0) // left subtree is longer than right subtree
            {
                root->data = biggestOnLeft->data;
                DeleteDataPrivate(biggestOnLeft->data, biggestOnLeft);
            }
            else // right subtree is longer than or equal in size to left subtree
            {
                root->data = smallestOnRight->data;
                DeleteDataPrivate(smallestOnRight->data, smallestOnRight);
            }
        }
    }
    else if(data < root->data && 0 !=root->left)
    {
        DeleteDataPrivate(data, root->left);
    }
    else if(data > root->data && 0 !=root->right)
    {
        DeleteDataPrivate(data, root->right);
    }
}

and my test code is as follows -

//TestMain.cpp
#include "stdafx.h"
#include "BST.h"


int _tmain(int argc, _TCHAR* argv[])
{
    BST bst;

    bst.InsertData(32);
    bst.InsertData(46);
    bst.InsertData(3463);
    bst.InsertData(32);
    bst.InsertData(856);
    bst.InsertData(8098);
    bst.InsertData(345);
    bst.InsertData(234554);
    bst.InsertData(77);
    bst.InsertData(9);
    bst.InsertData(15);
    bst.InsertData(390);
    bst.InsertData(350);
    bst.InsertData(400);
    bst.InsertData(76);
    bst.InsertData(78);
    bst.InsertData(355);
    bst.DeleteData(77);

    return 0;
}

In the last step where I say bst.DeleteData(77); I have a problem. The node with '77' gets deleted alright and gets replaced by '78' in a fashion you delete a node with two children from a BST. However after deleting the node that had '78' before deletion of the parent node that had '77' still points to a non-null location.

I am calling my private function DeleteDataPrivate(int data, TNode* &root); which sets the TNode pointer to NULL after deleting it. Also I am passing the pointer-by-reference in the function so that the NULL value gets assigned to the deleted node pointer as the recursion stack unfolds back. This does not happen. Can someone explain what I am doing wrong here?

Thank you.

Chinmay

UPDATE

As per Dan's comment below the issue was with values being passed to local variables which made copies of my pointers and were not assigned back to anything. I have modified my function to account for this by using a pointer-to-pointer to so that the value of TNode pointer returned by the unwinding recursion is stored at the correct location in memory and not in some copy of TNode pointer.

The modified function is as follows

void BST::DeleteDataPrivate(int data, TNode* &root)
{
    if( 0 == root ) return;

    if( root->data == data )
    {
        if(0 == root->left && 0 == root->right)
        {
            delete root;
            root = 0;
        }
        else if(0 == root->left)
        {
            TNode* current = root;
            root = root->right;
            delete current;
            current = 0;
        }
        else if(0 == root->right)
        {
            TNode* current = root;
            root = root->left;
            delete current;
            current = 0;
        }
        else
        {
            TNode* biggestOnLeft = root->left;
            TNode* smallestOnRight = root->right;
            int i = 0;
            while (biggestOnLeft->right) // check if left subtree is longer than right subtree
            {
                biggestOnLeft = biggestOnLeft->right;
                --i;
            }
            while (smallestOnRight->left)
            {
                smallestOnRight = smallestOnRight->left;
                ++i;
            }
            TNode** locationOfDeletedNode = 0;
            if(i < 0) // left subtree is longer than right subtree
            {

                locationOfDeletedNode = &(root->left);
                while(*locationOfDeletedNode != biggestOnLeft) locationOfDeletedNode = &((*locationOfDeletedNode)->right);
            }
            else // right subtree is longer than or equal in size to left subtree
            {
                locationOfDeletedNode = &(root->right);
                while(*locationOfDeletedNode != smallestOnRight) locationOfDeletedNode = &((*locationOfDeletedNode)->left);

            }
            root->data = (*locationOfDeletedNode)->data;
            DeleteDataPrivate((*locationOfDeletedNode)->data, *locationOfDeletedNode);
        }
    }
    else if(data < root->data && 0 !=root->left)
    {
        DeleteDataPrivate(data, root->left);
    }
    else if(data > root->data && 0 !=root->right)
    {
        DeleteDataPrivate(data, root->right);
    }
}

Ofcourse this can be structured better and stuff but my goal here was to learn simple but tricky things here and thanks to Dan and others I have done that here.

Upvotes: 0

Views: 1552

Answers (2)

Dan
Dan

Reputation: 13160

When you call

DeleteDataPrivate(biggestOnLeft->data, biggestOnLeft);

It just assigns to the local variable biggestOnLeft, it does not assign to any object's member.

Because when you do

TNode* biggestOnLeft = root->left;

It creates a new variable which is a copy of root->left, not the same thing.

Fix ideas

You can try starting biggestOnLeft and smallestOnRight at root, and doing

while (biggestOnLeft->right->right) 

Then you can call

DeleteDataPrivate(biggestOnLeft->data, biggestOnLeft->right);

DISCLAIMER: I have not tested this, and could have made a typo or something.

Upvotes: 5

Corey D
Corey D

Reputation: 4689

You cannot assign NULL to the pointer by reference TNode because a reference variable cannot be NULL. Please see C++ pass pointer by reference and assign default value

Upvotes: -2

Related Questions