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 7813305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:48:33+00:00 2026-06-02T04:48:33+00:00

Maybe asked million times before but I simply cannot understand what’s wrong with this.

  • 0

Maybe asked million times before but I simply cannot understand what’s wrong with this. I didn’t want to use any code on the internet so I’ve just tried to program what’s on my mind. Either this or my print function is wrong. Is there anything wrong with the code below?

void addNode(int value)
    {
        Node* newNode=new Node;
        newNode->data=value;
        if(root==NULL)
            root=newNode;
        else {
            Node* temp=root,*parent;
            while(temp!=NULL)
            {
                parent=temp;
                if(temp->data == value)
                    return;
                else if(temp->data < value)
                    temp=temp->left;
                else 
                    temp=temp->right;
            }
            temp=newNode;
        }
    }
  • 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-02T04:48:34+00:00Added an answer on June 2, 2026 at 4:48 am
    temp=newNode;
    

    This assigns the pointer to a local variable, which is discarded when the function returns, losing the new node. Instead, you want to assign it to a pointer within the tree; perhaps something like:

    if (temp->data < value) {        // If the new node should be to the left
        if (temp->left) {            //   If there is a left subtree
            temp = temp->left;       //      Move into the left subtree
        } else {                     //   Otherwise
            temp->left = newNode;    //      Insert the new node there
            return;                  //      Done.
        }
    }
    

    and likewise for temp->right if value < temp->data.

    Also:

    if (temp->data == value) 
        return;
    

    You have a memory leak there; you should delete newNode before returning.

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

Sidebar

Related Questions

Maybe this question has been asked many times before, but I never found a
Maybe this has been asked before, but I couldn't find it. My question is
Sorry if this has been asked before, but as I understand it, in C++11,
I think this has been asked a million times, but with different definitions of
I know that maybe this question has been asked before, but I can't seem
I know this question has been asked countless times before, but the other threads
Maybe this question has been asked a lot, but I still can't understand how
Maybe this has been asked before (where I didn't find it)... I have a
I asked this question on superuser , but haven't gotten a response. Maybe here
Got a little problem i asked about it before but maybe i didnt ask

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.