-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.c
More file actions
163 lines (138 loc) · 5.01 KB
/
trie.c
File metadata and controls
163 lines (138 loc) · 5.01 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "trie.h"
/* Allocates a new Trie_t data structure and returns a pointer to it. */
Trie_t * newTrie(TrieValue_t value, bool endOfString) {
Trie_t * tree = (Trie_t *) malloc(sizeof (Trie_t));
tree->value = value;
tree->endOfString = endOfString;
tree->numChildren = 0;
tree->childrenCapacity = INITIAL_TRIE_CHILDREN;
tree->children = (Trie_t **) malloc(INITIAL_TRIE_CHILDREN * sizeof (Trie_t *));
for (size_t i = 0; i < INITIAL_TRIE_CHILDREN; i++) {
tree->children[i] = NULL;
}
return tree;
}
/* Deallocates a Trie_t data structure, and recursively deallocates all of
it's children as well. */
void destroyTrie(Trie_t * tree) {
if (tree == NULL) { return; }
//recursively destroy children
for (size_t i = 0; i < tree->numChildren; i++) {
destroyTrie(tree->children[i]);
}
free(tree->children);
free(tree);
}
/* Search a Trie_t for a particular value specified by val. Returns a pointer
to the child if it is found, otherwise NULL. */
static Trie_t * getChildOfTrie(Trie_t * tree, TrieValue_t val) {
for (size_t i = 0; i < tree->numChildren; i++) {
if (tree->children[i]->value == val) {
return tree->children[i];
}
}
return NULL;
}
/* Adds a new Trie_t child to an existing trie if the child did not
exist. Otherwise returns a pointer to the existing child with matching
value. */
static Trie_t * addChildToTrie(Trie_t * tree, TrieValue_t val, bool endOfString) {
Trie_t * existingChild = NULL;
existingChild = getChildOfTrie(tree, val);
if (existingChild != NULL) {
if (endOfString) {
existingChild->endOfString = true; //update this flag if the "new" child has it
}
return existingChild;
}
if (tree->numChildren >= tree->childrenCapacity - 1) {
Trie_t ** newChildren = (Trie_t **) malloc(sizeof (Trie_t *) * (tree->childrenCapacity * 2));
memcpy(newChildren, tree->children, sizeof (Trie_t *) * tree->numChildren);
free(tree->children);
tree->children = newChildren;
if (newChildren == NULL) {
puts("Memory allocation failed!");
exit(EXIT_FAILURE);
}
tree->childrenCapacity *= 2;
}
tree->children[tree->numChildren] = newTrie(val, endOfString);
tree->numChildren++;
return tree->children[tree->numChildren - 1];
}
/* Should always insert to the tree who's root node is NULL/0/empty
string value. The root node is essentially ignored when looking up
strings in the trie.
Note: string must be null-terminated or this function will have
undefined behavior. */
bool insertStringToTrie(Trie_t * tree, TrieValue_t * string) {
Trie_t * currentNode = tree;
for (size_t i = 0; string[i] != '\0'; i++) {
currentNode = addChildToTrie(currentNode, string[i], false);
}
currentNode->endOfString = true;
return true;
}
/* Returns true if the null-terminated string of type TrieValue_t is contained
in the Trie_t referenced by tree. Returns false otherwise.
This is the bread-and-butter of it's application in spell checking/dictionary
lookup. */
bool stringExistsInTrie(Trie_t * tree, TrieValue_t * string) {
Trie_t * currentNode = tree;
for (size_t i = 0; string[i] != '\0'; i++) {
currentNode = getChildOfTrie(currentNode, string[i]);
if (currentNode == NULL) {
return false;
}
}
if (currentNode->endOfString == false) {
return false;
}
return true;
}
/* Creates a new Trie_t from a dictionary. The provided dictionary should be a
text file full of words delimited by newline characters */
Trie_t * newTrieFromDictionary(char * dictionaryFileName) {
Trie_t * tree = newTrie(0, false);
char * string = NULL;
size_t len = 0;
ssize_t bytesRead = 0;
FILE * fp = NULL;
fp = fopen(dictionaryFileName, "r");
if (fp == NULL) {
destroyTrie(tree);
return NULL;
}
do {
bytesRead = getline(&string, &len, fp);
if (bytesRead > 0) {
//need to replace newlines, carriage returns, etc.
for (unsigned int i = 0; string[i] != '\0'; i++) {
if (string[i] == '\n' || string[i] == '\r') {
string[i] = '\0';
}
}
insertStringToTrie(tree, string);
}
} while (bytesRead != -1);
fclose(fp);
return tree;
}
/* Test cases for the Trie_t association functions. */
void testTrie() {
Trie_t * tree = newTrie(0, false);
assert(stringExistsInTrie(tree, "test") == false);
insertStringToTrie(tree, "test");
assert(stringExistsInTrie(tree, "test") == true);
assert(stringExistsInTrie(tree, "tes") == false);
assert(getChildOfTrie(tree, 'd') == NULL);
destroyTrie(tree);
Trie_t * dictionary = newTrieFromDictionary("words");
assert(stringExistsInTrie(dictionary, "hello") == true);
assert(stringExistsInTrie(dictionary, "guise") == true);
destroyTrie(dictionary);
}