1

i am trying to insert the charachter in to ternary search tree, please help me out with this segmentation fault ?? here is what i am doing to inser in to trie,on running this i am getting segmentation fault(core dumped ) please help me out why this is so ??

int main(int argc ,char* agrv[])
{
TSTNode *root;
char *str;
cin >> str;
InsertTST(root,str);
DisplayTST(root);
return 0; 
}

TSTNode* InsertTST(TSTNode *root, char *str)
{
if(root== NULL){
    TSTNode *root = (TSTNode *)malloc(sizeof(TSTNode *));
    root->left = NULL;
    root->right = NULL;
    root->eq = NULL;
    root->is_end_of_str = 0;

    return root;
}


if(root->data < *str)
    InsertTST(root->right, str);
else if (root->data == *str){
    if(*(str+1) != '\0')
    InsertTST(root->eq, str+1);
    else 
    root->is_end_of_str = 1;
}
else 
    InsertTST(root->left, str);

return root;
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
user1688392
  • 21
  • 1
  • 3

1 Answers1

2
char *str;
cin >> str;

no allocation of memory, followed by a write to said memory = seg fault.

Oren
  • 4,152
  • 3
  • 30
  • 37