-
Notifications
You must be signed in to change notification settings - Fork 5
/
trie.c
185 lines (152 loc) · 4.36 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
#include <stdio.h>
#include <stdlib.h>
#include "trie.h"
// creates a new trie.
struct trie* trie_create()
{
return malloc(sizeof(struct trie));
}
// returns pointer to thing at key
void* trie_traverse(struct trie* data, char* key)
{
struct trie* current_node = data;
// increment the pointer position of key until null terminator is reached
while (*key != 0)
{
// try to find the correct child node
struct trie* next_node = current_node->children[*key];
// if next_node doesn't actually exist, return NULL
if (!next_node)
{
return NULL;
}
// set current node to the correct child node
current_node = next_node;
// increment key pointer position
key++;
}
return current_node;
}
// inserts val at key
void* trie_insert(struct trie* data, char* key, void* val)
{
struct trie* current_node = data;
// increment the pointer position of key until null terminator is reached
while (*key != 0)
{
// try to find the correct child node
struct trie* next_node = current_node->children[*key];
switch (current_node->type)
{
case trienode_twig:
// if this twig goes in the same direction
if (next_node)
{
break;
}
// else it will promote to a branch
case trienode_leaf:
current_node->type++;
}
// if next_node doesn't actually exist, create it
if (!next_node)
{
current_node->children[*key] = trie_create();
next_node = current_node->children[*key];
if (trienode_twig == current_node->type)
{
// children[0] is never used; we'll hijack it as a fast-forward key
*current_node->children = next_node;
}
}
// set current node to the correct child node
current_node = next_node;
// increment key pointer position
key++;
}
// set val at current_node
current_node->val = val;
return current_node;
}
// returns void pointer of thing at key
void* trie_lookup(struct trie* data, char* key)
{
struct trie* current_node = trie_traverse(data, key);
if (!current_node)
{
return NULL;
}
return current_node->val;
}
// If there is only one key that maches the given `char* prefix`, returns its value. If there are several, returns the given `void* ambiguous`, if there are none, returns null.
void* trie_lookup_prefix(struct trie* data, char* prefix, void* ambiguous)
{
struct trie* current_node = trie_traverse(data, prefix);
if (!current_node)
{
return NULL;
}
// exact match
if(NULL!=current_node->val)
{
return current_node->val;
}
// prefix matched, but we still have to find the key itself
while(NULL==current_node->val)
{
switch(current_node->type)
{
case trienode_leaf:
// we can do nothing but return null
return current_node->val;
case trienode_twig:
// fast-forward to the next (only) child
current_node = *current_node->children;
continue;
case trienode_branch:
return ambiguous;
}
}
// we found a key with the requested prefix, but there are more
if(trienode_twig==current_node->type)
{
return ambiguous;
}
return current_node->val;
}
// removes thing at key
void trie_remove(struct trie* data, char* key)
{
struct trie* current_node = data;
// increment the pointer position of key until null terminator is reached
while (*key != 0)
{
// try to find the correct child node
struct trie* next_node = current_node->children[*key];
// if next_node doesn't actually exist, return NULL
if (!next_node)
{
return NULL;
}
// set current node to the correct child node
current_node = next_node;
// increment key pointer position
key++;
}
free(current_node->val);
return;
}
// destroys trie at pointer
void trie_destroy(struct trie* data)
{
int a;
for (a = 1; a < 256; a++)
{
if (data->children[a])
{
trie_destroy(data->children[a]);
}
}
free(data);
return;
}