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

  • Home
  • SEARCH
  • 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 6748657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:30+00:00 2026-05-26T12:35:30+00:00

I am trying to remove the left child (10) of a sample binary search

  • 0

I am trying to remove the left child (10) of a sample binary search tree using two methods:

  • Method1: By passing pointer to a pointer to the current node.
  • Method2: By passing address of the pointer to the current node. This does not removes the node, but calling delete corrupts the pointer arrangement, causing a crash while printing the nodes.

The tree looks like this and I am trying to delete 10 and replace it with 5

       20
       |  
   10--|---30
    |
5---|

I have some understanding of pointers. But still, I am not clear with this behavior of pointers.

#include <iostream>
class Node
{
public:
    Node(int key) : leftChild(0), rightChild(0), m_key (key){}
    ~Node(){}

    Node *leftChild;
    Node *rightChild;
    int m_key;
};

Node* build1234(int, int, int, int);
void print(Node *);
void print1234(Node *);

void removeLeft(Node **nodePtr)
{
    Node *oldPtr = *nodePtr;
    if(*nodePtr)
    {
        *nodePtr = (*nodePtr)->leftChild;
        delete oldPtr;
    }
}

int main()
{
    Node *demo1 = build1234(10, 20, 30, 5);
    Node *demo2 = build1234(10, 20, 30, 5);
    print1234(demo1);
    print1234(demo2);

    //Method1 - 10 is correctly removed with 5
    Node **nodePtr = &demo1;
    nodePtr = &(*nodePtr)->leftChild;
    removeLeft(nodePtr);
    print1234(demo1);

    //Method2 - 10 is not removed
    Node *node = demo2;
    node = node->leftChild;
    removeLeft(&node);
    print1234(demo2);       
    return 0;
}

Node* build1234(int B, int A, int C, int D)
{
    Node *root = new Node(A);
    root->leftChild = new Node(B);
    root->rightChild = new Node(C);
    root->leftChild->leftChild = new Node(D);
    return root;
}
void print(Node *node)
{
    if(node)
    {
        print(node->leftChild);
        std::cout << "[" << node->m_key << "]";
        print(node->rightChild);
    }
}

void print1234(Node *node)
{
    std::cout << std::endl;
    print(node);
}

Note: This question is not about BST, but pointers. If you see the two calls to removeLeft(nodePtr) and the removeLeft(&node) in the main() function.

  1. How are these two different?
  2. Why the second method fails to achieve the desired result?
  • 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-05-26T12:35:30+00:00Added an answer on May 26, 2026 at 12:35 pm

    In the first case, you are passing an address of a pointer that exists in the tree, so you are modifying the contents of the tree directly.

    In the second case, you are passing an address of a variable that is local to main() instead. The tree is not modified, and deleting from the address is accessing stack memory, which is why it crashes

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

Sidebar

Related Questions

I am trying to create a custom Binary Search Tree, and I have everything
I'm trying to remove all but the first child component from a Java Container.
I am trying to remove the attribute xmlns=http://webdev2003.test.com from the following xml using xsl/xslt,
I am using htmlpurifier to clean up user content. I am trying to remove
I'm trying to make a 2 column layout using class=left and class=right, at the
I am trying to select first child of ul and then remove it and
I am trying to create a control which contains two listboxes with add/remove buttons
I am trying to figure out how to remove a node from a binary
Im trying to do a left join on two tables to get all values
I am trying to remove a pending changelist in perforce. All the files (20

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.