-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree.cpp
373 lines (339 loc) · 9.56 KB
/
binary_tree.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <fstream>
#include <queue>
#include "binary_tree.h"
template<class key_type, class data_type>
binary_tree<key_type, data_type>::binary_tree()
{
start = NULL;
size = 0;
}
//template<class key_type, class data_type>
//bool binary_tree<key_type, data_type>::internal_search(node **& cursor, key_type query, int at, int length) {
// if ((*cursor) == NULL || (*cursor)->left > query && at ) {
// return false;
// }else if ((*cursor)->key.at(at) == query) {
// return true;
// }
// if ((*cursor)->key < query) {
// cursor = &((*cursor)->left);
// return internal_search(cursor, query);
// }
// else {
// cursor = &((*cursor)->right);
// return internal_search(cursor, query);
// }
//}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert(std::string key_in, data_type data_in) {
insert_priv(start, key_in, data_in, 0);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert(std::string key_in) {
insert_priv(start, key_in, 0);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert_priv(node *& cursor, std::string key_in, data_type data_in, int at) {
if (at < key_in.length()) {
if (cursor->key == key_in[at]) {
cursor = cursor->left;
}
else if (cursor->key > key_in[at]) {
node * new_node = new node;
new_node = cursor;
cursor->key = key_in[at];
cursor->left = new_node;
cursor->right = NULL;
}
else if (cursor == NULL) {
cursor = new node;
cursor->key = key_in[at];
cursor->data = NULL;
cursor->left = NULL;
cursor->right = NULL;
}
else if (cursor->key < key_in[at]) {
cursor = cursor->right;
}
insert_priv(cursor, key_in, data_in, ++at);
}
else {
cursor->data = data_in;
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::insert_priv(node *& cursor, std::string key_in, int at) {
if (at < key_in.length()) {
if (key_in == "as") {
std::cout << " ";
}
if (cursor == NULL) {
cursor = new node;
cursor->key = key_in[at];
cursor->data = 0;
cursor->left = NULL;
cursor->right = NULL;
if (at + 1 < key_in.length()) {
cursor = cursor->left;
}
}
else if (cursor->key == key_in[at]) {
cursor = cursor->left;
}
else if (cursor->key > key_in[at]) {
node * new_node = new node;
new_node = cursor;
cursor->key = key_in[at];
cursor->left = new_node;
cursor->right = NULL;
}
else if (cursor->key < key_in[at]) {
cursor = cursor->right;
}
insert_priv(cursor, key_in, ++at);
}
else {
cursor->data++;
}
}
//template<class key_type, class data_type>
//void binary_tree<key_type, data_type>::insert(key_type key_in) {
// node ** local_cursor = &start;
// if (!internal_search(local_cursor, key_in)) {
// *local_cursor = new node;
// (*local_cursor)->key = key_in;
// (*local_cursor)->data = 1;
// (*local_cursor)->left = NULL;
// (*local_cursor)->right = NULL;
// size++;
// }
// else {
// (*local_cursor)->data++;
// }
//}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::min_val(node **& cursor) {
if ((*cursor)->left != NULL) {
min_val((*cursor)->left);
}
else {
return cursor;
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::remove(key_type key_in) {
node ** local_cursor = &start;
if (internal_search(local_cursor, key_in)) {
if ((*local_cursor)->left == NULL && (*local_cursor)->right == NULL) {
delete local_cursor;
}
else if ((*local_cursor)->left != NULL && (*local_cursor)->right == NULL) {
node * temp_cursor = (*local_cursor);
(*local_cursor) = (*local_cursor->left);
delete temp_cursor;
}
else if ((*local_cursor)->left == NULL && (*local_cursor)->right != NULL) {
node * temp_cursor = (*local_cursor);
(*local_cursor) = (local_cursor->right);
delete temp_cursor;
}
else {
node * temp_cursor = *local_cursor;
min_val(local_cursor->right);
temp_cursor = local_cursor;
delete local_cursor;
}
}
else {
std::cout << "there is no matching entry" << std::endl;
}
size--;
}
template<class key_type, class data_type>
bool binary_tree<key_type, data_type>::is_empty() {
if (start == NULL) {
return true;
}
return false;
}
template<class key_type, class data_type>
data_type binary_tree<key_type, data_type>::search(std::string query) {
return search_priv(start, query, 0);
}
template<class key_type, class data_type>
data_type binary_tree<key_type, data_type>::search_priv(node *& cursor, std::string key_in, int at) {
if (at < key_in.length()) {
if (cursor->key == key_in[at]) {
cursor = cursor->left;
}
else if (cursor->key > key_in[at]) {
return NULL;
}
else if (cursor == NULL) {
return NULL;
}
else if (cursor->key < key_in[at]) {
cursor = cursor->right;
}
return search_priv(cursor, key_in, ++at);
}
else {
return cursor->data;
}
/*if (at < key_in.length()) {
if (cursor->key == key_in[at]) {
return cursor->key + search(cursor->left, key_in, ++at);
}
else if (cursor->key > key_in[at]) {
std::cout << "entry not found" << std::endl;
}
else if (cursor == NULL) {
std::cout << "entry not found" << std::endl;
}
else if (cursor->key < key_in[at]) {
cursor = cursor->right;
}
insert(cursor, key_in, data_in, ++at);
}*/
}
//template<class key_type, class data_type>
//data_type binary_tree<key_type, data_type>::search(key_type query) {
// node ** local_cursor = &start;
// if (internal_search(local_cursor, query)) {
// return (*local_cursor)->data;
// }
// else {
// std::cout << std::endl <<std::endl << "search query not found" << std::endl;
// return NULL;
// }
//}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::print_preorder() {
print_preorder(start);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::print_preorder(node * cursor) {
if (cursor != NULL) {
std::cout << "key : " << cursor->key << " data: " << cursor->data << std::endl;
print_preorder(cursor->left);
print_preorder(cursor->right);
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::print_inorder() {
print_inorder_pri(start);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::print_inorder_pri(node * cursor) {
if (cursor != NULL) {
print_inorder_pri(cursor->left);
std::cout << "key : " << cursor->key << " data: " << cursor->data << std::endl;
print_inorder_pri(cursor->right);
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::print_postorder() {
print_postorder(start);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::print_postorder(node * cursor) {
if (cursor != NULL) {
print_postorder(cursor->left);
print_postorder(cursor->right);
std::cout << "key : " << cursor->key << " data: " << cursor->data << std::endl;
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::file_preorder(std::ofstream& out) {
file_preorder(out, start);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::file_preorder(std::ofstream& out, node * cursor) {
if (cursor != NULL) {
out << cursor->key << ' ' << cursor->data << std::endl;
print_preorder(cursor->left);
print_preorder(cursor->right);
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::file_inorder_pri(std::ofstream& out) {
file_inorder(out, start);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::file_inorder(std::ofstream& out, node * cursor) {
if (cursor != NULL) {
file_inorder(out, cursor->left);
out << cursor->key << " " << cursor->data << std::endl;
file_inorder(out, cursor->right);
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::file_postorder(std::ofstream& out) {
file_postorder(out, start);
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::file_postorder(std::ofstream& out, node * cursor) {
if (cursor != NULL) {
print_postorder(cursor->left);
print_postorder(cursor->right);
out << cursor->key << ' ' << cursor->data << std::endl;
}
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::save_file(std::string in_file_name) {
std::ofstream out_file;
in_file_name += ".txt";
out_file.open(in_file_name.c_str());
if (out_file.is_open()) {
file_inorder_pri(out_file);
}
else {
std::cout << "error: unable to open output file" << std::endl;
}
out_file.close();
}
template<class key_type, class data_type>
bool binary_tree<key_type, data_type>::load_file(std::string in_file_name) {
std::ifstream in_file;
in_file_name += ".txt";
in_file.open(in_file_name.c_str());
if (in_file.is_open()) {
while (!in_file.eof()) {
std::string word_temp, num_temp;
in_file >> word_temp >> num_temp;
if (in_file.eof()) {
break;
}
insert(word_temp, std::stoi(num_temp));
}
}
else {
//std::cout << "file does not exist" << std::endl;
return false;
}
return true;
in_file.close();
}
template<class key_type, class data_type>
void binary_tree<key_type, data_type>::print_level() {
node * local_cusor = start;
std::queue<node*> temp_que;
temp_key.push(local_cusor);
while (!temp_que.empty()) {
temp_que.pop(cursor);
std::cout << cursor->key << " " << cursor->data << std::endl;
if (cursor->left != NULL) {
temp_que.push(cursor->left);
}
if (cursor->right != NULL) {
temp_que.push(cursor->right);
}
}
}
template<class key_type, class data_type>
int binary_tree<key_type, data_type>::get_size() {
return size;
}
template<class key_type, class data_type>
binary_tree<key_type, data_type>::~binary_tree()
{
}