-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrieTree.cpp
359 lines (329 loc) · 8.2 KB
/
TrieTree.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <vector>
#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include "TrieTree.h"
const int ALPHABET_SIZE = 41; // 增加的特殊字符个数
// 插入一个单词到Trie树
void TrieTree::insert(const std::string& word)
{
TrieNode* current = root;
for (char c : word)
{
int index;
if (std::islower(c))
{
index = c - 'a';
}
else if (std::isdigit(c))
{
index = c - '0' + 26;
}
else
{
// Handle other special characters as needed
if (c == ' ')
{
index = 36; // 空格的索引为36
}
else if (c == '.')
{
index = 37; // . 的索引为37
}
else if (c == ',')
{
index = 38; // " 的索引为38
}
else if (c == '\'')
{
index = 39; // ' 的索引为39
}
else if (c == '-')
{
index = 40; // - 的索引为40
}
else
{
// Skip unsupported characters
continue;
}
}
if (current->children[index] == nullptr)
{
current->children[index] = new TrieNode();
}
current = current->children[index];
}
current->isEndOfWord = true;
}
int TrieTree::build()
{
std::ifstream wordFile(filename);
if (wordFile.is_open())
{
std::string line;
std::string word;
std::istringstream iss;
int lineNumber = 1;
while (std::getline(wordFile, line))
{
iss.str(line);
std::getline(iss, word, ',');
word = convertToLowerCase(word);
insert(word);
++lineNumber;
}
wordFile.close();
return lineNumber;
}
else
{
return 0;
}
}
// 搜索模糊匹配的单词
std::vector<std::string> TrieTree::search(std::string word)
{
std::vector<std::string> result;
TrieNode* current = root;
for (char& c : word)
{
if (std::isalpha(c))
{
c = std::tolower(c);
}
}
for (char c : word)
{
int index;
if (std::islower(c))
{
index = c - 'a';
}
else if (std::isdigit(c))
{
index = c - '0' + 26;
}
else
{
// Handle other special characters as needed
if (c == ' ')
{
index = 36; // 空格的索引为36
}
else if (c == '.')
{
index = 37; // . 的索引为37
}
else if (c == ',')
{
index = 38; // " 的索引为38
}
else if (c == '\'')
{
index = 39; // ' 的索引为39
}
else if (c == '-')
{
index = 40; // - 的索引为40
}
else
{
// Skip unsupported characters
continue;
}
}
if (current->children[index] == nullptr)
{
std::vector<std::string> null;
return null; // 没有匹配的前缀
}
current = current->children[index];
}
// 从匹配的前缀开始进行深度优先搜索
int count = 0;
searchHelper(current, word, result, count);
return result;
}
// 辅助函数:从给定节点开始进行深度优先搜索
void TrieTree::searchHelper(TrieNode* node, std::string& prefix, std::vector<std::string>& result, int& count)
{
if (count >= 30) // 达到30个结果,停止递归
{
return;
}
if (node->isEndOfWord)
{
result.push_back(prefix);
count++; // 结果数量加一
}
for (int i = 0; i < ALPHABET_SIZE; i++)
{
if (node->children[i] != nullptr)
{
if (i < 26)
{
prefix.push_back('a' + i);
}
else if (i < 36)
{
prefix.push_back('0' + i - 26);
}
else if (i == 36)
{
prefix.push_back(' '); // 空格字符
}
else if (i == 37)
{
prefix.push_back('.'); // . 字符
}
else if (i == 38)
{
prefix.push_back(','); // " 字符
}
else if (i == 39)
{
prefix.push_back('\''); // ' 字符
}
else if (i == 40)
{
prefix.push_back('-'); // - 字符
}
searchHelper(node->children[i], prefix, result, count);
prefix.pop_back();
}
}
}
// 从 Trie 树中删除键
void TrieTree::remove(const std::string key)
{
if (key.empty())
{
return;
}
TrieNode* current = root;
std::stack<TrieNode*> nodeStack;
std::stack<int> indexStack;
int len = key.length();
// 找到要删除的键所在的最后一个节点
for (int i = 0; i < len; i++)
{
char c = key[i];
int index;
if (std::islower(c))
{
index = c - 'a';
}
else if (std::isdigit(c))
{
index = c - '0' + 26;
}
else
{
// Handle other special characters as needed
if (c == ' ')
{
index = 36; // 空格的索引为36
}
else if (c == '.')
{
index = 37; // . 的索引为37
}
else if (c == ',')
{
index = 38; // " 的索引为38
}
else if (c == '\'')
{
index = 39; // ' 的索引为39
}
else if (c == '-')
{
index = 40; // - 的索引为40
}
else
{
// Skip unsupported characters
continue;
}
}
if (current->children[index] == nullptr)
{
return; // 如果键不存在,则直接返回
}
nodeStack.push(current);
indexStack.push(index);
current = current->children[index];
}
// 删除键标记
current->isEndOfWord = false;
// 删除节点(如果该节点没有子节点且不是其他键的前缀)
if (isNodeDeletable(current))
{
while (!nodeStack.empty())
{
TrieNode* node = nodeStack.top();
int index = indexStack.top();
nodeStack.pop();
indexStack.pop();
TrieNode* child = node->children[index];
node->children[index] = nullptr;
delete child;
// 如果节点不可删除,则终止删除过程
if (!isNodeDeletable(node))
{
break;
}
}
}
}
// 判断节点是否可以删除(没有子节点且不是其他键的前缀)
bool TrieTree::isNodeDeletable(TrieNode* node)
{
for (TrieNode* child : node->children)
{
if (child != nullptr)
{
return false; // 存在子节点,不可删除
}
}
return !node->isEndOfWord; // 不是其他键的前缀,可删除
}
// 迭代释放Trie树节点的内存
void TrieTree::destroyTrie(TrieNode* root)
{
if (root == nullptr)
{
return;
}
std::stack<TrieNode*> nodeStack;
nodeStack.push(root);
while (!nodeStack.empty())
{
TrieNode* current = nodeStack.top();
nodeStack.pop();
for (TrieNode* child : current->children)
{
if (child != nullptr)
{
nodeStack.push(child);
}
}
delete current;
}
}
// 转换成小写
std::string TrieTree::convertToLowerCase(const std::string& word)
{
std::string result = word;
const int length = word.length();
for (int i = 0; i < length; ++i)
{
if (result[i] >= 'A' && result[i] <= 'Z')
{
result[i] |= 32;
}
}
return result;
}