1 什么是前缀树
前缀树又叫字典树
typedef struct Trie {
struct Trie *child[26];
int end;
} Trie;
/** Initialize your data structure here. */
Trie* trieCreate()
{
Trie *t = (Trie*) malloc(sizeof(Trie));
memset(t, 0, sizeof(Trie));
t->end = 0;
return t;
}
/** Inserts a word into the trie. */
void trieInsert(Trie *obj, char *word)
{
while (*word) {
if (!obj->child[*word-'a'])
obj->child[*word-'a'] = trieCreate();
obj = obj->child[*word-'a'];
++word;
}
obj->end = 1;
}
/** Returns if the word is in the trie. */
bool trieSearch(Trie *obj, char *word)
{
while (*word) {
if (!obj->child[*word-'a'])//
return false;
obj = obj->child[*word-'a'];
word++;
}
return (obj->end);
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool trieStart