0

So I have a problem here, I have done everything but it is pointing a lot of misspelled words, although all the times and valgrind seems to be correct.

WORDS MISSPELLED:     5067
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        19190
TIME IN load:         0.08
TIME IN check:        0.01
TIME IN size:         0.00
TIME IN unload:       0.04
TIME IN TOTAL:        0.13

I can't spot if the problem is in check or load.

load

[code hidden to comply to academic honesty]

if(isalpha(ch))
{
    index = tolower(ch) - 'a';

    if(walker->children[index] == NULL)
    {
        node* letter = malloc(sizeof(node));
        letter->children[index] = NULL;
        walker->children[index] = letter;
    }

    else if(walker->children[index] != NULL)
    {
        walker = walker->children[index];
    }

}

[code hidden to comply to academic honesty]

Can somebody please help?

2

Your problem is not in check, but in load. Take a look at your code here:

if(isalpha(ch))
{
    index = tolower(ch) - 'a';

    if(walker->children[index] == NULL)
    {
        node* letter = malloc(sizeof(node));
        letter->children[index] = NULL;
        walker->children[index] = letter;
    }

    else if(walker->children[index] != NULL)
    {
        walker = walker->children[index];
    }

}

Say that walker->children[index] == NULL

  1. you create a new node
  2. you incorrectly initialize only children[index] to NULL. Better either use calloc instead of malloc, or initialize all children to NULL and not just this arbitrary one. Although this is still not a problem.
  3. you attach the new node to the trie in walker->children[index] place.
  4. Here is the mistake. You don't move your walker to the newly created node. You continue to attach "leaves" to the root of the trie. You walk down the trie only when walker->children[index] != NULL, but you should also do so when you create a new node.

You also have the same mistake in the other characters you read. Hope this will get you going. If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.

5
  • I solved it by doing what you said. But I quite didnt understand. Well, the part of initializing onle the children[index] to NULL I understand. But why I had to do: walker->children[index] = letter; walker = walker->children[index]; I did not understand.. – Joao Turolla Aug 22 '15 at 14:51
  • Because if you want to store the word "fox", you create a node at root's children[4] for 'f'. Then, the 'o' must be stored on root->children[4]->children[14]. So on the 'f''s children[14], not roots. It's the whole logic behind tries. If you don't understand it maybe you should take another look at the walkthrough? – ChrisG Aug 22 '15 at 15:06
  • I am pretty sure I understood the logic behind tries, because I watched every video etc there was. For me is just a bit hard to see in code, because I am not all that confident about pointers and structs, but now I see. – Joao Turolla Aug 22 '15 at 15:49
  • It's OK. Nobody knows that. It's knowledge you acquire with practice. Keep coding! 😉 – ChrisG Aug 22 '15 at 15:52
  • I will, always. – Joao Turolla Aug 22 '15 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .