I am trying to insert node into a binary search tree inserting a node in the left subtree is giving no issues but inserting a node at right subtree is giving issue of null pointer exception.
inserting a node first time work properly but when I try to insert a new node in the right part of the root, I found the root has already been given some garbage value while traversing n it prints null pointer exception.
typedef struct tree
{
int elt;
struct tree *left,*right;
}T;
T *RT=null;
void insert()
{
int n;
T *move,*temp,*back;
printf("\nEnter an element: ");
scanf("%d",&n);
temp=(T *)malloc(sizeof(T *));
temp->left=NULL;
temp->right=NULL;
temp->elt=n;
if(RT==NULL)
RT=temp;
else
{
move=RT;
while(move!=NULL)
{
back=move;
if(move->elt>=n)
move=move->left;
else if(move->elt<n)
move=move->right;
}
if(back->elt>=n)
back->left=temp;
else
back->right=temp;
}
printf("%d %d %d",RT->elt,RT->left->elt,RT->right->elt);
}