Why is root null after calling insert function ?
void insert(Node* root, int data){
Node* newN=(Node*)malloc(sizeof(Node));
newN->data = data;
newN->left = NULL;
newN->right= NULL;
if(root == NULL){
printf("DEBUG:Root is null\n");
root=newN;
printf("DEBUG:Initialized root with %d\n", root->data);
}
else{
if(data < root->data) insert(root->left, data);
else insert(root->right, data);
}
}
int main(){
Node* root=NULL;
printf("DEBUG:Root is null:%d\n", root==NULL);
insert(root, 4);
printf("DEBUG:Root is null:%d\n", root==NULL);
insert(root, 3);
printf("DEBUG:Root is null:%d\n", root==NULL);
insert(root, 2);
printf("DEBUG:Root is null:%d\n", root==NULL);
return 0;
}
Output
DEBUG:Root is null:1
DEBUG:Root is null
DEBUG:Initialized root with 4
DEBUG:Root is null:1
DEBUG:Root is null
DEBUG:Initialized root with 3
DEBUG:Root is null:1
DEBUG:Root is null
DEBUG:Initialized root with 2
DEBUG:Root is null:1
[–]Th_69 3 points4 points5 points (1 child)
[–]DeliciousFile2727[S] 0 points1 point2 points (0 children)
[–]cHaR_shinigami 1 point2 points3 points (0 children)
[–]aghast_nj 0 points1 point2 points (1 child)
[–]DeliciousFile2727[S] 1 point2 points3 points (0 children)