autocomplete
TRANSCRIPT
-
7/23/2019 autocomplete
1/6
#include #include #include #include #include #include
using namespace std;
#define NR 27 // the American alphabet(26 letters) plus blank.
typedef struct TrieNode{bool NotLeaf; // indicates if the TrieNode struct is a leaf o
r an intern nodeTrieNode *pChildren[NR]; // a list of pointers corresponding to the used
alphabetchar word[20]; // the string stored in node
};
//function for creating a leaf nodeTrieNode *NewLeaf(char keyWord[20]){
TrieNode *node;int count;
//allocating the necessary memorynode = (TrieNode *)malloc(sizeof(TrieNode));
for(count = 0; count < 27; count++) //the terminal nodes don't have children
node->pChildren[count] = NULL;node->NotLeaf = false; // the node is a leaf
strcpy(node->word,keyWord); //store in the structure(node->word) the string
return node;
}
//function for creating a intern nodeTrieNode *NewIntern(){
TrieNode *node;int count;
//allocating the necessary memorynode = (TrieNode *)malloc(sizeof(TrieNode));
for(count = 0; count < 27; count++) // initial the intern node don'thave children
node->pChildren[count] = NULL;node->NotLeaf = true; //it isn't a leafnode->word[0] = 0; //so we store the null string in node
return node;}
//function performs a search in the TRIE when a string or key is passed.void Find(TrieNode *trie, char keyWord[20]){
-
7/23/2019 autocomplete
2/6
TrieNode *next, *index, *data;int count;
next = trie; //start searching from the trie root
if(next == NULL) //trie is empty{
cout pChildren[keyWord[count]-'a'] != NULL))
{// .... go down with 1 levelnext = index->pChildren[keyWord[count]-'a']; // the actual node
goes down with 1 level in trie following the pChildren field // corresponding t
o the actual letter from keyWordindex = next;count ++ ; //move right in word with 1 level (1 letter)
}
if(next == NULL)cout word,keyWord))cout
-
7/23/2019 autocomplete
3/6
re the new wordtrie->pChildren[keyWord[0]-'a'] = new_leaf; //add the leaf into
the trie in the corresponding position
return trie;}else // non empty trie ..start searching
index = next;
inWordIndex = 0; //move down in trie while end of word isn't reached and the pChildren branch node doesn't leads to NULL
while((inWordIndex < lenght) &&(index->NotLeaf == true)&&(index->pChildren[keyWord[inWordIndex]-'a'] != NULL))
{ // .... go down with 1 levelparent = next; //set as parent the actual nodenext = index->pChildren[keyWord[inWordIndex]-'a']; // the actu
al node goes down with 1 level in trie following the pChildren field // correspo
nding to the actual letter from keyWordindex = next;
inWordIndex++; //move right in word with 1 level (1 letter)}
// if pChildren branch node points to NULL(end of prefix is reached) andno word already inserted, simply insert the word
if((inWordIndex < lenght) && (index->pChildren[keyWord[inWordIndex]-'a']== NULL) && (index->NotLeaf == true))
{new_index = NewLeaf(keyWord);index->pChildren[keyWord[inWordIndex]-'a'] = new_index;
return trie;}else
data=next;
if(!strcmp(data->word,keyWord))cout word[0] != '\0') if(strlen(data->word) < prefixLenght)// determine the minimum
lenght of words prefixLenght = strlen(data->word);
}
bool createIntern = false;// Build a new subtree while the word to be inserted and the item that w
as in the leaf node have the same letters or the end of one of them is reached.while((inWordIndex word[0] != '\0' )&& (data-
>word[inWordIndex-1] == keyWord[inWordIndex-1])) || (data->word[0] == '\0'))){
-
7/23/2019 autocomplete
4/6
intern = NewIntern();//create a new intern nodeparent->pChildren[keyWord[inWordIndex-1]-'a'] = intern; //insert
this node in the corresponding field in parent->pChildren(with respect to the letter index in array)
parent->NotLeaf = true;parent = intern; // move down in tree with 1 levelinWordIndex++; // move right in word with 1 letter
createIntern = true;}if(createIntern) inWordIndex--;
//if items have a common prefixif((inWordIndex != prefixLenght) || ((inWordIndex == prefixLenght)&&(str
len(keyWord) == strlen(data->word)))){//store in leaves the item that was to be inserted and the item that wa
s originally in the leaf node.parent->pChildren[data->word[inWordIndex]-'a'] = oldChildren;parent->pChildren[keyWord[inWordIndex]-'a'] = newWord;
}else // one word (keyWord or an item that was originally in the leaf nod
e) represents a prefix for the other(s) item(s)if(data->word[0] != '\0')//just a word that has as prefix the ke
yword or vice versa // insert the items as information nodes corresponding to the pChildren fields of prefixLenght and blank character
if(strlen(data->word) pChildren[26] = oldChildren;parent->pChildren[keyWord[prefixLenght]-'a'] = newWord;
}else{
parent->pChildren[26] = newWord;parent->pChildren[data->word[prefixLenght]-'a'] = oldChi
ldren;
}else// Two or more words that have the same prefix{
for (int count = 0 ; count < 27;count++)//copy the subtree
parent->pChildren[count] = oldChildren->pChildren[count];
parent->pChildren[26] = newWord;//newWord is the prefix(save in blank pointer)
}
return trie;}
//function for displaying the words stored in the trievoid DisplayTrie(TrieNode *trie, int nivel){
int count;
if(trie){
if (trie->NotLeaf != true) // if TrieNode is a leaf(a word is stored in)
-
7/23/2019 autocomplete
5/6
{ // display the string at his levelfor (count = 0; count NotLeaf != true && strncmp( str ,trie -> word,strlen(str) )== 0 ) // if TrieNode is a leaf(a word is stored in)
{
cout word = 0; count--) Complete(trie->pChildren[count], str);
}
}
int main(){
TrieNode *trie;char UserInputWord[20], cont_insert=' ';int option = 0; //stores the user's input(the chosen option)
char str[20] ;
trie = NULL;
label_menu:while( option != 5){
//display menucout
-
7/23/2019 autocomplete
6/6
cout UserInputWord;
trie = Insert(trie,UserInputWord);
cout