This repository was archived by the owner on Oct 9, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathb_tree.cpp
More file actions
257 lines (234 loc) · 7.77 KB
/
Copy pathb_tree.cpp
File metadata and controls
257 lines (234 loc) · 7.77 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
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
// Author: Samed Kahyaoglu
// Github: urtuba
// Restored: 2026
#include <fstream>
#include <iostream>
#include <istream>
#include <stdexcept>
#include <string>
#include <vector>
// Thrown when the input is wrong. The message names the line.
class InputError : public std::runtime_error {
public:
InputError(int line, const std::string& what)
: std::runtime_error("line " + std::to_string(line) + ": " + what) {}
};
enum class NumberStatus { Ok, NotANumber, OutOfRange };
// Reads a whole number (optional sign, then digits) that fits in an int.
NumberStatus parse_int(const std::string& word, int& value) {
size_t i = 0;
bool negative = false;
if (!word.empty() && (word[0] == '+' || word[0] == '-')) {
negative = (word[0] == '-');
i = 1;
}
if (i == word.size()) {
return NumberStatus::NotANumber;
}
// The magnitude stops growing once it is clearly too big for an int.
unsigned long long magnitude = 0;
for (; i < word.size(); i++) {
if (word[i] < '0' || word[i] > '9') {
return NumberStatus::NotANumber;
}
if (magnitude <= 2147483648ULL) {
magnitude = magnitude * 10 + static_cast<unsigned>(word[i] - '0');
}
}
if (magnitude > (negative ? 2147483648ULL : 2147483647ULL)) {
return NumberStatus::OutOfRange;
}
value = negative ? static_cast<int>(-static_cast<long long>(magnitude))
: static_cast<int>(magnitude);
return NumberStatus::Ok;
}
// Splits a line into words. Spaces, tabs and a carriage return (from a
// Windows line ending) separate words.
std::vector<std::string> split_words(const std::string& line) {
std::vector<std::string> words;
std::string word;
for (char c : line) {
if (c == ' ' || c == '\t' || c == '\r') {
if (!word.empty()) {
words.push_back(word);
word.clear();
}
} else {
word += c;
}
}
if (!word.empty()) {
words.push_back(word);
}
return words;
}
// A word for an error message. Long words are cut.
std::string shown(const std::string& word) {
if (word.size() > 20) {
return "'" + word.substr(0, 20) + "...'";
}
return "'" + word + "'";
}
// One node of the tree. Nodes are numbered level by level, left to right,
// starting with 1 at the root. Node k has the children 2k and 2k+1.
struct Node {
int value;
int number;
Node* left;
Node* right;
};
// Which subtree of the root a path ends in.
enum class Side { Left, Right };
// The best path found for one side.
struct Result {
bool found = false;
std::string text = "No Path Found";
};
// A complete binary tree that owns its nodes and frees them when destroyed.
class Tree {
public:
Tree() = default;
Tree(const Tree&) = delete;
Tree& operator=(const Tree&) = delete;
~Tree() {
for (Node* node : nodes_) {
delete node;
}
}
// Line 1 holds the node values, line 2 holds the target. Nothing but
// whitespace may follow. Throws InputError if the input breaks a rule.
void read(std::istream& input) {
std::string line;
if (!std::getline(input, line)) {
throw InputError(1, "input is empty");
}
std::vector<std::string> words = split_words(line);
if (words.empty()) {
throw InputError(1, "no values (expected whole numbers separated by spaces)");
}
for (const std::string& word : words) {
int value = 0;
NumberStatus status = parse_int(word, value);
if (status == NumberStatus::NotANumber) {
throw InputError(1, "value " + shown(word) + " is not a whole number");
}
if (status == NumberStatus::OutOfRange) {
throw InputError(1, "value " + shown(word) + " is out of int range");
}
add_node(value);
}
if (!std::getline(input, line)) {
throw InputError(2, "target is missing");
}
words = split_words(line);
if (words.empty()) {
throw InputError(2, "target is missing");
}
int target = 0;
NumberStatus status = parse_int(words[0], target);
if (status == NumberStatus::NotANumber) {
throw InputError(2, "target must be a whole number, got " + shown(words[0]));
}
if (status == NumberStatus::OutOfRange) {
throw InputError(2, "target " + shown(words[0]) + " is out of int range");
}
if (words.size() > 1) {
throw InputError(2, "target must be one number, got a second: " + shown(words[1]));
}
target_ = target;
int line_number = 2;
while (std::getline(input, line)) {
line_number++;
words = split_words(line);
if (!words.empty()) {
throw InputError(line_number,
"unexpected data after the target: " + shown(words[0]));
}
}
}
// Search both subtrees of the root and print the left path, then the
// right path. A path has at least one node below the root, because the
// root alone is in neither subtree.
void print_paths() {
if (!nodes_.empty()) {
const Node* root = nodes_[0];
std::string text = "Path Found: " + std::to_string(root->value);
if (root->left != nullptr) {
search(root->left, root->value, text, Side::Left);
}
if (root->right != nullptr) {
search(root->right, root->value, text, Side::Right);
}
}
std::cout << left_.text << '\n';
std::cout << right_.text << '\n';
}
private:
// nodes_[k - 1] is node number k, so the parent of node k is node k / 2.
std::vector<Node*> nodes_;
long long target_ = 0;
Result left_;
Result right_;
// Put the value in the first free place, so that the tree stays complete.
void add_node(int value) {
int number = static_cast<int>(nodes_.size()) + 1;
Node* node = new Node{value, number, nullptr, nullptr};
nodes_.push_back(node);
if (number == 1) {
return;
}
Node* parent = nodes_[number / 2 - 1];
if (number % 2 == 0) {
parent->left = node;
} else {
parent->right = node;
}
}
// Preorder search of one subtree. It carries the sum and the text of the
// path from the root to this node. The first path found for a side is kept.
void search(const Node* node, long long sum, std::string text, Side side) {
sum += node->value;
text += " " + std::to_string(node->value);
if (sum == target_) {
Result& result = (side == Side::Left) ? left_ : right_;
if (!result.found) {
result.found = true;
result.text = text;
}
}
if (node->left != nullptr) {
search(node->left, sum, text, side);
}
if (node->right != nullptr) {
search(node->right, sum, text, side);
}
}
};
// Reads the tree from the stream and prints the two paths.
// Returns the exit status.
int run(std::istream& input) {
try {
Tree tree;
tree.read(input);
tree.print_paths();
} catch (const InputError& e) {
std::cerr << "error: " << e.what() << '\n';
return 1;
}
return 0;
}
int main(int argc, char* argv[]) {
if (argc > 2) {
std::cerr << "usage: b_tree [INPUT_FILE]\n";
return 2;
}
if (argc == 1) {
return run(std::cin);
}
std::ifstream file(argv[1]);
if (!file) {
std::cerr << "error: cannot open " << argv[1] << '\n';
return 1;
}
return run(file);
}