1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
int main(void)
{
//Create Root Node
Trie *Head = new Trie;
Head->SetFrequency(0);
Head->SetIsLeaf(false);
string w1 = "test";
string w2 = "testing";
AddWord(w1,Head);
AddWord(w2,Head);
string w3 = "testtest";
AddWord(w3,Head);
w3 = "testtesting";
AddWord(w3,Head);
w3 = "testingtest";
AddWord(w3,Head);
w3 = "testingtesting";
AddWord(w3,Head);
string word = "testing";
if(FindWord(w2,Head))
cout << "The word \"" << w2 << "\" has been found!" << endl;
else
cout << "The word \"" << w2 << "\" was not found!" << endl;
cout << "Prefix: " << w1 << endl;
cout << "-------" << endl;
string w_backup = w1;
AutoComplete(w1, Head, GetPrefixCount(w1,Head), w_backup, 0);
cout << endl;
cout << "Prefix: " << w2 << endl;
cout << "-------" << endl;
w_backup = w2;
AutoComplete(w2, Head, GetPrefixCount(w2,Head), w_backup, 0);
return 0;
}
bool FindWord(string &word, Trie *root)
{
Trie *new_word = root;
for(unsigned int i = 0 ; i < word.length(); i++)
{
int letter = (int)word[i] - (int)'a';
if(new_word->child[letter] == nullptr) //if not found
return false;
new_word = new_word->child[letter];
}
return new_word->GetIsLeaf();
}
void AddWord(string &word, Trie *root)
{
Trie *new_word = root;
new_word->SetFrequency(new_word->GetPrefixes()+1);
for(unsigned int 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;
}
new_word->child[letter]->SetPrefixes(new_word->child[letter]->GetPrefixes()+1);
new_word = new_word->child[letter];
}
new_word->SetIsLeaf(true);
new_word->SetFrequency(new_word->GetFrequency()+1);
//Debugging Purposes
/*
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(unsigned int 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(unsigned int 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->GetPrefixes();
}
void AutoComplete(string &prefix, Trie *root, int total, string origional_prefix, int counter)
{
if (counter == 0 || FindWord(origional_prefix,root))
{
//cout << "current word is: " << prefix << endl;
int frequency = FrequencyCount(origional_prefix, root);
cout << prefix << " - " << frequency << " occurrences" << endl;
total = total - root->GetFrequency();
counter++;
}
else
{
//cout << "word not found" << endl;
if (root->GetFrequency() > 0)
{
total = total - root->GetFrequency();
if (prefix.size() != 2*origional_prefix.size() && total != 0)
cout << prefix.substr(origional_prefix.size(),prefix.size()) << " - " << root->GetFrequency() << " occurrences" << endl;
}
if (root->GetFrequency() < 1)
{
//cout << "frequency is less than 1!" << endl;
total = total - root->GetFrequency();
}
else
total = total - root->GetFrequency();
}
cout << "current total is: " << total << endl;
if(total != 0 && total > 0)
{
for (int i = 0; i < 26; i++)
{
Trie *pointerChild = root->child[i];
if(pointerChild)
{
char nextChar = 'a'+i;
prefix.push_back(nextChar);
AutoComplete(prefix,pointerChild,total, origional_prefix, counter);
prefix.pop_back();
}
}
}
else
{
return;
}
}
|