-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_examples.c
More file actions
76 lines (60 loc) · 2.15 KB
/
trie_examples.c
File metadata and controls
76 lines (60 loc) · 2.15 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
#include <stdio.h>
#include <string.h>
#include "tries.h"
int main(void) {
tries_init();
// create an EmptyTrie
HsStablePtr empty_trie = get_empty_trie();
printf("get_empty_trie():\n");
print_trie(empty_trie);
// create a more interesting trie
double weights[5] = {30.0, 12.0, 25.0, 20.0, 14.0};
char* words[5] = {"an", "to", "ted", "a", "tea"};
HsStablePtr mytrie = trie_from_arrays(weights, words, 5);
printf("mytrie:\n");
print_trie(mytrie);
// get the next node when searching for "ted"
printf("get_next_node(\"ted\", mytrie):\n");
print_trie(get_next_node("ted", mytrie));
// get the node for "te"
printf("get_node(\"te\", mytrie):\n");
print_trie(get_node("te", mytrie));
// is "ted" in the trie?
printf("is_in_trie(\"ted\", mytrie): %d\n", is_in_trie("ted", mytrie));
// is "tedtalk" in the trie?
printf("is_in_trie(\"tedtalk\", mytrie): %d\n",
is_in_trie("tedtalk", mytrie));
// what's the maximum weight in this trie?
printf("get_max_weight(mytrie): %f\n", get_max_weight(mytrie));
// insert some nodes
double new_weights[2] = {5, 15};
char* new_words[2] = {"in", "inn"};
HsStablePtr mytrie_augmented = insert_words(new_weights, new_words,
2, mytrie);
printf("\nmytrie_augmented:\n");
print_trie(mytrie_augmented);
// remove a prefix
mytrie_augmented = remove_prefix("in", mytrie_augmented);
printf("\nmytrie_augmented after deleting \"in\"\n");
print_trie(mytrie_augmented);
// read in a trie
HsStablePtr mytrie4 = read_terms("Data/mytrie4.txt");
printf("mytrie4 using read_terms:\n");
print_trie(mytrie4);
// use autocomplete
double weights_out[3];
char* words_out[3];
autocomplete(weights_out, words_out, "t", mytrie4, 3);
printf("autocomplete results:\n");
for (int i = 0; i < 3; i++){
printf("(%f, %s)\n", weights_out[i], words_out[i]);
}
// Request the same autocomplete results but use slowcomplete
slowcomplete(weights_out, words_out, 5, "t", weights, words, 3);
printf("\nslowcomplete results:\n");
for (int i = 0; i < 3; i++){
printf("(%f, %s)\n", weights_out[i], words_out[i]);
}
tries_end();
return 0;
}