-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.c
164 lines (122 loc) · 3.78 KB
/
trie.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "trie.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Trie *trie_init(void) {
Trie *t = (Trie *) malloc(sizeof(Trie));
t->prefix = (char *) malloc((TRIE_PREFIX_SIZE + 1) * sizeof(char));
memset(t->prefix, '\0', (TRIE_PREFIX_SIZE + 1) * sizeof(char));
t->prefixSize = TRIE_PREFIX_SIZE;
t->matches = (char **) malloc(sizeof(char *) * TRIE_MATCHES_SIZE);
t->matchesCount = 0;
t->matchesSize = 1;
t->root = trie_node_create();
trie_match_set_null(t, 0, TRIE_MATCHES_SIZE);
return t;
}
void trie_match_set_null(Trie *t, int l, int u) {
for (int i = l; i < u; i++) {
t->matches[i] = NULL;
}
}
TrieNode *trie_node_create(void) {
TrieNode *node = (TrieNode *) malloc(sizeof(TrieNode));
if (node == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
for (int i = 0; i < CHARACTER_SET_SIZE; i++) {
node->children[i] = NULL;
}
node->is_end = 0;
return node;
}
void trie_insert(TrieNode *root, char *key) {
TrieNode *current = root;
unsigned long len = strlen(key);
for (unsigned long level = 0; level < len; level++) {
int index = (unsigned char) key[level];
if (!current->children[index]) {
current->children[index] = trie_node_create();
}
current = current->children[index];
}
current->is_end = 1;
}
void trie_search(Trie *t, char *key) {
TrieNode *current = t->root;
unsigned long len = strlen(key);
unsigned long prefix_size = len;
char *tmp;
for (unsigned long level = 0; level < len; level++) {
int index = (unsigned char) key[level];
if (!current->children[index]) {
return;
}
if (prefix_size >= TRIE_PREFIX_SIZE) {
tmp = realloc(t->prefix, (prefix_size + 1) * sizeof(char));
if (!tmp) {
// TODO
} else {
t->prefix = tmp;
}
}
if (t->prefix == NULL) {
perror("realloc");
exit(EXIT_FAILURE);
}
t->prefix[level] = key[level];
t->prefix[level + 1] = '\0';
current = current->children[index];
}
trie_search_helper(current, t, len);
}
void trie_search_helper(TrieNode *current, Trie *t, unsigned long level) {
if (current->is_end) {
if (t->matchesCount + 1 >= TRIE_MATCHES_SIZE * t->matchesSize) {
char **tmp = realloc(t->matches, ++t->matchesSize * TRIE_MATCHES_SIZE * sizeof(char *));
if (!tmp) {
// TODO
} else {
t->matches = tmp;
}
trie_match_set_null(t, t->matchesCount, t->matchesSize * TRIE_MATCHES_SIZE);
}
if (t->matches == NULL) {
perror("realloc");
exit(EXIT_FAILURE);
}
if (t->matches[t->matchesCount] != NULL) {
free(t->matches[t->matchesCount]);
}
t->matches[t->matchesCount++] = strdup(t->prefix);
}
for (int i = 0; i < CHARACTER_SET_SIZE; i++) {
if (current->children[i]) {
t->prefix[level] = (char) i;
trie_search_helper(current->children[i], t, level + 1);
t->prefix[level] = '\0';
}
}
}
void trie_print_words(TrieNode *root, char *prefix, unsigned long level) {
if (root->is_end) {
prefix[level] = '\0';
printf("%s\n", prefix);
}
for (int i = 0; i < CHARACTER_SET_SIZE; i++) {
if (root->children[i]) {
prefix[level] = (char) i;
trie_print_words(root->children[i], prefix, level + 1);
}
}
}
void trie_free(TrieNode *root) {
if (root == NULL) {
return;
}
for (int i = 0; i < CHARACTER_SET_SIZE; i++) {
trie_free(root->children[i]);
}
free(root);
}