-
Notifications
You must be signed in to change notification settings - Fork 17
/
trie.c
93 lines (80 loc) · 2 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
#include <stdlib.h>
#include <stdio.h>
#include <Block.h>
struct _Trie {
char state;
char * value;
struct _Trie * sibling;
struct _Trie * children;
};
typedef struct _Trie Trie;
Trie * Trie_new() {
Trie * trie = (Trie *) malloc(sizeof(Trie));
trie->state = '\0';
trie->value = NULL;
trie->sibling = NULL;
trie->children = NULL;
return trie;
}
void Trie_free(Trie * trie) {
if(trie->sibling)
Trie_free(trie->sibling);
if(trie->children)
Trie_free(trie->children);
free(trie);
}
Trie * Trie_node(char state) {
Trie * trie = Trie_new();
trie->state = state;
return trie;
}
void Trie_add(Trie * trie, char * data, int length, char * value) {
// look for the child
Trie * child = trie->children;
while(child) {
if(child->state == *data)
break;
child = child->sibling;
}
// if the child doesn't exist add it
if(!child) {
child = Trie_node(*data);
child->sibling = trie->children;
trie->children = child;
}
if(length == 1) {
child->value = value;
} else {
// oh, tail recursion... how I wish you existed in C.
Trie_add(child, data + 1, length - 1, value);
}
}
char * Trie_get(Trie * trie, char * data, int length) {
Trie * child = trie;
// look for the child
while(length > 0) {
child = child->children;
while(child) {
if(child->state == *data)
break;
child = child->sibling;
}
if(!child)
return NULL;
length--;
data++;
}
return child->value;
}
/*
int main() {
Trie * trie = Trie_new();
Trie_add(trie, "foo", 3, "foo");
Trie_add(trie, "bar", 3, "bar");
Trie_add(trie, "food", 4, "food");
printf("foo: %s\n", Trie_get(trie, "foo", 3));
printf("food: %s\n", Trie_get(trie, "food", 4));
printf("bar: %s\n", Trie_get(trie, "bar", 3));
printf("argh: %s\n", Trie_get(trie, "argh", 4));
}
*/