1

I am trying to create a function for adding a node to BST.

The code for insert function is as below.

void insert(struct node **root,int data){
    if(!(*root)){
        struct node *temp=(struct node*)malloc(sizeof(struct node));
        temp->data=data; //how to do without temp node
        temp->left=temp->right=NULL;
        *root=temp;
        return;
    }

    if(data<(*root)->data){
        insert(&(*root)->left,data);
    }
    else if(data>(*root)->data){
        insert(&(*root)->right,data);
    }
}

how do i insert the value for the first if condition without using a temp node. I am fairly new to pointers. I am able to get the correct answer but want to avoid using the temp node

EDIT:

this is as per the suggestion of @1201ProgramAlarm As per this i am not getting any output on printing it

struct node{
    int data;
    struct node *left,*right;
};
//
void print_inorder(struct node * tree)
{
    if (tree)
    {
        print_inorder(tree->left);
        printf("%d\n",tree->data);
        print_inorder(tree->right);
    }
}
void insert(struct node **root,int data){
    if(!(*root)){
        struct node *temp=(struct node*)malloc(sizeof(struct node));
        (*root)->data=data;
        (*root)->left=(*root)->right=NULL;
        return;
    }

    if(data<(*root)->data){
        insert(&(*root)->left,data);
    }
    else if(data>(*root)->data){
        insert(&(*root)->right,data);
    }
}

void main(){

    struct node *root=(struct node*)malloc(sizeof(struct node));
    root=NULL;
    insert(&root,9);
    insert(&root,4);
    insert(&root,15);
    insert(&root,6);
    insert(&root,12);
    insert(&root,17);
    insert(&root,2);
    print_inorder(root);
}

EDIT 2: removed the temp initialization in the function and initialized the root as null in main.

void insert(struct node **root,int data){
    if(!(*root)){
        (*root)->data=data;
        (*root)->left=(*root)->right=NULL;
        return;
    }

    if(data<(*root)->data){
        insert(&(*root)->left,data);
    }
    else if(data>(*root)->data){
        insert(&(*root)->right,data);
    }
}

void main(){
    struct node *root=NULL;
    insert(&root,9);
    insert(&root,4);
    insert(&root,15);
    insert(&root,6);
    insert(&root,12);
    insert(&root,17);
    insert(&root,2);
    printf("Inorder Traversal\n");
    print_inorder(root);
}

STill not working. Any help is appreciated as am fairly new to pointers.

  • 2
    It isn't a "temp" node. You're allocating a node to store the data to insert. Would it be easier to understand if the variable name was "new_node" instead of "temp"? Or are you asking how to create the new node without using a local variable? – 1201ProgramAlarm Jun 17 '19 at 21:22
  • yea i am asking how to create without using local variable – Harit Kapoor Jun 17 '19 at 21:23
  • The same way you're recursively calling `insert` for the left or right nodes: use `(*root)` instead of `temp`. – 1201ProgramAlarm Jun 17 '19 at 21:29
  • Actually, what your code needs is another local variable to use as a substitute for `(*root)` – user3386109 Jun 17 '19 at 21:29
  • You forgot to delete the `temp` variable. Which means that the memory returned by `malloc` is not actually being used. And on a related topic, the memory returned by `malloc` in `main` is also not being used. The `malloc` in `main` should be removed. – user3386109 Jun 17 '19 at 21:48
  • @user3386109 done all that but still not printing – Harit Kapoor Jun 17 '19 at 21:52
  • 1
    To be clear, the `malloc` in `main` isn't needed. The `malloc` in `insert` is definitely needed, but the `temp` variable needs to be removed. In any case, if the problem persists, be sure to update the question with the latest code. – user3386109 Jun 17 '19 at 21:57

0 Answers0