30 trie_t *t = malloc(
sizeof(*t));
33 if (!t->
root) { free(t);
return NULL; }
44 unsigned char u = (
unsigned char)c;
45 if (u >=
'a' && u <=
'z')
return u -
'a';
50 if (!t || !word)
return false;
52 for (
size_t i = 0; word[i] !=
'\0'; i++) {
54 if (j < 0)
return false;
66 if (!t || !s)
return NULL;
68 for (
size_t i = 0; s[i] !=
'\0'; i++) {
70 if (j < 0 || !cur->children[j])
return NULL;
82 return walk(t, prefix) != NULL;
struct trie_node * children[26]
bool trie_insert(trie_t *t, const char *word)
bool trie_contains(const trie_t *t, const char *word)
static int idx_of(char c)
struct trie_node trie_node_t
static const trie_node_t * walk(const trie_t *t, const char *s)
static trie_node_t * node_create(void)
bool trie_starts_with(const trie_t *t, const char *prefix)
static void node_destroy(trie_node_t *n)
void trie_destroy(trie_t *t)
trie_t * trie_create(void)
Prefix tree (trie) over lowercase ASCII letters a-z.