The following code for Trie implementation is throwing Floating point exception upon call to function insert. The line inside for loop to check for existing node is where the problem is.
struct Node {
char c;
bool isend;
unordered_map<int, struct Node*> map;
};
void insert(struct Node* root, string contact) {
int size = contact.size();
char ch;
for (int i = 0; i < size; i++) {
ch = contact[i];
// this check is creating problem
if (root->map.find(ch) == root->map.end()) {
struct Node* tmp = (struct Node*) malloc(sizeof(struct Node));
tmp->c = ch;
if (i == (size - 1)) {
tmp->isend = true;
} else {
tmp->isend = false;
}
root->map.insert(make_pair(ch, tmp));
root = tmp;
} else {
root = root->map[ch];
}
}
}
int main()
{
struct Node* root = NULL;
root = (struct Node*) malloc(sizeof(struct Node));
insert(root, "text");
}
Any help?