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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:30:25+00:00 2026-06-05T23:30:25+00:00

I am trying to implement a simple C++ function that adds a node to

  • 0

I am trying to implement a simple C++ function that adds a node to a Binary Search Tree given the value of the node to be inserted, and the root of the BST.
Surprisingly, I am not able to push any element. Although I made sure that the statement where I am inserting the node is entered by the compiler, the tree did not have any of the nodes I am trying to add. I think the problem could be in how I am passing the node in the function argument. Anyone can help? Thank you. Here’s my Node type and the implementation of the function.

 struct Node{

    int value;
    Node *left;
    Node *right;
    };

    //this method adds a new node with value v to Binary Search Tree
    // node is initially the root of the tree
    void Push_To_BST(Node* node,int v)
    {

    // the right place to add the node
    if(node==NULL)
    {

    node=new Node();
    node->value= v;
    node->left= NULL;
    node->right=NULL;

    }

    //the value to be added is less than or equal the reached node
    else if(v <= node->value)
        {
    //adding the value to the left subtree
    Push_To_BST(node->left,v);
    }

    //the value to be added is greater than the reached node
    else if(v > node->value)
    {
    //adding the value to the right subtree
    Push_To_BST(node->right,v);
    }

    }
  • 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-05T23:30:28+00:00Added an answer on June 5, 2026 at 11:30 pm

    Careful with your referencing, there.

    void Push_To_BST(Node* node,int v) 
    { 
    
    // the right place to add the node 
    if(node==NULL) 
    {  
        node=new Node(); 
        // etc
    

    The node you are allocating memory to is a local variable. You would need to pass in a Node** in order to pass out a pointer to a freshly created node.

    Example:

    void Push_To_BST(Node** pnode,int v) 
    { 
        Node* node = *pnode;
    
        // the right place to add the node 
        if(node==NULL) 
        {  
            node=new Node(); 
            // etc
        }
        else if(v < node->value)  
        {  
            //adding the value to the left subtree  
            Push_To_BST(&node->left,v);  
        }  
        // etc
    

    and call it like

    Node* n = new Node;
    Push_To_BST(&n, 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to implement a simple function that can concatenate any number of
I am trying to implement a simple min function that accepts two parameters &
I'm trying to implement a very simple shorthand function for sending headers, based on
I am trying to implement a simple client that connects to a give address.
I'm trying to implement a simple watermark on a text box that disappears when
I am trying to implement a function that will insert a new entry in
Grrrrrr, trying to implement a very simple tree. It doesn't need to be bidirectional
I'm trying to implement a simple function using jqGrid, but it doesn't seem to
I'm trying to create a simple program or function that will take the users
I am trying to implement a very simple little dropdown-style mini-menu that will appear

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.