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);
    }

    }
3
  • You should use the debugger to trace the activity of your program. Commented Jun 15, 2012 at 16:51
  • 1
    one problem is that else if (v <= node->value) statement. If v==node->value, it shouldn't be added to the tree at all. BSTs cannot contain duplicates. So I would add an else to throw those away. Commented Jun 15, 2012 at 16:59
  • Or just drop the = from else if(v <= node->value) Commented Jun 15, 2012 at 17:04

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

As an addendum, consider using std::shared_ptr<Node> instead of Node* in the interest of sensible automated memory management.
Thank you Rook....but when I pass it to the arguments as Node** I am getting a compiler error? Is there any further modifications I need to add in the code? Can you please send me the whole part modified? Thank you.
Part of the code inline. You can adapt the rest easily enough ;-)
Node*& would be a lot easier to work with (i.e., less dereferencing).
0

You are allocating a new node and filling it in, but never changing a pointer in an existing node to point to the new node.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.