forked from kish-an/cs50
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.c
169 lines (143 loc) · 3.47 KB
/
dictionary.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
/* Trie Implementation */
// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
#define ALPHABET_SIZE 27
// Represents a node in a trie
typedef struct trieNode
{
struct trieNode *next[ALPHABET_SIZE];
bool endOfWord;
}
trieNode;
// Gets a trie node
trieNode *getNode(void)
{
// Create a null pointer
trieNode *pNode = NULL;
// Set to size of a trie node
pNode = calloc(1, sizeof(trieNode));
if (pNode == NULL)
{
printf("Could not allocate memory for trie node\n.");
pNode = NULL;
return pNode;
}
else
{
pNode->endOfWord = false;
// Set every pointer in node array to NULL
for (int i = 0; i < ALPHABET_SIZE; i++)
{
pNode->next[i] = NULL;
}
}
return pNode;
}
int words = 0;
// Inserts key into trie
void insertNode(trieNode *rootNode, char *key)
{
int length = strlen(key);
int index;
// Create temp traversal pointer
trieNode *trav = rootNode;
for (int i = 0; i < length; i++)
{
// Get index from character (a = 0, b = 1...)
if (key[i] == '\'')
{
index = ALPHABET_SIZE - 1;
}
else
{
// All dictionary words are lower case by default
index = key[i] - 97;
}
// Point to another node for next character (if it doesn't already exist)
if (trav->next[index] == NULL)
{
trav->next[index] = getNode();
}
// Update traversal pointer
trav = trav->next[index];
}
// Mark last node (leaf) after loop as end of the word
trav->endOfWord = true;
}
// Set up root node of trie (can not set root to getNode() in global scope);
struct trieNode *root;
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
root = getNode();
// Open dictionary and all all words to trie
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
return false;
}
char word[LENGTH + 1];
while (fscanf(dict, "%s", word) != EOF)
{
insertNode(root, word);
words++;
}
fclose(dict);
return true;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int length = strlen(word);
int index;
// Create temp traversal pointer
trieNode *trav = root;
for (int i = 0; i < length; i++)
{
// Get index from character (a = 0, b = 1...)
if (word[i] == '\'')
{
index = ALPHABET_SIZE - 1;
}
else
{
index = tolower(word[i]) - 97;
}
// Check if next node is null (a.k.a word doesn't exist)
if (trav->next[index] == NULL)
{
return false;
}
// Update traversal pointer
trav = trav->next[index];
}
return (trav != NULL && trav->endOfWord);
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return words;
}
// Recursive function to destroy trie
bool unloader(trieNode *node)
{
for (int i = 0; i < ALPHABET_SIZE; i++)
{
if (node->next[i] != NULL)
{
unloader(node->next[i]);
}
}
free(node);
return true;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
return unloader(root);
}