-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtrie.c
More file actions
92 lines (74 loc) · 1.87 KB
/
trie.c
File metadata and controls
92 lines (74 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Trie set.
Unlike single struct BSTs, we can represent empty tries without the NULL pointer:
TODO:
- free
- is_empty
- find with prefix
- typo correction
*/
#include <assert.h>
#include <stdlib.h>
#define ALPHABET_SIZE 8
typedef unsigned int t;
typedef t ts[];
typedef struct Trie Trie;
struct Trie {
Trie *children[ALPHABET_SIZE];
int is_word;
};
void Trie_init(Trie **trie) {
size_t i;
*trie = malloc(sizeof(Trie));
(*trie)->is_word = 0;
for (i = 0; i < ALPHABET_SIZE; ++i)
(*trie)->children[i] = NULL;
}
int Trie_add(Trie *trie, t *word, size_t size) {
size_t i;
int present;
Trie **next;
for (i = 0; i < size; ++i) {
next = &trie->children[word[i]];
if (*next == NULL)
Trie_init(next);
trie = *next;
}
present = trie->is_word;
trie->is_word = 1;
return present;
}
int Trie_find(Trie *trie, t *word, size_t size) {
size_t i;
for (i = 0; i < size; ++i) {
trie = trie->children[word[i]];
if (trie == NULL)
return 0;
}
if (trie->is_word)
return 1;
return 0;
}
int main(void) {
Trie *trie = NULL;
Trie_init(&trie);
/* find on empty trie. */
assert(!Trie_find(trie, (ts){0}, 1));
/* Add and find length 1. */
assert(!Trie_add(trie, (ts){0}, 1));
assert(Trie_find(trie, (ts){0}, 1));
assert(!Trie_find(trie, (ts){1}, 1));
/* Add previously added. */
assert(Trie_add(trie, (ts){0}, 1));
/* Add and find length 3 */
{
assert(Trie_find(trie, (ts){0}, 1));
assert(!Trie_find(trie, (ts){0, 1}, 2));
assert(!Trie_find(trie, (ts){0, 1, 2}, 3));
assert(!Trie_add(trie, (ts){0, 1, 2}, 3));
assert(Trie_find(trie, (ts){0}, 1));
assert(!Trie_find(trie, (ts){0, 1}, 2));
assert(Trie_find(trie, (ts){0, 1, 2}, 3));
}
return EXIT_SUCCESS;
}