The load function is loading the dictionary correctly (to the best of my knowledge). I have confirmed this by stepping through it in the debugger and watching the values change in the trie in the local variables.

My strategy for loading is...

iterate through each character in dictionary

  if character == '\n'
    change cursor->is_word to true
    reset cursor to root

  get an alphabetic index (0-26, 26 for " ' ")

  if cursor->children[alpha_index] == NULL
    create new node and assign to cursor->children[alpha_index]

  move cursor to cursor->children[alpha_index]

Problem is during the check function only the last word in the dictionary has is_word set to true.


  dictionary.txt: cat cater
     example.txt: ca cat cats cater caterlo

  ./speller dictionary.txt example.txt 



I am getting similar results when I add words to the dictionary and example text.

When step debugging through the check function, when I reach the letter 'T' node I would expect the value of that node's 'is_word' property to be true but it shows false. Like I mentioned earlier this was not the case when stepping through the load function.

However, when I get to the 'R' node, I get the expected is_word: true. When I put 'cater' before 'cat' in the dictionary, node 'T'->is_word == true and node 'R'->is_word == false.

I can't figure out why it is being loaded correctly and then when it is being check it's like none of that worked properly with the exception of the last word in the dictionary.

I've spent two full days on this and need some fresh eyes. I'm super stuck on this one. Uploaded my code to Github.


Thanks for the help in advance!

1 Answer 1

if (cursor->children[alpha_index] == NULL)
                cursor->children[alpha_index] = malloc(sizeof(node));
            // advance to next node in trie
            cursor = cursor->children[alpha_index];

What happens if cursor->children[alpha_index] is not NULL?

When it gets to the 'c' in cater (which is not null), it pushes the cursor and promptly erases (initToNull) any prior knowledge of the 'c' node tree.

  • Thank you so much! So initToNull was nulling out all nodes I was traversing to, deleting all their properties. I realize now that is appropriate to put initToNull in the if statement immediately after I create the node. Tests pass now, thanks again!.
    – Ben Baik
    May 26, 2017 at 19:45

You must log in to answer this question.

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