Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:28:13+00:00 2026-06-11T23:28:13+00:00

I was writing code to delete a node from a BST recursively mainly as

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:28:14+00:00Added an answer on June 11, 2026 at 11:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have am writing a code where I can edit or delete certain details
I'm writing some code that has to cascade delete records in a certain database,
I am writing code to clone object but have no cue from Hobo documentation.
I want to know if its possible writing some code which will delete an
I have been writing same code for insert, update, delete with LINQ over and
I am writing some code to update an application by fetching new dlls from
I am writing some code to implement a deep copy of an object. Here
I'm writing some C++ code for a simple Node class. This is basically a
I am writing this code to protect my server from SQL injection. The goal
I am writing code that checks for the permission to write to and delete

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.