-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrie.cc
191 lines (160 loc) · 3.24 KB
/
trie.cc
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
#include "trie.h"
#include<iostream>
#include<cstdlib>
subnode& subnode::operator = ( subnode& src ){
value = src.value;
ptr = src.ptr;
}
node::node(){
m_bdata = false;
m_child = NULL;
m_data = NULL;
m_cnt = 0;
m_size = 0;
init_child();
}
node::~node(){
if( m_bdata && (m_data != NULL) ){
delete m_data;
}
if( m_child )
delete [] m_child;
}
node* node::query( unsigned int key ){
int high = m_cnt -1, low=0, mid=0;
while( low <= high ){
mid = ( high + low )/2;
if( (m_child[mid]).value == key ){
return (m_child[mid]).ptr;
}
if ( key > (m_child[mid]).value ) {
low = mid + 1;
} else {
high = mid -1;
}
}
return NULL;
}
void node::init_child(){
m_child = new subnode[4];
if( ! m_child ){
std::cerr << "node::init_child : new failed"
<< std::endl;
exit(1);
}
m_size = 4;
}
void node::enlarge(){
subnode* tmp = new subnode[m_size*2];
if( !tmp ){
std::cerr << "node::enlarge new failed"
<< std::endl;
}
for( int i=0; i < m_cnt; ++i ){
tmp[i] = m_child[i];
}
delete [] m_child;
m_child = tmp;
m_size = m_size*2;
}
node* node::add_child( unsigned int key ){
node* target = query( key );
if( target )
return target;
if( m_cnt+1 > m_size )
enlarge();
int j=m_cnt-1;
while( (j >= 0) && ((m_child[j]).value > key) )
--j;
++j;
int i=m_cnt-1;
while( i >= j ){
m_child[i+1] = m_child[i];
--i;
}
(m_child[j]).value = key;
(m_child[j]).ptr = new node;
if( !(m_child[j]).ptr ){
std::cerr << "node::add_child new failed"
<< std::endl;
}
m_cnt = m_cnt + 1;
return (m_child[j]).ptr;
}
void node::add_data(const std::string& str ){
if( ! m_bdata ){
m_data = new dnode;
#ifdef DEBUG2
std::cout << "node::add_data Create New dnode\n";
#endif
if( ! m_data ){
std::cerr << "node::add_data new failed"
<<"\n";
}
m_bdata = true;
}
#ifdef DEBUG2
std::cout << "node::add_data\t" << str << "\n";
#endif
m_data->add( str );
}
bool node::data(){
return m_bdata;
}
void node::copy_data( dnode& d ){
d.m_d = m_data->m_d;
}
void trie::add(const std::vector<unsigned int>& key,const std::string& value ){
node* ptr = &m_root;
int i;
#ifdef DEBUG
std::cout << "trie::add " << value << "\n";
#endif
for( i=0; i<key.size() && ptr; ++i ){
ptr = ptr->add_child( key.at(i) );
}
if( i < key.size() || !ptr ){
std::cerr << "trie::add add failed\n";
exit(1);
}
ptr->add_data( value );
}
bool trie::query( const std::vector<unsigned int>& key, dnode& data){
node* ptr = &m_root;
int i;
for(i=0; i<key.size() && ptr; ++i){
ptr = ptr->query( key[i] );
}
if( i < key.size() || !ptr ){
#ifdef DEBUG2
std::cout << "trie::query No such words\n";
#endif
return false;
}
if( ! ptr->data() ){
#ifdef DEBUG2
std::cout << "trie::query No Data\n";
#endif
return false;
}
ptr->copy_data( data );
return true;
}
trie::trie(){
}
trie::~trie(){
}
bool trie::query( const std::vector<unsigned int>& key ){
node* ptr = &m_root;
int i;
for(i=0; i<key.size() && ptr; ++i){
ptr = ptr->query( key[i] );
}
if( i < key.size() || !ptr ){
return false;
}
if( ! ptr->data() ){
return false;
}
return true;
}