-
Notifications
You must be signed in to change notification settings - Fork 2
/
Trie.c
175 lines (161 loc) · 4.13 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
// CSE 374
// HW 5
// Jisoo Jung
//
// Trie.c:
// Trie here is a variation of tree where each node contains
// 8 children. Trie stores data with common prefixes in
// memory efficient manner. This trie is specifically designed
// for T9 predictive text, where the user can input number
// sequence to extract possible word in a dictionary.
#include "Util.h"
#include "Trie.h"
// Returns dictionary word that corresponds to the given key sequence.
Node * search(char * keySeq, Node * root) {
Node * current = root;
int len = strlen(keySeq) - 1;
int i = 0;
// Cases:
// 1. key length shorter than trie height
// 2. key length longer than trie heigh
// 3. # symbol termination
while (i < len && current && keySeq[i] != '#') {
int childIndex = keySeq[i] - '0' - 2;
if (childIndex < 0 || !isdigit(keySeq[i])) {
return NULL;
}
current = current->children[childIndex];
i++;
}
return current;
}
// Inserts the given word to trie specified by the root.
// Does not modify existing common prefix path.
// e.g. Inserting 'ab' and 'abc' add 3 nodes in total because
// 'ab' is common prefix.
void insert(Node * root, char * word) {
Node * current = root;
int wordIndex = 0;
// insert a word into the trie
while (*(word + wordIndex)) {
char letter = word[wordIndex];
int childIndex = getChildIndex(letter);
Node * child = current->children[childIndex];
// make new node if there's no child at this index
if (!child) {
child = makeNode();
current->children[childIndex] = child;
}
current = child;
wordIndex++;
}
// create a linked list when word(s) with the
// same sequence already exists
// -> append a node
if (current->word) {
Node * temp = current;
while (temp && temp->next) {
temp = temp->next;
}
Node * newNode = makeNode();
temp->next = newNode;
current = newNode;
}
int len = wordIndex + 1;
// Note: sizeof(char) is 1 in all machine
current->word = (char *) mallocVerif(len);
strncpy(current->word, word, len);
}
// Builds trie with words in the specified file.
// Prints error message if it's not a valid file.
Node * build(FILE * fp) {
char * word;
Node * root;
word = (char *) mallocVerif(MAX_WORD_LENGTH);
root = makeNode();
while (fgets(word, MAX_WORD_LENGTH, fp)) {
int len = strlen(word);
// replace newline character to null terminator
if (word[len - 1] == '\n') {
word[len - 1] = '\0';
}
insert(root, word);
}
free(word);
return root;
}
// Returns 0-7 child index that corresponds to the given letter
int getChildIndex(char letter) {
int chMap[NUM_ALPHABET] = {
2, 2, 2,
3, 3, 3,
4, 4, 4,
5, 5, 5,
6, 6, 6,
7, 7, 7, 7,
8, 8, 8,
9, 9, 9, 9
};
int i = letter - 'a';
// subtract 2 because 0 and 1 are reserved for
// space and set of special characters
return chMap[i] - 2;
}
// Clears/frees the given node and its children.
void clear(Node * root) {
if (root) {
int i;
for (i = 0; i < NUM_CHILDREN; i++) {
clear(root->children[i]);
}
freeList(root->next);
freeNode(root);
}
}
// Frees the linked list of node with the given front
void freeList(Node * front) {
while (front) {
Node * next = front->next;
freeNode(front);
front = next;
}
}
// Frees a node referenced by the given node pointer
void freeNode(Node * node) {
// free(NULL) is safe
free(node->word);
free(node->children);
free(node);
}
// Prints the given trie in preorder
// For debugging
void printTrie(Node * root, int level) {
if (root) {
if (root->word != NULL) {
printTabs(level);
Node * temp = root;
printf("word = ");
while (temp) {
printf("%s -> ", temp->word);
temp = temp->next;
}
printf("NULL\n");
}
int i;
for (i = 0; i < NUM_CHILDREN; i++) {
Node * child = root->children[i];
if (child != NULL) {
printTabs(level);
printf("key=%d, index=%d, level=%d : \n", i + 2, i, level);
printTrie(child, level + 1);
}
}
}
}
// prints numTabs many tabs
void printTabs(int numTabs) {
int i;
for (i = 0; i < numTabs; i++) {
printf("\t");
}
}