A company dictory is launching a new dictionary application for mobile users. initially, the dictionary will not have any words. instead it will be an auto-learning application that will learn according to a user's given text. when a user types text, the application auto-detects the words that appear more than once. the application then stores these words in the dictionary and uses them as suggestions in future typing sessions. write an algorithm to identify which words will be saved in the dictionary. write a program in c
Answers
Answer:
The answer to the given question is given in the explanation.
Explanation:
Given a query prefix, we search for all the words that have that query.
- Find the given query using the standard Trie search algorithm.
- If the query prefix itself is not present, return -1 to indicate that.
- If the query is present and matches the end of a word in the Trie, print the query. This can be quickly checked by seeing if the last matching node has the isEndWord flag set. We use this flag in Trie to mark the end of word nodes for search purposes.
- If the last matching node in the query has no child nodes, return.
- Else recursively prints all nodes under the subtree of the last matched node.
code in c++ is given below:
/ C++ programme to show auto-complete functionality
utilising the Trie data structure, and
#include using the std namespace;
/ Define ALPHABET SIZE (alphabet size, number of symbols) (26)
Use only lowercase letters "a" through "z" and the key current character (converts it to an index) #define ((int)c - (int)'a') CHAR TO INDEX(c)
/ children[ALPHABET SIZE] of the trie node struct TrieNode;
bool isWordEnd; // isWordEnd is true if the node reflects the end of a word.
Returns a new trie node in / (initialized to NULLs)
new TrieNode = struct TrieNode* pNode;
pNode->isWordEnd = false;
struct TrieNode* getNode(void);
pNode->children[i] = NULL for (int I = 0; I ALPHABET SIZE; i++);
bring back pNode;
/ Inserts key into trie if not already there. Just mark the leaf node void insert(struct TrieNode* root, const string key) if the / key is the trie node's prefix.
pCrawl=struct TrieNode* root;
level++ for (int level = 0;
level key.length());
Integer index: CHAR TO INDEX(key[level]);
if pCrawl->children[index] = getNode() (!pCrawl->children[index]);
pCrawl = children[index]->pCrawl;
Mark the final node as a leaf by setting pCrawl->isWordEnd to true.
Returns 0 in the event that the current node has a child. Return 1 if all of the children are NULL.
bool isLastNode(struct TrieNode* root)
for (int I = 0; I ALPHABET SIZE; i++)
if (root->children[i])
return 0,
else return 1.
/ Print auto-suggestions using recursive function for provided
void suggestions. / node
Rec(struct TrieNode* root, string currPrefix) /
if (root->isWordEnd)
there is a string in Trie with the specified prefix.
currPrefix cout endl;
suggestionsRec(root->children[i], currPrefix + child);
for (int I = 0; I ALPHABET SIZE; i++)
if (root->children[i])
/ child node character value char child = 'a' + I
/ print ideas for the specified query prefix.
TrieNode* root, int printAutoSuggestions(const string query),
for (char c: query)
int ind = CHAR TO INDEX(c);
struct TrieNode* pCrawl = root;
if (!pCrawl->children[ind])
returns 0
and no string in the Trie has this prefix, then;
Children[ind] = pCrawl->children;
If the prefix appears as a word but there is no subtree below the final matching node, the prefix is present.
isLastNode(pCrawl)
if Ending with "cout" and returning -1 is an error.
returns 1 from suggestionsRec(pCrawl, query);
#SPJ1
Answer:
/ C++ programme to show auto-complete functionality
utilising the Trie data structure, and
#include using the std namespace;
/ Define ALPHABET SIZE (alphabet size, number of symbols) (26)
Use only lowercase letters "a" through "z" and the key current character (converts it to an index) #define ((int)c - (int)'a') CHAR TO INDEX(c)
/ children[ALPHABET SIZE] of the trie node struct TrieNode;
bool isWordEnd; // isWordEnd is true if the node reflects the end of a word.
Returns a new trie node in / (initialized to NULLs)
new TrieNode = struct TrieNode* pNode;
pNode->isWordEnd = false;
struct TrieNode* getNode(void);
pNode->children[i] = NULL for (int I = 0; I ALPHABET SIZE; i++);
bring back pNode;
/ Inserts key into trie if not already there. Just mark the leaf node void insert(struct TrieNode* root, const string key) if the / key is the trie node's prefix.
pCrawl=struct TrieNode* root;
level++ for (int level = 0;
level key.length());
Integer index: CHAR TO INDEX(key[level]);
if pCrawl->children[index] = getNode() (!pCrawl->children[index]);
pCrawl = children[index]->pCrawl;
Mark the final node as a leaf by setting pCrawl->isWordEnd to true.
Returns 0 in the event that the current node has a child. Return 1 if all of the children are NULL.
bool isLastNode(struct TrieNode* root)
for (int I = 0; I ALPHABET SIZE; i++)
if (root->children[i])
return 0,
else return 1.
/ Print auto-suggestions using recursive function for provided
void suggestions. / node
Rec(struct TrieNode* root, string currPrefix) /
if (root->isWordEnd)
there is a string in Trie with the specified prefix.
currPrefix cout endl;
suggestionsRec(root->children[i], currPrefix + child);
for (int I = 0; I ALPHABET SIZE; i++)
if (root->children[i])
/ child node character value char child = 'a' + I
/ print ideas for the specified query prefix.
TrieNode* root, int printAutoSuggestions(const string query),
for (char c: query)
int ind = CHAR TO INDEX(c);
struct TrieNode* pCrawl = root;
Explanation:
We look for all the terms that contain a certain query prefix.
Use the common Trie search technique to find the requested information.
Return -1 to indicate that the query prefix itself is missing if it exists.
Print the query if it exists and a word end in the Trie matches it. If the isEndWord flag is set on the final matching node, this may be rapidly verified. This flag is used in Trie to indicate a word node's end for the purpose of searching.
Return whether the final matched node in the query has no subnodes.
If not, recursively prints every node in the last-matched node's subtree.
code in c++ is given below:
/ C++ programme to show auto-complete functionality
utilising the Trie data structure, and
#include using the std namespace;
/ Define ALPHABET SIZE (alphabet size, number of symbols) (26)
Use only lowercase letters "a" through "z" and the key current character (converts it to an index) #define ((int)c - (int)'a') CHAR TO INDEX(c)
/ children[ALPHABET SIZE] of the trie node struct TrieNode;
bool isWordEnd; // isWordEnd is true if the node reflects the end of a word.
Returns a new trie node in / (initialized to NULLs)
new TrieNode = struct TrieNode* pNode;
pNode->isWordEnd = false;
struct TrieNode* getNode(void);
pNode->children[i] = NULL for (int I = 0; I ALPHABET SIZE; i++);
bring back pNode;
/ Inserts key into trie if not already there. Just mark the leaf node void insert(struct TrieNode* root, const string key) if the / key is the trie node's prefix.
pCrawl=struct TrieNode* root;
level++ for (int level = 0;
level key.length());
Integer index: CHAR TO INDEX(key[level]);
if pCrawl->children[index] = getNode() (!pCrawl->children[index]);
pCrawl = children[index]->pCrawl;
Mark the final node as a leaf by setting pCrawl->isWordEnd to true.
Returns 0 in the event that the current node has a child. Return 1 if all of the children are NULL.
bool isLastNode(struct TrieNode* root)
for (int I = 0; I ALPHABET SIZE; i++)
if (root->children[i])
return 0,
else return 1.
/ Print auto-suggestions using recursive function for provided
void suggestions. / node
Rec(struct TrieNode* root, string currPrefix) /
if (root->isWordEnd)
there is a string in Trie with the specified prefix.
currPrefix cout endl;
suggestionsRec(root->children[i], currPrefix + child);
for (int I = 0; I ALPHABET SIZE; i++)
if (root->children[i])
/ child node character value char child = 'a' + I
/ print ideas for the specified query prefix.
TrieNode* root, int printAutoSuggestions(const string query),
for (char c: query)
int ind = CHAR TO INDEX(c);
struct TrieNode* pCrawl = root;
if (!pCrawl->children[ind])
returns 0
and no string in the Trie has this prefix, then;
Children[ind] = pCrawl->children;
If the prefix appears as a word but there is no subtree below the final matching node, the prefix is present.
isLastNode(pCrawl)
if Ending with "cout" and returning -1 is an error.
returns 1 from suggestionsRec(pCrawl, query);
#SPJ2