0

I can't figure out what is the problem. Please help me. Sorry for posting my check and load functions. But as I understand the problem can be in check or in load. I'm using trie:

 
    bool check(const char* word)
    {
    node* next_node = root;
    int j = 0;
    while (word[j] != '\0')
    {
        int children_index = 0;
        if (word[j] == '\'')
        {
            children_index = 26;
        }
        else if (isalpha(word[j]))
        {
            if (word[j] >= 65 && word[j] <= 90)
            {
                children_index = word[j] - 65;
            }
            else if (word[j] >= 97 && word[j] <= 112)
            {
                children_index = word[j] - 97;
            }
        }
        else
        {
            return false;
        }

    if (next_node -> children[children_index] != NULL)
    {
        next_node = next_node -> children[children_index];
    }
    else
    {
        return false;
    }
    j++;
    next_node -> is_word = true;
    return true;
}
return false;

}

/** * Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) { FILE* dictionary_file = fopen(dictionary, "r"); if (dictionary_file == NULL) { printf("Can't open the file\n"); return false; }

root = calloc(1, sizeof(node)); if (root == NULL) { return false; } char word[LENGTH + 2]; // until the end of file while (fgets(word, LENGTH + 2, dictionary_file) != NULL) { node* next_node = root; int i = 0; while (word[i] != '\n') { int children_index = 0; if (word[i] == '\'') { children_index = 26; } else if (isalpha(word[i])) { if (word[i] >= 65 && word[i] <= 90) { children_index = word[i] - 65; } else if (word[i] >= 97 && word[i] <= 112) { children_index = word[i] - 97; } } else { return false; } if (next_node -> children[children_index] == NULL) { next_node -> children[children_index] = calloc(1, sizeof(node)); next_node = next_node -> children[children_index]; } else { next_node = next_node -> children[children_index]; } // break; i++; } next_node -> is_word = true; dictionary_size++; } fclose(dictionary_file); return true;

}

It finds too much words: enter image description here

The result of check50: enter image description here

Thanks in advance!

1

Here's a big red flag. The following line is near the bottom of the check() function.

    next_node -> is_word = true;

In every dictionary word that it checks, it will set is_word to true for every letter that it moves across in the trie. The check function should not make any changes in the trie, it should only read the trie data.

It looks like that line is actually supposed to be the test condition in an if() statement. That should fix part of the problem. The second part of the problem is that it is still within the while statement that traverses the trie. You need to move the test outside of the while loop. The if() test should not be done until you get to the last letter in the word being searched.

There may be other issues, but this is a big one. IF this answers your question, please click on the check mark to accept. Let's keep uo on forum maintenance. ;-)

2
  • Thanks for pointing on that! :) I'm still trying to fix my function. – Yana Mikhnich Dec 19 '15 at 21:37
  • It was a tiny typo, 122 is a lowercase "z", not 112, in my load and check. I've copied it to check from my load. Do I need to delete my load and check functions from here? – Yana Mikhnich Dec 23 '15 at 20:44

You must log in to answer this question.

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