-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
example1.cpp
97 lines (73 loc) · 2.2 KB
/
example1.cpp
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
#include <string>
#include <iostream>
#include <cstdlib>
#include "../radix_tree.hpp"
radix_tree<std::string, int> tree;
void insert() {
tree["apache"] = 0;
tree["afford"] = 1;
tree["available"] = 2;
tree["affair"] = 3;
tree["avenger"] = 4;
tree["binary"] = 5;
tree["bind"] = 6;
tree["brother"] = 7;
tree["brace"] = 8;
tree["blind"] = 9;
tree["bro"] = 10;
}
void longest_match(std::string key)
{
radix_tree<std::string, int>::iterator it;
it = tree.longest_match(key);
std::cout << "longest_match(\"" << key << "\"):" << std::endl;
if (it != tree.end()) {
std::cout << " " << it->first << ", " << it->second << std::endl;
} else {
std::cout << " failed" << std::endl;
}
}
void prefix_match(std::string key)
{
std::vector<radix_tree<std::string, int>::iterator> vec;
std::vector<radix_tree<std::string, int>::iterator>::iterator it;
tree.prefix_match(key, vec);
std::cout << "prefix_match(\"" << key << "\"):" << std::endl;
for (it = vec.begin(); it != vec.end(); ++it) {
std::cout << " " << (*it)->first << ", " << (*it)->second << std::endl;
}
}
void greedy_match(std::string key)
{
std::vector<radix_tree<std::string, int>::iterator> vec;
std::vector<radix_tree<std::string, int>::iterator>::iterator it;
tree.greedy_match(key, vec);
std::cout << "greedy_match(\"" << key << "\"):" << std::endl;
for (it = vec.begin(); it != vec.end(); ++it) {
std::cout << " " << (*it)->first << ", " << (*it)->second << std::endl;
}
}
void traverse() {
radix_tree<std::string, int>::iterator it;
std::cout << "traverse:" << std::endl;
for (it = tree.begin(); it != tree.end(); ++it) {
std::cout << " " << it->first << ", " << it->second << std::endl;
}
}
int main()
{
insert();
longest_match("binder");
longest_match("bracelet");
longest_match("apple");
prefix_match("aff");
prefix_match("bi");
prefix_match("a");
greedy_match("avoid");
greedy_match("bring");
greedy_match("attack");
traverse();
tree.erase("bro");
prefix_match("bro");
return EXIT_SUCCESS;
}