내 트라이에 "all"이라는 단어가 있고 "alt"라는 단어가 있지만 "alt"는 트라이의 단어가 아닙니다. 그러나 "alt"를 검사 할 때 "all"이 단어이므로 is_word가 true이기 때문에 여전히 true를 반환합니다. 이 오류를 어떻게 처리해야합니까?trie의 단어 차별화
//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;
}
일부 모호한 점이있다 ... 먼저 하나 : 왜'나 strlen (워드) -1'? 둘째 : 당신은 어떻게 당신의 트라이를 채우고 있습니까? – Fefux
trie를 입력하기 위해 별도의 함수로드를했고 strlen (word) -1을 사용했습니다. 마지막 노드에 단어가 들어 있는지 두 번째 마지막 노드로 이동 한 다음 포인터 검사를 사용해야하기 때문입니다. –
로드 기능을 게시 할 수 있습니까? – Fefux