Skip to content

Commit 76bb9cb

Browse files
Implement trie
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent bede715 commit 76bb9cb

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

208_implement_trie/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test trie.c

208_implement_trie/trie.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
typedef struct Trie {
7+
struct Trie *subs[27];
8+
char letters[27];
9+
} Trie;
10+
11+
/** Initialize your data structure here. */
12+
static Trie* trieCreate()
13+
{
14+
Trie *obj = malloc(sizeof(*obj));
15+
memset(obj->letters, 0xff, sizeof(obj->letters));
16+
memset(&obj->subs[0], 0, sizeof(obj->subs));
17+
return obj;
18+
}
19+
20+
/** Inserts a word into the trie. */
21+
static void trieInsert(Trie* obj, char* word)
22+
{
23+
while (*word != '\0') {
24+
int pos = *word - 'a' + 1;
25+
obj->letters[pos] = *word++;
26+
if (obj->subs[pos] == NULL) {
27+
obj->subs[pos] = trieCreate();
28+
}
29+
obj = obj->subs[pos];
30+
}
31+
obj->letters[0] = '\0';
32+
}
33+
34+
/** Returns if the word is in the trie. */
35+
static bool trieSearch(Trie* obj, char* word)
36+
{
37+
while (obj != NULL) {
38+
int pos = *word == '\0' ? 0 : *word - 'a' + 1;
39+
if (obj->letters[pos] != *word) {
40+
return false;
41+
}
42+
word++;
43+
obj = obj->subs[pos];
44+
}
45+
return true;
46+
}
47+
48+
/** Returns if there is any word in the trie that starts with the given prefix. */
49+
static bool trieStartsWith(Trie* obj, char* prefix)
50+
{
51+
while (*prefix != '\0') {
52+
int pos = *prefix - 'a' + 1;
53+
if (pos < 0 || obj->letters[pos] != *prefix) {
54+
return false;
55+
}
56+
if (*++prefix != '\0') {
57+
if (obj->subs[pos] == NULL) {
58+
return false;
59+
}
60+
obj = obj->subs[pos];
61+
}
62+
}
63+
return true;
64+
}
65+
66+
static void trieFree(Trie* obj)
67+
{
68+
int i;
69+
for (i = 0; i < sizeof(obj->letters); i++) {
70+
if (obj->subs[i] != NULL) {
71+
trieFree(obj->subs[i]);
72+
}
73+
}
74+
free(obj);
75+
}
76+
77+
int main(void)
78+
{
79+
char *word1 = "abc";
80+
char *word2 = "ab";
81+
82+
Trie* obj = trieCreate();
83+
trieInsert(obj, word1);
84+
printf("%s\n", trieSearch(obj, word1) ? "true" : "false");
85+
printf("%s\n", trieSearch(obj, word2) ? "true" : "false");
86+
trieInsert(obj, word2);
87+
printf("%s\n", trieSearch(obj, word2) ? "true" : "false");
88+
trieInsert(obj, word2);
89+
printf("%s\n", trieStartsWith(obj, word2) ? "true" : "false");
90+
trieFree(obj);
91+
return 0;
92+
}

211_add_and_search_word/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test word_dict.c

211_add_and_search_word/word_dict.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
typedef struct WordDictionary {
7+
struct WordDictionary *subs[27];
8+
char letters[27];
9+
} WordDictionary;
10+
11+
/** Initialize your data structure here. */
12+
static WordDictionary* wordDictionaryCreate()
13+
{
14+
WordDictionary *obj = malloc(sizeof(*obj));
15+
memset(obj->letters, 0xff, sizeof(obj->letters));
16+
memset(&obj->subs[0], 0, sizeof(obj->subs));
17+
return obj;
18+
}
19+
20+
/** Inserts a word into the wordDictionary. */
21+
static void wordDictionaryInsert(WordDictionary* obj, char* word)
22+
{
23+
while (*word != '\0') {
24+
int pos = *word - 'a' + 1;
25+
obj->letters[pos] = *word++;
26+
if (obj->subs[pos] == NULL) {
27+
obj->subs[pos] = wordDictionaryCreate();
28+
}
29+
obj = obj->subs[pos];
30+
}
31+
obj->letters[0] = '\0';
32+
}
33+
34+
/** Returns if the word is in the wordDictionary. */
35+
static bool wordDictionarySearch(WordDictionary* obj, char* word)
36+
{
37+
int i;
38+
while (obj != NULL) {
39+
if (*word == '.') {
40+
bool found;
41+
for (i = 1; i < sizeof(obj->letters); i++) {
42+
if (obj->subs[i] != NULL) {
43+
found = wordDictionarySearch(obj->subs[i], word + 1);
44+
if (found) {
45+
return true;
46+
}
47+
}
48+
}
49+
return false;
50+
} else {
51+
int pos = *word == '\0' ? 0 : *word - 'a' + 1;
52+
if (obj->letters[pos] != *word) {
53+
return false;
54+
}
55+
word++;
56+
obj = obj->subs[pos];
57+
}
58+
}
59+
return true;
60+
}
61+
62+
static void wordDictionaryFree(WordDictionary* obj)
63+
{
64+
int i;
65+
for (i = 0; i < sizeof(obj->letters); i++) {
66+
if (obj->subs[i] != NULL) {
67+
wordDictionaryFree(obj->subs[i]);
68+
}
69+
}
70+
free(obj);
71+
}
72+
73+
int main(void)
74+
{
75+
char *word1 = "abc";
76+
char *word2 = "ab";
77+
char *word3 = "...";
78+
79+
WordDictionary* obj = wordDictionaryCreate();
80+
wordDictionaryInsert(obj, word1);
81+
printf("%s\n", wordDictionarySearch(obj, word1) ? "true" : "false");
82+
printf("%s\n", wordDictionarySearch(obj, word2) ? "true" : "false");
83+
wordDictionaryInsert(obj, word2);
84+
printf("%s\n", wordDictionarySearch(obj, word2) ? "true" : "false");
85+
wordDictionaryInsert(obj, word2);
86+
printf("%s\n", wordDictionarySearch(obj, word3) ? "true" : "false");
87+
wordDictionaryFree(obj);
88+
return 0;
89+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy