トライに「すべて」という単語があり、「変更」という単語がありますが、「トート」の単語は「トライ」ではありません。しかし、 "alt"をチェックすると、 "all"が単語だったのでis_wordが真であるため、それでもtrueを返します。どのようにこのエラーを処理するはずです。トライで言葉を区別する
//Here's the code
typedef struct node{
bool is_word;
struct node *children[27];
} node;
unsigned int wsize=0;
node * root;
bool check(const char* word)
{
// TODO
node *chrawler=root;
for(int i=0;i<strlen(word)-1;i++)
{
int t;
if(word[i]>=65&&word[i]<=90)
{
t=word[i]-'A';
}
else if(isalpha(word[i]))
t=word[i]-'a';
else
t=26;
if(chrawler->children[t]==NULL)
return false;
else
chrawler=chrawler->children[t];
}
if(chrawler->is_word)
return true;
return false;
}
// Load function
bool load(const char* dictionary)
{
// TODO
FILE *inptr=fopen(dictionary,"r");
if(inptr==NULL)
{
return false;
}
node *new_node=malloc(sizeof(node));
root=new_node;
char * word=malloc((LENGTH+1)*sizeof(char));
int index=0;
for(int c=fgetc(inptr);c!=EOF;c=fgetc(inptr))
{
char ch=c;
if(ch=='\n')
{
word[index]='\0';
index=0;
node *chrawler=root;
for(int i=1;i<strlen(word);i++)
{
int t;
if(isalpha(word[i-1]))
t=word[i-1]-'a';
else
t=26;
if(chrawler->children[t]==NULL)
{
node *new_node=malloc(sizeof(node));
chrawler->children[t]=new_node;
chrawler=chrawler->children[t];
}
else
chrawler=chrawler->children[t];
}
chrawler->is_word=1;
wsize++;
}
else
{
word[index]=ch;
index++;
}
}
return true;
}
いくつかのあいまいな点がある...まず1:なぜ 'strlenを(単語) -1?第二:あなたのトライをどのように人口に入れますか? – Fefux
trieを入力するために、私は別の関数のロードを行い、最後のノードに単語が含まれているかどうかを確認するために、2番目の最後のノードに移動してポインタチェックを使用する必要があるためstrlen(word)-1を使用しました。 –
あなたの読み込み機能を投稿できますか? – Fefux