【发布时间】:2015-10-21 00:02:10
【问题描述】:
所以我正在实现一个用于从文件中读取唯一单词的 trie。我在网上寻找如何实现它并遇到了这种方式: //将字符串插入到trie树中 '
void insert(struct node *head, string str)
{
int i, j;
for(i = 0;i < str.size(); ++i){
//if the child node is pointing to NULL
if(head -> next_char[str[i] - 'a'] == NULL){
struct node *n;
//initialise the new node
n = new struct node;
for(j = 0;j < 26; ++j){
n -> next_char[j] = NULL;
}
n -> end_string = 0;
head -> next_char[str[i] - 'a'] = n;
head = n;
}
//if the child node is not pointing to q
else head = head -> next_char[str[i] - 'a'];
}
//to mark the end_string flag for this string
head -> end_string = 1;
}
我的困惑来自于: 'head -> next_char[str[i] - 'a'] == NULL 在这段代码实现它的所有方式中使用“a”的减法的目的是什么?
【问题讨论】:
标签: data-structures trie