C++ c++;无法让trie提供正确的搜索词
我正在创建一个word建议自动完成界面。我用C++中的TIE来做这个。我想在我的代码中输入一个单词的一部分,并让trie建议可能的单词来完成我的单词结尾C++ c++;无法让trie提供正确的搜索词,c++,trie,C++,Trie,我正在创建一个word建议自动完成界面。我用C++中的TIE来做这个。我想在我的代码中输入一个单词的一部分,并让trie建议可能的单词来完成我的单词结尾 #include<bits/stdc++.h> using namespace std; #define alphabet (26) #define CHAR_TO_INDEX(c) ((int)c - (int)'a') struct TrieNode { struct TrieNode *children
#include<bits/stdc++.h>
using namespace std;
#define alphabet (26)
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
struct TrieNode
{
struct TrieNode *children[alphabet];
// isWordEnd is true if the node represents
// end of a word
bool isWordEnd;
};
struct TrieNode *getNode(void)
{
struct TrieNode *Node = new TrieNode;
Node->isWordEnd = false;
for (int i = 0; i < alphabet; i++)
Node->children[i] = NULL;
return Node;
}
void insert(struct TrieNode *root, const string key)
{
struct TrieNode *Crawl = root;
for (int level = 0; level < key.length(); level++)
{
int index = CHAR_TO_INDEX(key[level]);
if (!Crawl->children[index])
{
Crawl->children[index] = getNode();
//Crawl = Crawl->children[index];
}
Crawl = Crawl->children[index];
}
// mark last node as leaf
Crawl->isWordEnd = true;
}
//returns 0 if current node has a child
// If all children are NULL, return 1.
bool isLastNode(struct TrieNode* root)
{
for (int i = 0; i < alphabet; i++)
if (root->children[i])
return 0;
return 1;
}
void suggestionsRec(struct TrieNode* root, string currPrefix)
{
// found a string in Trie with the given prefix
if (root->isWordEnd)
{
cout << currPrefix;
cout << endl;
}
// All children struct node pointers are NULL
if (isLastNode(root))
{
//currPrefix = "help";
//deleteNode(root);
//delete root;
//root = NULL;
//currPrefix.pop_back();
return;
}
for (int i = 0; i < alphabet; i++)
{
if (root->children[i])
{
currPrefix.push_back(97 + i);
//currPrefix.push_back(i);
//currPrefix.pop_back(97 + i);
/*if (isLastNode(root))
{
currPrefix.erase(3);
}*/
// recur over the rest
suggestionsRec(root->children[i], currPrefix);
//printAutoSuggestions(root->children[i], currPrefix);
}
}
}
// print suggestions for given query prefix.
int printAutoSuggestions(TrieNode* root, const string query)
{
struct TrieNode* Crawl = root;
// Check if prefix is present and find the
// the node (of last level) with last character
// of given string.
int level;
int n = query.length();
for (level = 0; level < n; level++)
{
int index = CHAR_TO_INDEX(query[level]);
// no string in the Trie has this prefix
if (!Crawl->children[index])
return 0;
Crawl = Crawl->children[index];
}
// If prefix is present as a word.
bool isWord = (Crawl->isWordEnd == true);
// If prefix is last node of tree (has no
// children)
bool isLast = isLastNode(Crawl);
// If prefix is present as a word, but
// there is no subtree below the last
// matching node.
if (isWord && isLast)
{
cout << query << endl;
return -1;
}
// If there are are nodes below last
// matching character.
if (!isLast)
{
string prefix = query;
suggestionsRec(Crawl, prefix);
return 1;
}
}
// Driver Code
int main()
{
struct TrieNode* root = getNode();
insert(root, "hello");
insert(root, "dog");
insert(root, "hell");
insert(root, "cat");
insert(root, "a");
insert(root, "hel");
insert(root, "help");
insert(root, "helps");
insert(root, "helping");
int comp = printAutoSuggestions(root, "hel");
if (comp == -1)
cout << "No other strings found with this prefix\n";
else if (comp == 0)
cout << "No string found with this prefix\n";
return 0;
}
#包括
使用名称空间std;
#定义字母表(26)
#定义字符到索引(c)((int)c-(int)“a”)
结构三节点
{
结构三元组*子级[字母表];
//如果节点表示
//词尾
布尔·伊斯沃登德;
};
结构三节点*getNode(无效)
{
结构三节点*节点=新三节点;
Node->isWordEnd=false;
for(int i=0;i子节点[i]=NULL;
返回节点;
}
无效插入(结构三节点*根,常量字符串键)
{
结构三节点*爬网=根;
对于(int-level=0;level子项[索引])
{
爬网->子项[index]=getNode();
//爬网=爬网->子项[索引];
}
爬网=爬网->子项[索引];
}
//将最后一个节点标记为叶
爬网->iWordEnd=true;
}
//如果当前节点有子节点,则返回0
//如果所有子项都为NULL,则返回1。
布尔isLastNode(结构三节点*根)
{
for(int i=0;ichildren[i])
返回0;
返回1;
}
void suggestionsRec(结构三节点*根,字符串前缀)
{
//在Trie中找到具有给定前缀的字符串
if(root->isWordEnd)
{
cout children[i],前缀);
//打印自动建议(root->children[i],currPrefix);
}
}
}
//打印给定查询前缀的建议。
int printAutoSuggestions(三节点*根,常量字符串查询)
{
结构三节点*爬网=根;
//检查前缀是否存在并查找
//具有最后一个字符的节点(最后一级)
//给定字符串的。
智力水平;
int n=query.length();
对于(级别=0;级别子项[索引])
返回0;
爬网=爬网->子项[索引];
}
//如果前缀作为单词出现。
bool-isWord=(爬网->isWordEnd==true);
//如果前缀是树的最后一个节点(没有
//(儿童)
bool isLast=isLastNode(爬网);
//如果前缀作为单词出现,但是
//最后一个树下面没有子树
//匹配节点。
if(isWord&&isLast)
{
cout在suggestionsRec(…)
中,您有:
for (int i = 0; i < alphabet; i++)
{
currPrefix.push_back(97 + i);
...
suggestionsRec(root->children[i], currPrefix);
}
}
for(int i=0;ichildren[i],currPrefix);
}
}
您正在将字符添加到currPrefix
并保留它们。因此,您在以后的子级上调用suggestionsRec
,使用currPrefix
中不属于那里的字符。您是否尝试在调试器中运行此操作?您忘记提供struct triode、getNode()的定义,字母表和字符到索引sorry,复制和粘贴搞笑,只是添加了这些。