I was working on trying to implement Binary Search Tree in C and had come across this issue. I have made my Insert function of void type and hence I do not have a variable which assigns the value of this call-back. When I run the program I do not see any output. Is this something to do with me not assigning my root node pointer once it has changed from NULL to pointing to a value?
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left, *right;
}Node;
void addToNode(Node *A, int value)
{
A->data=value;
A->left=NULL;
A->right=NULL;
}
void insert(Node *A, int value)
{
if(A==NULL)
{
A = (Node*)malloc(sizeof(Node));
addToNode(A, value);
// printf("Hello\n");
return;
}
else
{
if(value>A->data)
{
insert(A->right, value);
// printf("Hello\n");
return;
}
else if(value<A->data)
{
insert(A->right, value);
// printf("Hello\n");
return;
}
}
}
void inorder(Node *root)
{
if (root != NULL)
{
// printf("Hello\n");
inorder(root->left);
printf("%d \n", root->data);
inorder(root->right);
}
}
int main()
{
Node *root = NULL;
int A[]={45,32,56,23,11,89};
for(int i=0; i<6; i++) insert(root,A[i]);
inorder(root);
}