bool FindWord(string &word, Trie *root)
{
Trie *new_word = root;
for(unsignedint i = 0 ; i < word.length(); i++)
{
int letter = (int)word[i] - (int)'a';
if(new_word->child[letter] == nullptr) //if not found
returnfalse;
new_word = new_word->child[letter];
}
return new_word->GetIsLeaf();
}
void AddWord(string &word, Trie *root)
{
Trie *new_word = root;
new_word->prefixes_++;
for(unsignedint i = 0 ; i < word.length(); i++)
{
int letter = (int)word[i] - (int)'a'; //extract character of word
if(new_word->child[letter] == nullptr)
{
new_word->child[letter] = new Trie;
}
/*cout << "not value of x: " << new_word->child[letter]->GetPrefixes() << endl;
int x = (new_word->child[letter]->GetPrefixes())+1;
cout << "value of x: " << x << endl;
new_word->child[letter]->SetPrefixes(x);*/
new_word->child[letter]->prefixes_++;
new_word = new_word->child[letter];
}
new_word->SetFrequency(new_word->GetFrequency()+1);
/*
cout << "Word: " << word << endl;
cout << "frequency: " << new_word->GetFrequency() << endl;
cout << "prefixes: " << new_word->GetPrefixes() << endl;
cout << "is leaf: " << new_word->GetIsLeaf() << endl << endl;
*/
}
int FrequencyCount(string &prefix, Trie *root)
{
Trie *word = root;
for(unsignedint i = 0 ; i < prefix.length(); i++)
{
int letter = (int)prefix[i] - (int)'a';
if(word->child[letter] == nullptr) //if not found
return 0;
else
word = word->child[letter];
}
return word->GetFrequency();
}
int GetPrefixCount(string &prefix, Trie *root)
{
Trie *current = root;
for(unsignedint i = 0; i < prefix.length() ; i++)
{
int letter = (int)prefix[i] - (int)'a';
if(current->child[letter] == nullptr)
return 0;
else
current = current->child[letter];
}
return current->prefixes_;
}
//void Trie::AutoComplete(string &prefix, Trie &root)
void AutoComplete(string &prefix, Trie *root, int total)
{
Trie *current = root;
for(unsignedint i = 0; i < prefix.length() ; i++)
{
int letter = (int)prefix[i] - (int)'a';
if(current->child[letter] != nullptr)
current = current->child[letter];
}
int frequency = FrequencyCount(prefix, root);
if (FindWord(prefix,root))
{
cout << prefix << " - " << frequency << " occurrences" << endl;
total = total - frequency;
}
if(total != 0)
{
//insert code here
}
else
{
return;
}
}
When I debug, I get an error in the AddTrie function. Specifically, this line:
new_word->child[letter]->prefixes_++;
In addition, I have no clue how to start with getting the other words. I think the above error has some impact on my results, but I'm not 100% sure. Any advice and information you can give is greatly appreciated!
When I debug, I get an error in the AddTrie function. Specifically, this line:
Sounds like a compiler error, not debugging. What was the exact text of the error - verbatim?
Speaking of debugging, try to use the debugger in your IDE. Create a watch list of variables, step through code 1 line at a time, deduce where things go wrong.