1

So I'm trying to insert a value into a binary tree using this recursive function:

void add(node* *hd, int v){
node* curr = *hd;
if(curr == NULL){
    curr = (node*)malloc(sizeof(node));
    curr->value = v;
}else{
    if(v < curr->value){
        add(&curr->left, v);
    }else{
        add(&curr->right, v);
    }
}
}

It doesn't seem to be working, and I just don't understand why I can't do something like this. How would I go about fixing it?

0

6 Answers 6

6

You need to initilize the pointers, as they probably will be set to whatever you get when allocating space. Right now when you pass add(&curr->left, v); curr->left may not be a pointer somewhere but it is still not NULL;

void add(node* *hd, int v){
    node* curr = *hd;
    if(curr == NULL){
        curr = malloc(sizeof(node));
        curr->left = curr->right = NULL;
        curr->value = v;
        *hd = curr; // from Mohamed KALLEL
    }else{
        if(v < curr->value){
            add(&curr->left, v);
        }else{
            add(&curr->right, v);
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

that's right. your answer missed the fact to link the new node curr to the tree
Thank you, I understand now! I think it's supposed to be *hd = curr; though. But otherwise yes this is correct.
Don't you need to add return; to stop the recursion?
No as we are looking for a node which is empty to insert at, thus when we reach if(curr == NULL) we will allocate the memory, set the pointers and then the recursion stops(reaches the end of the function) as it no longer calls add() as it is the other branch which was not taken.
5

Your new node is not being "hooked up" correctly, since you're just storing the pointer in the local variable curr, instead of writing it to *hd to change the caller's pointer.

Also, don't cast the return value of malloc() in C.

Comments

2
if(curr == NULL){
    curr = malloc(sizeof(node));
    curr->right = NULL;
    curr->left = NULL;  // From ks6g10 in order to initialize right and left to NULL
    curr->value = v;
    *hd = curr; // add this
}

BTW use calloc instead of malloc. it initializes your node memory to 0

2 Comments

No point whatsoever in using calloc(); the only content it works for (the integer) is immediately overwritten. Note that calloc() can not be relied on to set pointers to NULL correctly.
yes you are right. In some platforms NULL is not 0. thx. answer updated
1

Another way to add in the binary tree recursively can be done like this:

node *add(node *hd, int v) {
   node* curr = NULL;

   if(!hd){
      curr = malloc(sizeof(node));
      curr->value = v;
      curr->left = NULL;
      curr->right = NULL;
      return curr;
   }
   else {
     if(v < curr->value)
        curr->left = add(curr->left,v);
     else 
        curr->right = add(curr->right,v);  
  }
  return hd;
}

Comments

0

You need to initialize left and right pointers of your newly formed node with NULL and let your hd point to that node.

void add(node* *hd, int v){
node* curr = *hd;
if(curr == NULL){
    curr = malloc(sizeof(node));
    curr->value = v;
    curr->left=curr->right=NULL;
    *hd = curr;

}else{
    if(v < curr->value){
        add(&curr->left, v);
    }else{
        add(&curr->right, v);
    }
}
}

Comments

0

I did it like this:

void insert(int data, node *&cur)
{
    if (cur == NULL)
    {
        cur = (struct node*) malloc(sizeof(struct node));
        cur->data = data;
        cur->left = NULL;
        cur->right = NULL;              
    }
    else
    {
        if (data > cur->data)
        {
            insert(data, cur->right);
        }
        else
        {
            insert(data, cur->left);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.