我的Trie程序在C++;有逻辑错误 我试图用Link和Trie来实现C++中的Trie,但是它有一些逻辑错误,我无法跟踪。在程序中,我希望输入字符串作为插入的输入,并结束我提供的“#”字符的输入模式。 它切换到搜索模式,但它打印的每个查询
我的Trie程序在C++;有逻辑错误 <>我试图用Link和Trie来实现C++中的Trie,但是它有一些逻辑错误,我无法跟踪。在程序中,我希望输入字符串作为插入的输入,并结束我提供的“#”字符的输入模式。 它切换到搜索模式,但它打印的每个查询都“未找到”。 我正在使用编译器VC12-Visual Studio 2013社区我的Trie程序在C++;有逻辑错误 我试图用Link和Trie来实现C++中的Trie,但是它有一些逻辑错误,我无法跟踪。在程序中,我希望输入字符串作为插入的输入,并结束我提供的“#”字符的输入模式。 它切换到搜索模式,但它打印的每个查询,c++,search,insert,linked-list,trie,C++,Search,Insert,Linked List,Trie,我的Trie程序在C++;有逻辑错误 我试图用Link和Trie来实现C++中的Trie,但是它有一些逻辑错误,我无法跟踪。在程序中,我希望输入字符串作为插入的输入,并结束我提供的“#”字符的输入模式。 它切换到搜索模式,但它打印的每个查询都“未找到”。 我正在使用编译器VC12-Visual Studio 2013社区 #include <iostream> #include <string> #include <cctype> using
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
enum state{
acceptable, na
};
class TrieNode{
void setNULL(){
for (int i = 0; i < 26; i++)
{
next[i] = NULL;
}
}
public:
state s;
TrieNode *next[26];
TrieNode(){
setNULL();
s = na;
}
TrieNode(state s){
this->s = s;
setNULL();
}
TrieNode *&goNext(char ch){
ch = tolower(ch);
return next[ch - 'a'];
}
};
class Trie{
TrieNode *head[26];
void setNULL(){
for (int i = 0; i < 26; i++)
{
head[i] = NULL;
}
}
public:
Trie(){
setNULL();
}
void insert(string &s){
TrieNode *ptr = head[s[0] - 'a'];
if (ptr == NULL){
ptr = new TrieNode();
}
int i = 1;
while (i < s.length())
{
if (ptr->goNext(s[i]) == NULL){
ptr->goNext(s[i]) = new TrieNode();
}
ptr = ptr->goNext(s[i]);
i++;
}
ptr->s = acceptable;
}
bool search(string &s){
TrieNode *ptr = head[s[0] - 'a'];
int i = 1;
bool found = true;
while (i < s.length() && found){
if (ptr == NULL || ptr->goNext(s[i]) == NULL)
found = false;
else
ptr = ptr->goNext(s[i]);
}
if (found == true)
return true;
return false;
}
};
int main(){
Trie t;
string s;
while (cin >> s, s != "#"){
t.insert(s);
}
cout << "Search mode\n";
while (cin >> s, s != "#"){
if (t.search(s))
cout << "Found\n";
else
cout << "Not found\n";
}
/*
Sample Input:
hello
how
are
you
#
hello
*/
return 0;
}
#包括
#包括
#包括
使用名称空间std;
枚举状态{
可以接受,na
};
类三节点{
void setNULL(){
对于(int i=0;i<26;i++)
{
next[i]=NULL;
}
}
公众:
s国;
三元组*next[26];
三节点(){
setNULL();
s=na;
}
三节点(州s){
这->s=s;
setNULL();
}
三元组*&goNext(字符组){
ch=托洛尔(ch);
返回下一个[ch-'a'];
}
};
三类{
三节点*头[26];
void setNULL(){
对于(int i=0;i<26;i++)
{
head[i]=NULL;
}
}
公众:
Trie(){
setNULL();
}
空白插入(字符串和s){
三节点*ptr=头[s[0]-'a'];
如果(ptr==NULL){
ptr=新的三节点();
}
int i=1;
而(igoNext(s[i])==NULL){
ptr->goNext(s[i])=新的三节点();
}
ptr=ptr->goNext(s[i]);
i++;
}
ptr->s=可接受;
}
布尔搜索(字符串和s){
三节点*ptr=头[s[0]-'a'];
int i=1;
布尔发现=真;
while(igoNext(s[i])==NULL)
发现=错误;
其他的
ptr=ptr->goNext(s[i]);
}
if(find==true)
返回true;
返回false;
}
};
int main(){
特里特;
字符串s;
而(cin>>s,s!=“#”){
t、 插入(s);
}
cout>s,s!=“#”){
如果(t.搜索)
修改后的最后一个程序。谢谢大家的帮助
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
enum state{
acceptable, na
};
class TrieNode{
void setNULL(){
for (int i = 0; i < 26; i++)
{
next[i] = NULL;
}
}
public:
state s;
TrieNode *next[26];
TrieNode(){
setNULL();
s = na;
}
TrieNode(state s){
this->s = s;
setNULL();
}
TrieNode *&goNext(char ch){
ch = tolower(ch);
return next[ch - 'a'];
}
};
class Trie{
TrieNode *head[26];
void setNULL(){
for (int i = 0; i < 26; i++)
{
head[i] = NULL;
}
}
public:
Trie(){
setNULL();
}
void insert(string &s){
TrieNode *ptr = head[s[0] - 'a'];
if (ptr == NULL){
ptr = new TrieNode();
head[s[0] - 'a'] = ptr;
}
int i = 1;
while (i < s.length())
{
if (ptr->goNext(s[i]) == NULL){
ptr->goNext(s[i]) = new TrieNode();
}
ptr = ptr->goNext(s[i]);
i++;
}
ptr->s = acceptable;
}
bool search(string &s){
TrieNode *ptr = head[s[0] - 'a'];
if (ptr == NULL)
return false;
int i = 1;
bool found = true;
while (i < s.length() && found){
if (ptr == NULL || ptr->goNext(s[i]) == NULL)
found = false;
else
ptr = ptr->goNext(s[i]);
i++;
}
if (ptr->s == acceptable)
return found;
return false;
}
};
int main(){
Trie t;
cout << "Note : Only small characters are acceptable\n";
string s;
while (cin >> s, s != "#"){
t.insert(s);
}
cout << "Search mode\n";
while (cin >> s, s != "#"){
if (t.search(s))
cout << setw(20) << "Found\n";
else
cout << setw(20) << "Not found\n";
}
/*
Sample Input:
hello
how
are
you
#
hello
*/
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
枚举状态{
可以接受,na
};
类三节点{
void setNULL(){
对于(int i=0;i<26;i++)
{
next[i]=NULL;
}
}
公众:
s国;
三元组*next[26];
三节点(){
setNULL();
s=na;
}
三节点(州s){
这->s=s;
setNULL();
}
三元组*&goNext(字符组){
ch=托洛尔(ch);
返回下一个[ch-'a'];
}
};
三类{
三节点*头[26];
void setNULL(){
对于(int i=0;i<26;i++)
{
head[i]=NULL;
}
}
公众:
Trie(){
setNULL();
}
空白插入(字符串和s){
三节点*ptr=头[s[0]-'a'];
如果(ptr==NULL){
ptr=新的三节点();
头[s[0]-'a']=ptr;
}
int i=1;
而(igoNext(s[i])==NULL){
ptr->goNext(s[i])=新的三节点();
}
ptr=ptr->goNext(s[i]);
i++;
}
ptr->s=可接受;
}
布尔搜索(字符串和s){
三节点*ptr=头[s[0]-'a'];
如果(ptr==NULL)
返回false;
int i=1;
布尔发现=真;
while(igoNext(s[i])==NULL)
发现=错误;
其他的
ptr=ptr->goNext(s[i]);
i++;
}
如果(ptr->s==可接受)
发现退货;
返回false;
}
};
int main(){
特里特;
cout>s,s!=“#”){
t、 插入(s);
}
cout>s,s!=“#”){
如果(t.搜索)
无法查找将节点添加到head
数组中的位置。-一个前缀树,您可以在其中搜索从第一个字符开始的包含的单词,这对于sql(例如“a%”很有用。旁注:if(found==true)return true;return false;
是一种非常复杂的编写return found;
的方法。在搜索()的循环中,您永远不会在中增加i
!感谢您的帮助…目前正在尝试上述更改