0

In my dictionary.c file, I declared my own function called trieNavigate(), whose code is below. I use it for loading and for checking, and so it returns a bool and accepts 3 parameters: a char (that is the character I use to navigate the trie), the "trieCrawler", a pointer to a trieNode, and a bool that tells the function if it is supposed to create a new trieNode if it tries to navigate to one that doesn't already exist. The problem is that when I am checking, it returns false when bool createNodePath is false, even if the node it tries to navigate to exists. I'm not sure if this code is the problem, or if the problem lies in my implementation of load() or check().

Please help.

bool trieNavigate(char c, trieNode *trieCrawler, bool createNodePath)
{
    char stringOfC[2];
    stringOfC[0] = c;
    int letterValue;
    if (strcmp(stringOfC, "\'") == 0)
    {
        letterValue = 26;
    }
    else
    {
        letterValue = c - 65;
    }
    if (trieCrawler->lettersAndApos[letterValue] == NULL)
    {
        if (createNodePath == true)
        {
            trieNode *newTrieNode = malloc(sizeof(trieNode));
            newTrieNode = NULL;
            trieCrawler->lettersAndApos[letterValue] = newTrieNode;
            trieCrawler = trieCrawler->lettersAndApos[letterValue];
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        trieCrawler = trieCrawler->lettersAndApos[letterValue];
        return true;
    }
}

1 Answer 1

0

1) When and how are you setting the bool createNodePath to false? 2) You do:

trieNode *newTrieNode = malloc(sizeof(trieNode));
newTrieNode = NULL;

So, you're allocating a block of memory, assigning a pointer to point at that address, and in the next line, you're setting the address newTrieNode points at to NULL. You're losing the block right there. So, basically, all you do from now on is having pointers that point at NULL.

Then, i guess, when you check (i'm guessing that you set the bool to false then), the pointers will be NULL, so the function will return false.

There's other things that may or may not work in your function. Using the same function for loading and checking is, at the very least, confusing, and you have to do a lot of extra checking outside of it: i'm guessing that you check for leaf nodes in check()? And also you have to handle there all those individual true returns from this function?, and you also mark the leaf node from load() itself? And also that you reset the trieCrawler pointer (which i assume is your real trie structure?) to root every time you're done with checking or loading a word.

Thing is, you don't really gain anything from joining the two into one function, and it is the ideal way to hide errors. If you're sure that all the things I just mentioned are fine, then it should work, provided that you correct the NULL assignment for the pointer.

1
  • I did that, and trieNavigate(), however I created this function because I am trying to implement a more complex data structure. I want a 4 level trie, and after that, the leaf nodes branch off into linked lists for possible word endings. This might be faster than instantiating a new trieNode for every letter during the load function.
    – David
    Sep 9, 2015 at 1:03

You must log in to answer this question.

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