-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst.cpp
128 lines (107 loc) · 2.9 KB
/
tst.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
#include "tst.h"
TSTNode::TSTNode(char chr)
{
this->chr = chr;
this->biggerChild = nullptr;
this->selfChild = nullptr;
this->smallerChild = nullptr;
this->isWord = false;
word_head = NULL;
word_last = NULL;
}
TST::TST()
{
}
void TST::add(QString word, FileWordNode *pos_in_file)
{
word = word.toLower();
TSTNode *pointer;
if(!this->root)
{
this->root = new TSTNode(word[0].toLatin1());
pointer = this->root;
for (int i = 1; i < word.size(); ++i) {
pointer->selfChild = new TSTNode(word[i].toLatin1());
pointer = pointer->selfChild;
}
}
else
{
int i = 0;
pointer = this->root;
char chrWord;
for (; i < word.size() - 1;) {
chrWord = word[i].toLatin1();
if (chrWord < pointer->chr) {
if (!pointer->smallerChild)
pointer->smallerChild = new TSTNode(chrWord);
pointer = pointer->smallerChild;
} else if (chrWord > pointer->chr) {
if (!pointer->biggerChild)
pointer->biggerChild = new TSTNode(chrWord);
pointer = pointer->biggerChild;
} else {
if (!pointer->selfChild)
pointer->selfChild = new TSTNode(word[i + 1].toLatin1());
pointer = pointer->selfChild;
i++;
}
}
}
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 TST::search(QString word)
{
word = word.toLower();
int i = 0;
if(!this->root)
return FileWordPositionWrapper(NULL, NULL);
TSTNode *pointer = this->root;
char chrWord;
for (; i < word.size() - 1;) {
chrWord = word[i].toLatin1();
if(chrWord < pointer->chr)
{
if (!pointer->smallerChild)
return FileWordPositionWrapper(NULL, NULL);
else
pointer = pointer->smallerChild;
}
else if(chrWord > pointer->chr)
{
if (!pointer->biggerChild)
return FileWordPositionWrapper(NULL, NULL);
pointer = pointer->biggerChild;
}
else
{
if(! pointer->selfChild)
return FileWordPositionWrapper(NULL, NULL);
pointer = pointer->selfChild;
i++;
}
}
if(pointer->isWord)
{
return FileWordPositionWrapper(pointer->word_head, pointer->word_last);
}
else
{
return FileWordPositionWrapper(NULL, NULL);
}
}
QStringList TST::listAllWords()
{
}