-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.cpp
84 lines (66 loc) · 1.65 KB
/
trie.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
#include "trie.h"
TrieNode::TrieNode()
{
for (int i = 0; i < MAX_ALPH; ++i) {
children[i] = nullptr;
}
isWord = false;
this->word_head = NULL;
this->word_last = NULL;
}
Trie::Trie()
{
this->root = new TrieNode;
}
void Trie::add(QString word, FileWordNode *pos_in_file)
{
word = word.toLower();
TrieNode *pointer = this->root;
int childIndex;
for (int i = 0; i < word.size(); ++i) {
childIndex = word.at(i).toLatin1() - 'a';
if(!pointer->children[childIndex])
pointer->children[childIndex] = new TrieNode;
pointer = pointer->children[childIndex];
}
if(pointer->isWord)
{
pointer->word_last->next_equal = pos_in_file;
pos_in_file->last_equal = pointer->word_last;
pointer->word_last = pos_in_file;
pos_in_file->next_equal = NULL;
}
else
{
pointer->word_head = pos_in_file;
pointer->word_last = pos_in_file;
pointer->isWord = true;
}
}
FileWordPositionWrapper Trie::search(QString word)
{
word = word.toLower();
TrieNode *pointer = this->root;
int childIndex;
for (int i = 0; i < word.size(); ++i) {
childIndex = word.at(i).toLatin1() - 'a';
if(pointer->children[childIndex])
pointer = pointer->children[childIndex];
else
return FileWordPositionWrapper(NULL, NULL);
}
if(pointer->isWord)
return FileWordPositionWrapper(pointer->word_head, pointer->word_last);
else
return FileWordPositionWrapper(NULL, NULL);
}
QStringList Trie::listAllWords()
{
}
void Trie::freeMemory()
{
}
Trie::~Trie()
{
freeMemory();
}