-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressedTrie.h
More file actions
177 lines (149 loc) · 5.59 KB
/
CompressedTrie.h
File metadata and controls
177 lines (149 loc) · 5.59 KB
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
#pragma once
#include "Headers.h"
class CompressedTrie {
public:
unordered_map<char, CompressedTrie*> nodes;
string prefix;
bool is_leaf;
CompressedTrie(const string& prefix = "", bool is_leaf = false)
: prefix(prefix), is_leaf(is_leaf) {}
tuple<string, string, string> match(const string& word) {
size_t x = 0;
for (size_t i = 0; i < prefix.size() && i < word.size(); ++i) {
if (prefix[i] != word[i]) break;
x++;
}
return { prefix.substr(0, x), prefix.substr(x), word.substr(x) };
}
void insert_many(const vector<string>& words) {
for (const auto& word : words) {
insert(word);
}
}
void insert(const string& word) {
if (prefix == word && !is_leaf) {
is_leaf = true;
return;
}
if (nodes.find(word[0]) == nodes.end()) {
nodes[word[0]] = new CompressedTrie(word, true);
return;
}
CompressedTrie* incoming_node = nodes[word[0]];
tuple<string, string, string> result = incoming_node->match(word);
string matching_string = std::get<0>(result);
string remaining_prefix = std::get<1>(result);
string remaining_word = std::get<2>(result);
if (remaining_prefix.empty()) {
incoming_node->insert(remaining_word);
}
else {
incoming_node->prefix = remaining_prefix;
CompressedTrie* aux_node = nodes[matching_string[0]];
nodes[matching_string[0]] = new CompressedTrie(matching_string, false);
nodes[matching_string[0]]->nodes[remaining_prefix[0]] = aux_node;
if (remaining_word.empty()) {
nodes[matching_string[0]]->is_leaf = true;
}
else {
nodes[matching_string[0]]->insert(remaining_word);
}
}
}
bool find(const string& word) {
auto it = nodes.find(word[0]);
if (it == nodes.end()) return false;
tuple<string, string, string> result = it->second->match(word);
string matching_string = get<0>(result);
string remaining_prefix = get<1>(result);
string remaining_word = get<2>(result);
if (!remaining_prefix.empty())
return false;
if (remaining_word.empty())
return it->second->is_leaf;
return it->second->find(remaining_word);
}
bool delete_word(const string& word) {
auto it = nodes.find(word[0]);
if (it == nodes.end())
return false;
tuple<string, string, string> result = it->second->match(word);
string matching_string = get<0>(result);
string remaining_prefix = get<1>(result);
string remaining_word = get<2>(result);
if (!remaining_prefix.empty())
return false;
if (!remaining_word.empty()) {
return it->second->delete_word(remaining_word);
}
if (!it->second->is_leaf)
return false;
if (it->second->nodes.empty()) {
delete it->second;
nodes.erase(it);
if (nodes.size() == 1 && !is_leaf) {
CompressedTrie* merging_node = nodes.begin()->second;
is_leaf = merging_node->is_leaf;
prefix += merging_node->prefix;
nodes = move(merging_node->nodes);
delete merging_node;
}
}
else if (it->second->nodes.size() > 1) {
it->second->is_leaf = false;
}
else {
CompressedTrie* merging_node = it->second->nodes.begin()->second;
it->second->is_leaf = merging_node->is_leaf;
it->second->prefix += merging_node->prefix;
it->second->nodes = move(merging_node->nodes);
delete merging_node;
}
return true;
}
void get_similar_results(const string& word, vector<string>& res) {
CompressedTrie* node = this;
string prefix_match = "";
int curIndex = 0;
int prefixInd = 0;
while (curIndex < word.length()) {
for (int i = 0; i < node->prefix.length() && curIndex < word.length(); i++) {
if (node->prefix[i] != word[curIndex]) {
cout << "No matching words found !" << endl;
return;
}
prefix_match += word[curIndex];
curIndex++;
prefixInd = i;
}
if (curIndex >= word.length())
break;
if (curIndex < word.length() && node->nodes.find(word[curIndex]) != node->nodes.end())
node = node->nodes[word[curIndex]];
else if (node->nodes.find(word[curIndex]) == node->nodes.end())
return;
}
get_all_words(node, prefix_match, prefixInd + 1, res);
}
void get_all_words(CompressedTrie* node, const string& prefix, int prefixInd, vector<string>& res) {
string s = prefix;
for (int i = prefixInd; i < node->prefix.length(); i++) {
s += node->prefix[i];
}
if (node->is_leaf) {
res.push_back(s);
}
for (const auto& pair : node->nodes) {
get_all_words(pair.second, s + pair.second->prefix, pair.second->prefix.length(), res);
}
}
void print_tree(int height = 0) const {
if (!prefix.empty()) {
cout << string(height, '-') << " " << prefix
<< (is_leaf ? " (leaf)" : "") << "\n";
}
for (const auto& pair : nodes) {
pair.second->print_tree(height + 1);
}
}
};