0

I am implementing a trie and I can't figure out the load function. I can't understand how do I put the letters into the trie other than the first trie. So, far I have only figured out this much

while (fscanf(file, "%s", word) != EOF)
    {
        for(int j = 0, l = strlen(word); j < l; j++)
        {
            int k=tolower(word[j])-'a';
            root->children[k] = calloc(1,sizeof(node));
        }
        root->is_word = true;

    }
2
  • OK : so first of all the values of k which your are setting will only work when word[j] will be an alphabet, remember the apostrophes(') are also allowed , therefore you need to assign it to a new place in your initial array . After that remember that you need to allocate space to a child if it has not been already allocated . So first you need to check if the child is pointing to NULL or not. if it is not NULL then no need to allocate space. After allocating space to a child, make root to point towards that child . Repeat this process for the whole word – CeNiEi Jul 19 '19 at 18:42
  • Hey thank you very much. The code seems to work using the debugger as it correctly skips over 'c' 'a' 't' when caterpillar was the input. – Prashant Kumar Jul 20 '19 at 7:41

You must log in to answer this question.

Browse other questions tagged .