-
Notifications
You must be signed in to change notification settings - Fork 119
/
208.c
338 lines (272 loc) · 8.26 KB
/
208.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
/* I implement Trie using left-child right-sibling binary tree. */
/* https://en.wikipedia.org/wiki/Left-child_right-sibling_binary_tree */
/* It's not an easy way, the code may not be so elegant. */
/* We can use an array each node to store the children pointers in easy way. */
/* use 1 or 0 to alternate method */
#define LCRS 1
#if LCRS
struct TrieNode {
char val;
struct TrieNode *child; /* most left child */
struct TrieNode *sibling; /* brothers of the current child*/
};
/** Initialize your data structure here. */
struct TrieNode* trieCreate() {
struct TrieNode* dummy = (struct TrieNode *)malloc(sizeof(struct TrieNode));
dummy->val = 0;
dummy->child = NULL;
dummy->sibling = NULL;
return dummy;
}
/**
* Iterative Method
*/
/** Inserts a word into the trie. */
void insert(struct TrieNode* root, char* word) {
if (root == NULL || word == NULL) return;
struct TrieNode** p = &(root->child);
struct TrieNode* new_node = NULL;
while (*word != '\0') {
/* get common prefix */
while (*p) {
if ((*p)->val == *word) {
p = &((*p)->child);
word++;
}
else {
p = &((*p)->sibling);
}
}
new_node = trieCreate();
new_node->val = *word;
*p = new_node;
p = &((*p)->child); /* move forward */
word++;
}
/* put the null character in the tree */
new_node = trieCreate();
new_node->val = '\0';
/* no need to check *p
* 'cause p is new_node's child, *p always equals to NULL */
*p = new_node;
}
/** Returns if the word is in the trie. */
bool search(struct TrieNode* root, char* word) {
if (root == NULL || word == NULL) return false;
if (root->child == NULL) return false;
struct TrieNode* p = root->child;
while (p && *word != '\0') {
if (p->val == *word) {
p = p->child;
word++;
}
else {
p = p->sibling;
}
}
/* check if we have a null in the siblings */
while (p) {
if (p->val == '\0') return true;
p = p->sibling;
}
return false;
}
/** Returns if there is any word in the trie
that starts with the given prefix. */
bool startsWith(struct TrieNode* root, char* prefix) {
if (root == NULL || prefix == NULL) return false;
if (root->child == NULL) return false;
struct TrieNode* p = root->child;
while (p && *prefix != '\0') {
if (p->val == *prefix) {
p = p->child;
prefix++;
}
else {
p = p->sibling;
}
}
if (p && *prefix == '\0') return true;
else return false;
}
/** Deallocates memory previously allocated for the TrieNode. */
void trieFree(struct TrieNode* root) {
if (root == NULL) return;
/* Post order traversal (recursive) */
trieFree(root->child);
trieFree(root->sibling);
free(root);
}
/**
* Recursive Method (insert, search and startWith)
*/
void insert_r(struct TrieNode* root, char* word) {
if (root == NULL || word == NULL) return;
if (root->child != NULL) {
if (root->child->val == *word) {
if (*word == '\0') return;
insert_r(root->child, word + 1);
}
else {
/* get most right sibling */
struct TrieNode** p = &(root->child->sibling);
while (*p) {
if ((*p)->val == *word) {
if (*word == '\0') return;
insert_r(*p, word + 1);
return;
}
p = &((*p)->sibling);
}
struct TrieNode* new_node = trieCreate();
new_node->val = *word;
*p = new_node;
if (*word == '\0') return;
insert_r(*p, word + 1);
}
}
else {
/* if the child is NULL, the sibling must be NULL too */
/* so we don't need to check that. */
struct TrieNode* new_node = trieCreate();
new_node->val = *word;
root->child = new_node;
if (*word == '\0') return;
insert_r(root->child, word + 1);
}
}
bool search_r(struct TrieNode* root, char* word) {
if (root == NULL || word == NULL) return false;
if (root->child == NULL) return false;
if (root->child->val == *word) {
if (*word == '\0') return true;
else return search_r(root->child, word + 1);
}
else {
struct TrieNode* p = root->child->sibling;
while (p) {
if (p->val == *word) {
if (*word == '\0') return true;
else return search_r(p, word + 1);
}
p = p->sibling;
}
return false;
}
}
bool startsWith_r(struct TrieNode* root, char* prefix) {
if (root == NULL || prefix == NULL) return false;
if (root->child == NULL) return false;
if (*prefix == '\0') return true;
if (root->child->val == *prefix) {
return startsWith_r(root->child, prefix + 1);
}
else {
struct TrieNode* p = root->child->sibling;
while (p) {
if (p->val == *prefix) {
return startsWith_r(p, prefix + 1);
}
p = p->sibling;
}
return false;
}
}
#else
struct TrieNode {
bool end;
struct TrieNode *next[26]; /* a-z */
};
struct TrieNode* trieCreate() {
struct TrieNode *dummy = (struct TrieNode *)malloc(sizeof(struct TrieNode));
dummy->end = false;
memset(dummy->next, 0, sizeof(dummy->next));
return dummy;
}
void insert(struct TrieNode* root, char* word) {
if (root == NULL || word == NULL) return;
struct TrieNode *p = root;
while (*word != '\0') {
if (p->next[*word - 'a'] == NULL) {
p->next[*word - 'a'] = trieCreate();
}
p = p->next[*word - 'a'];
word++;
}
p->end = true;
}
bool search(struct TrieNode* root, char* word) {
if (root == NULL || word == NULL) return false;
struct TrieNode *p = root;
while (*word != '\0') {
if (p->next[*word - 'a'] == NULL) {
return false;
}
else {
p = p->next[*word - 'a'];
}
word++;
}
if (p->end) return true;
else return false;
}
bool startsWith(struct TrieNode* root, char* prefix) {
if (root == NULL || prefix == NULL) return false;
struct TrieNode *p = root;
while (*prefix != '\0') {
if (p->next[*prefix - 'a'] == NULL) {
return false;
}
else {
p = p->next[*prefix - 'a'];
}
prefix++;
}
return true;
}
void trieFree(struct TrieNode* root) {
if (root == NULL) return;
int i;
for (i = 0; i < 26; i++) {
trieFree(root->next[i]);
}
free(root);
}
#endif
int main() {
/* recursive method switch */
#define RECURSIVE 0
#if LCRS && RECURSIVE
#define insert insert_r
#define search search_r
#define startsWith startsWith_r
#endif
struct TrieNode* node = trieCreate();
assert(search(node, "some") == false);
insert(node, "s");
assert(search(node, "some") == false);
assert(search(node, "s") == true);
insert(node, "some");
assert(search(node, "some") == true);
insert(node, "somestring");
insert(node, "someword");
insert(node, "word");
assert(search(node, "word") == true);
assert(search(node, "somestring") == true);
assert(search(node, "someword") == true);
assert(search(node, "somew") == false);
assert(search(node, "somes") == false);
assert(search(node, "somex") == false);
assert(startsWith(node, "some") == true);
assert(startsWith(node, "somew") == true);
assert(startsWith(node, "somes") == true);
assert(startsWith(node, "somex") == false);
trieFree(node);
printf("all tests passed!\n");
return 0;
}