-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
186 lines (153 loc) · 5.04 KB
/
main.cpp
File metadata and controls
186 lines (153 loc) · 5.04 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
178
179
180
181
182
183
184
185
186
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include <memory>
//#define _DEBUG
//#define _DEFDATA
struct RadixTree
{
private:
struct Node;
struct NodeComparator {
bool operator()(std::unique_ptr<Node> const &a, std::unique_ptr<Node> const &b) {
return a->label < b->label;
}
};
struct Node {
std::string label;
bool isEnd;
std::set<std::unique_ptr<Node>, NodeComparator> childs;
Node(const std::string &label, int isEnd, std::set<std::unique_ptr<Node>, NodeComparator> childs = std::set<std::unique_ptr<Node>, NodeComparator>())
: label(label)
, isEnd(isEnd)
, childs(std::move(childs))
{
}
~Node()
{
#ifdef _DEBUG
std::cout << "delete " << label << " " << isEnd << std::endl;
#endif
childs.clear();
}
bool insert(std::string &word, int depth = 0)
{
#ifdef _DEBUG
std::cout << "Start insert '" << word << "' into node '" << label << "'" << std::endl;
#endif
std::string equalPart;
for (int i = 0; i < static_cast<int>(std::min(word.length(), label.length())); ++i)
{
#ifdef _DEBUG
std::cout << " " << word.at(i) << " == " << label.at(i) << " -> " << (word.at(i) == label.at(i)) << std::endl;
#endif
if (word.at(i) == label.at(i))
equalPart += word.at(i);
else
break;
}
#ifdef _DEBUG
std::cout << "equalPart '" << equalPart << "' " << word << " " << label << std::endl;
#endif
if (equalPart.length() < label.length())
{
if (equalPart.length() == 0 && depth > 0)
{
#ifdef _DEBUG
std::cout << "equalPart is empty: '" << equalPart << "'" << std::endl;
#endif
return false;
}
auto childNode = std::make_unique<Node>(label.substr(equalPart.length()), isEnd, std::move(childs));
label = equalPart;
isEnd = equalPart.length() == word.length();
//childs are empty now, because we move their memory to created 'childNode'. Right? =)
childs.clear();
childs.insert(std::move(childNode));
if (equalPart.length() == 0)
childs.insert(std::make_unique<Node>(word, true));
if (equalPart.length() > 0 && equalPart.length() != word.length())
childs.insert(std::make_unique<Node>(word.substr(equalPart.length()), true));
return true;
}
depth++;
for ( auto &child : childs )
{
std::string subWord = word.substr(equalPart.length());
bool res = child->insert(subWord, depth);
if (res)
return true;
}
childs.insert(std::make_unique<Node>(word.substr(equalPart.length()), true));
return true;
}
};
std::unique_ptr<Node> m_root;
public:
RadixTree() = default;
~RadixTree()
{ }
void insert(std::string &word)
{
if (m_root == nullptr)
{
m_root = std::make_unique<Node>(word, true);
#ifdef _DEBUG
std::cout << "Create first node " << word << std::endl;
#endif
}
else
{
m_root->insert(word);
}
}
void toTree(Node *node = nullptr, int depth = 0, bool quotes = false)
{
if (node == nullptr)
node = m_root.get();
//if user try to print empty tree
if (node == nullptr)
return;
std::string space;
for (int i = 0; i < depth; ++i)
{
space += " ";
}
std::cout << space << (quotes ? "\"" : "") << node->label << (quotes ? "\"" : "") << (node->isEnd ? "$" : "") << std::endl;
depth++;
for ( auto &child : node->childs )
toTree(child.get(), depth);
}
void toList(Node *node = nullptr, std::string parentLabel = std::string())
{
if (node == nullptr)
node = m_root.get();
//if user try to print empty tree
if (node == nullptr)
return;
if (node->isEnd)
std::cout << (parentLabel + node->label) << " " << (parentLabel + node->label == parentLabel ? parentLabel + node->label : parentLabel + node->label[0] ) << std::endl;
for ( auto &child : node->childs )
toList(child.get(), parentLabel + node->label);
}
};
//$ nickname < testdata.txt
//cyrillic test: echo -e "Данил\nДенис\nДаша" | iconv -t cp1251 | ./nickname | iconv -f cp1251
int main(int, char *[])
{
RadixTree tree;
#ifdef _DEFDATA
std::ifstream file;
file.open("testdata.txt");
for(std::string line; std::getline(file, line);)
#else
for(std::string line; std::getline(std::cin, line);)
#endif
{
tree.insert(line);
}
tree.toList();
tree.toTree();
return 0;
}