-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie.h
218 lines (131 loc) · 3.76 KB
/
trie.h
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <map>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <cstdio>
#include <ostream>
template <class T>
class _TRIE_NODE_{
private:
T value;
std::map<T,_TRIE_NODE_> nodes;
public:
/*
TYPENAMES FOR EASY SEAHC
*/
void set_map(std::map<T,_TRIE_NODE_> neu){
this->nodes=neu;
}
typedef typename std::map<T,_TRIE_NODE_ >::iterator NODES_ITERATOR;
void set_value(T valuye){
this->value=valuye;
}
T get_value(){return this->value;}
unsigned int is_leaf(){
return this->nodes.size()==0;
}
NODES_ITERATOR contains(T value){
return this->nodes.find(value);
}
_TRIE_NODE_::NODES_ITERATOR end(){
return this->nodes.end();
};
_TRIE_NODE_::NODES_ITERATOR begin(){
return this->nodes.begin();
};
void add(T value){
_TRIE_NODE_<T> neu;
neu.set_value(value);
std::map<T,_TRIE_NODE_> nodes;
neu.set_map(nodes);
this->nodes.insert(std::pair<T,_TRIE_NODE_ >(value,neu));
}
};
template < class T >
class trie{
private:
_TRIE_NODE_<T> root;
void insert(_TRIE_NODE_<T>*,T *,int pos);
bool contains(_TRIE_NODE_<T>,T *,int pos);
std::ostream& print(_TRIE_NODE_<T>,std::ostream &);
public:
_TRIE_NODE_<T> get_root();
void insert(T *node);
bool contains(T *look);
std::ostream& print(std::ostream &out);
trie(){
std::map<T,_TRIE_NODE_<T> > nodes;
root.set_map(nodes);
}
};
template <class T>
bool trie<T>::contains(_TRIE_NODE_<T> current, T *value_array, int pos) {
int size= strlen(value_array);
if(pos<size) {
if (current.is_leaf()) {
if(pos==(size)){
return true;
}else{
return false;
}
} else {
T aux = value_array[pos];
typename std::map<T, _TRIE_NODE_<T> >::iterator where = current.contains(aux);
if (where == current.end()) {
//we reach a node that does not contain the letters
return false;
} else {
int nespos=pos+1;
return contains((*where).second,value_array,nespos);
}
}
}else if(pos==size) {
return true;
}else{
return false;
};
};
template <class T>
bool trie<T>::contains(T *value_array) {
return this->contains(this->root, value_array, 0);
}
template<class T>
_TRIE_NODE_<T> trie<T>::get_root() {
return this->root;
};
template <class T>
void trie<T>::insert(_TRIE_NODE_<T> *current, T *value_array, int pos) {
int size= sizeof(value_array)/ sizeof(T);
if(pos<strlen(value_array)){
T aux=value_array[pos];
typename std::map<T,_TRIE_NODE_<T> >::iterator where=current->contains(aux);
if(where==current->end()){
//the first leter was not found in the root, lets insert it and keep going
current->add(aux);
where=current->contains(aux);
}
int nespos=pos+1;
this->insert(&(*where).second, value_array, nespos);
}
};
template <class T>
std::ostream& trie<T>::print(_TRIE_NODE_<T> current,std::ostream & out) {
if(!current.is_leaf()){
//only traverse the trie if its not a leaf
out<<current.get_value()<<" ";
typename std::map<T,_TRIE_NODE_<T> >::iterator iter=current.begin();
for(;iter!=current.end();iter++){
print((*iter).second, out);
}
}
return out;
}
template <class T>
std::ostream& trie<T>::print(std::ostream &out) {
this->print(this->root,out);
return out;
}
template <class T>
void trie<T>::insert(T *value_array) {
this->insert(&this->root, value_array,0);
};