-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.c
217 lines (155 loc) · 5.21 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
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
#include "trie.h"
TrieNode *getNode(void) {
TrieNode *pNode = NULL;
pNode = (TrieNode *)malloc(sizeof(TrieNode));
if (pNode) {
int i;
pNode->color = 0;
pNode->isEndOfWord = 0;
//adding extra character '_'
for (i = 0; i < ALPHABET_SIZE + 1; i++)
pNode->children[i] = NULL;
}
return pNode;
}
int char_to_index(char c) {
if(c == '_')
return ALPHABET_SIZE;
else return ((int)c - (int)'a');
}
void insert(TrieNode *root, const char *key, char color) {
int level;
int length = strlen(key);
int index;
TrieNode *pCrawl = root;
for (level = 0; level < length; level++) {
index = char_to_index(key[level]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isEndOfWord = 1;
pCrawl->color = color;
}
// Returns true if key presents in trie, else false
int search(TrieNode *root, const char *key, char *color) {
int level;
int length = strlen(key);
int index;
struct TrieNode *pCrawl = root;
for (level = 0; level < length; level++) {
if( (key[level] < 'a' || key[level] > 'z') && key[level] != '_')
return 0;
index = char_to_index(key[level]);
if (!pCrawl->children[index])
return 0;
pCrawl = pCrawl->children[index];
}
*color = pCrawl->color;
return (pCrawl != NULL && pCrawl->isEndOfWord);
}
TrieNode_c *codebaseNode(void) {
TrieNode_c *pNode = NULL;
pNode = (TrieNode_c *)malloc(sizeof(TrieNode_c));
if (pNode) {
int i;
pNode->isEndOfWord = 0;
pNode->lower = pNode->upper = 0;
//adding extra character '_'
for (i = 0; i < ALPHABET_SIZE + 1; i++)
pNode->children[i] = NULL;
}
return pNode;
}
void insert_in_codebase(TrieNode_c *root, const char *key, int lower, int upper) {
int level;
int length = strlen(key);
int index;
TrieNode_c *pCrawl = root;
for (level = 0; level < length; level++) {
index = char_to_index(key[level]);
if (!pCrawl->children[index])
pCrawl->children[index] = codebaseNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isEndOfWord = 1;
pCrawl->lower = lower;
pCrawl->upper = upper;
}
// Returns true if key presents in trie, else false
int search_in_codebase(TrieNode_c *root, const char *key, int *lower, int *upper) {
int level;
int length = strlen(key);
int index;
struct TrieNode_c *pCrawl = root;
for (level = 0; level < length; level++) {
if( (key[level] < 'a' || key[level] > 'z') && key[level] != '_')
return 0;
index = char_to_index(key[level]);
if (!pCrawl->children[index])
return 0;
pCrawl = pCrawl->children[index];
}
*lower = pCrawl->lower;
*upper = pCrawl->upper;
//printf("search\n%d\n%d\n", *lower, *upper);
return (pCrawl != NULL && pCrawl->isEndOfWord);
}
/*
int main() {
char grp1[][8] = {"int", "char", "double", "long", "auto",
"signed", "unsigned", "void", "float", "short"};
char grp2[][8] = {"register", "extern", "static", "volatile", "const"};
char grp3[][8] = {"typedef", "struct", "enum", "union"};
char grp4[][8] = {"continue", "break", "return", "sizeof"};
char grp5[][8] = {"for", "while", "do", "goto"};
char grp6[][8] = {"if", "else", "switch", "case", "default"};
TrieNode_c *root = codebaseNode();
// Construct trie
int i;
for (i = 0; i < ARRAY_SIZE(grp1); i++)
insert_in_codebase(root, grp1[i], "suraj yerkal");
for (i = 0; i < ARRAY_SIZE(grp2); i++)
insert(root, grp2[i], 1);
for (i = 0; i < ARRAY_SIZE(grp3); i++)
insert(root, grp3[i], 5);
for (i = 0; i < ARRAY_SIZE(grp4); i++)
insert(root, grp4[i], 25);
for (i = 0; i < ARRAY_SIZE(grp5); i++)
insert(root, grp5[i], 9);
for (i = 0; i < ARRAY_SIZE(grp6); i++)
insert(root, grp6[i], 7);
insert_in_codebase(root, "inta", "hello");
char *tp;
search_in_codebase(root, "inta", &tp);
for(int i = 0; tp[i] != '\0'; i++)
printf("%c", tp[i]);
char tmp;
printf("%d \n", search(root, "sizeof(", &tmp));
printf("%d\n", tmp);
return 0;
}*/
/*
// Driver
int main() {
// Input keys (use only 'a' through 'z' and lower case)
char keys[][8] = {"the", "a", "there", "answer", "any",
"by", "bye", "their"};
char output[][32] = {"Not present in trie", "Present in trie"};
TrieNode *root = getNode();
// Construct trie
int i;
for (i = 0; i < ARRAY_SIZE(keys); i++)
insert(root, keys[i], 0);
insert(root, "BLACK", 50);
char tmp;
// Search for different keys
printf("%s --- %s\n", "the", output[search(root, "th", &tmp)] );
printf("%d\n", tmp);
printf("%s --- %s\n", "these", output[search(root, "these")] );
printf("%s --- %s\n", "their", output[search(root, "their")] );
printf("%s --- %s\n", "thaw", output[search(root, "thaw")] );
return 0;
}*/