-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.c
More file actions
366 lines (309 loc) · 6.56 KB
/
Source.c
File metadata and controls
366 lines (309 loc) · 6.56 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
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
/* ELISEEV PAVEL, SMTU, 3170 */
#include <stdio.h>
#include <stdlib.h>
// èíèöèàëèçàöèÿ ñòðóêòóðû óçëà äåðåâà
typedef struct tree {
int item;
tree *parent;
tree *left;
tree *right;
};
// èíèöèàëèçàöèÿ êîðíÿ äåðåâà
tree *root;
// ôóíêöèÿ ñðàâíåíèÿ äâóõ ýëåìåíòîâ
int compare(int a, int b) {
if (a < b) {
return(0);
}else if(a > b) {
return(1);
}
return(2);
}
// ôóíêöèÿ ïîëó÷åíèÿ ðàçìåðà îòêðûòîãî ôàéëà
long int filesize(FILE *file){
long int save_pos, size_of_file;
save_pos = ftell(file);
fseek(file, 0L, SEEK_END);
size_of_file = ftell(file);
fseek(file, save_pos, SEEK_SET);
return(size_of_file);
}
// ôóíêöèÿ ïîèñêà ìèíèìàëüíîãî çíà÷åíèÿ â äåðåâå
tree *findMin(tree *in) {
tree *min;
if (in == NULL) return NULL;
min = in;
while (min->left != NULL) {
min = min->left;
}
return(min);
}
// ôóíêöèÿ ïîèñêà ýëåìåíòà â äåðåâå
tree *search(tree *in, int x) {
if (in == NULL) return(NULL);
switch (compare(x, in->item)) {
case 0:
return search(in->left, x);
case 1:
return search(in->right, x);
default:
return in;
}
}
// ôóíêöèÿ âñòàâêè ýëåìåíòà â äåðåâî
int insert(tree **in, int x, tree *parent) {
tree *temp;
if (*in == NULL) {
temp = (tree*)malloc(sizeof(tree));
temp->item = x;
temp->left = temp->right = NULL;
temp->parent = parent;
*in = temp;
return(1);
}else if ((*in)->item<0) {
temp = (tree*)malloc(sizeof(tree));
temp->item = x;
temp->left = (*in)->left;
temp->right = (*in)->right;
temp->parent = parent;
*in = temp;
return(1);
}
switch (compare(x, (*in)->item)) {
case 0:
return insert(&(*in)->left, x, *in);
case 1:
return insert(&(*in)->right, x, *in);;
default:
return(0);
}
}
// ôóíêöèÿ óäàëåíèÿ óçëà áåç ðàçâåòëåíèé
void deleteSheet(tree *in) {
if (in->parent != NULL) {
if (in->parent->left == in) {
in->parent->left = NULL;
}
else {
in->parent->right = NULL;
}
}
free(in);
}
// ôóíêöèÿ óäàëåíèÿ óçëà ñ îäíîé âåòâüþ
void deleteOneShitNode(tree *in) {
tree *temp;
if (in->left != NULL) {
temp = in->left;
}else {
temp = in->right;
}
if (in->parent->left == in) {
in->parent->left = temp;
}else {
in->parent->right = temp;
}
free(in);
}
// ôóíêöèÿ óäàëåíèÿ óçëà â äåðåâå
void deleteNode(tree *in) {
if ((in->left == NULL) && (in->right == NULL)) {
deleteSheet(in);
}else if((in->left == NULL) xor (in->right == NULL)){
deleteOneShitNode(in);
}else {
tree *temp;
temp = findMin(in->right);
in->item = temp->item;
deleteNode(temp);
}
}
// ôóíêöèÿ óäàëåíèÿ ýëåìåíòà â äåðåâå
void deleteItem(tree **in,int x) {
deleteNode(search((*in),x));
}
// ôóíêöèÿ ñîõðàíåíèÿ äåðåâà â ôàéë
void saveTree(tree **in, FILE *file) {
if ((*in) != NULL) {
fwrite(&(*in)->item, sizeof(int), 1, file);
saveTree(&(*in)->left, file);
saveTree(&(*in)->right, file);
}
}
// ôóíêöèÿ çàãðóçêè äåðåâà èç ôàéëà
void loadTree(tree **in, FILE *file) {
int temp;
while (!feof(file)) {
fread(&temp, sizeof(int), 1, file);
if (temp >= 0) {
insert(&(*in), temp, NULL);
}
}
}
// ôóíêöèÿ óäàëåíèÿ âñåãî äåðåâà
void deleteTree(tree *&in){
if (in != NULL){
deleteTree(in->left);
deleteTree(in->right);
free(in);
in = NULL;
}
}
// ôóíêöèÿ îòîáðàæåíèÿ ýëåìåíòîâ â äåðåâå
void showTree(tree *in) {
if (in != NULL) {
if (in->item<0) {
printf("N ");
}else{
printf("%d ", in->item);
}
showTree(in->left);
showTree(in->right);
}
}
// ôóíêöèÿ îïðåäåëåíèÿ âûñîòû äåðåâà
int getHeight(tree *in) {
if (in == NULL) {
return -1;
}
int lefth = getHeight(in->left);
int righth = getHeight(in->right);
if(lefth > righth){
return lefth + 1;
}else{
return righth + 1;
}
}
// ôóíêöèÿ îïðåäåëåíèÿ øëóáèíû óçëà â äåðåâå
int getDept(tree *in) {
if(in->parent == NULL){
return 0;
}else{
return 1 + getDept(in->parent);
}
}
// ôóíêöèÿ äîñòðîéêè ñòðóêòóðû äåðåâà
void completeTree(tree **in) {
if ((*in) != NULL) {
if ((*in)->left == NULL) {
if (getDept((*in)) != getHeight(root)) {
tree *newLNode;
newLNode = (tree*)malloc(sizeof(tree));
newLNode->parent = (*in);
newLNode->item = -1;
newLNode->left = NULL;
newLNode->right = NULL;
(*in)->left = newLNode;
}
}
if ((*in)->right == NULL) {
if (getDept((*in)) != getHeight(root)) {
tree *newRNode;
newRNode = (tree*)malloc(sizeof(tree));
newRNode->parent = (*in);
newRNode->item = -1;
newRNode->left = NULL;
newRNode->right = NULL;
(*in)->right = newRNode;
}
}
completeTree(&(*in)->left);
completeTree(&(*in)->right);
}
}
// ôóíêöèÿ äëÿ îòîáðàæåíèÿ ìåíþ
void showMenu() {
system("cls");
printf("Commands:\n\n1 - Delete Tree\n2 - Add item\n3 - Complete Tree`s structure\n4 - Show Tree Items\n5 - View height of Tree\n6 - Searh Item\n7 - Delete item\n8 - Save Tree to tree.txt\n9 - Load Tree from tree.txt\n10 - Exit\n\n");
}
// ôóíêöèÿ îáðàáîòêè êîìàíä
void commandsHandling() {
int cmd;
int item;
FILE *file;
errno_t err;
printf("\nType command and press Enter: ");
scanf_s("%d",&cmd);
showMenu();
switch ((int)cmd){
case 1:
deleteTree(root);
printf("Tree was deleted\n");
break;
case 2:
int item;
printf("\nType item and press Enter: ");
scanf_s("%d",&item);
if (item >= 0) {
insert(&root, item, NULL);
printf("Item was added\n");
}else {
printf("Item wasn`t added\n");
}
break;
case 3:
completeTree(&root);
printf("Tree`s structure was completed\n");
break;
case 4:
printf("Tree items:\n");
showTree(root);
printf("\n");
break;
case 5:
printf("Height of Tree is %d\n",getHeight(root));
break;
case 6:
printf("\nType desired item and press Enter: ");
scanf_s("%d", &item);
if (search(root, item) != NULL) {
printf("Item was found\n");
}
else {
printf("Item wasn`t found\n");
}
break;
case 7:
printf("\nType desired item for delete and press Enter: ");
scanf_s("%d", &item);
deleteItem(&root,item);
printf("Item was deleted\n");
break;
case 8:
err = fopen_s(&file,"tree.txt","wb");
if (err == 0) {
saveTree(&root, file);
fclose(file);
printf("Tree was saved\n");
}else {
printf("Tree wasn`t saved\n");
}
break;
case 9:
deleteTree(root);
err = fopen_s(&file,"tree.txt", "rb");
if (err == 0) {
if (filesize(file) > 0) {
loadTree(&root, file);
fclose(file);
printf("Tree was loaded\n");
break;
}
}
printf("Bad tree.txt\n");
break;
case 10:
printf("Exit...\n");
exit(0);
break;
default:
printf("Fail command\n");
break;
}
commandsHandling();
}
// ôóíêöèÿ íà÷àëà ðàáîòû ïðîãðàììû
int main() {
showMenu();
commandsHandling();
}