-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.c
332 lines (286 loc) · 8.46 KB
/
identity.c
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
//*************************************************************
//*** ΠΛΣ50 "Βασικές Εξειδικεύσεις σε Θεωρία και Λογισμικό" ***
//*** Εργασία Ε03 - Θέμα 1 ***
//*** ***
//*** Ακ.έτος 2015-16 ***
//*** ***
//*************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node node;
typedef struct data node_data;
typedef struct binary_tree binary_tree;
//Data of a Binary Tree Node
struct data{
char identity[9];
char name[15];
char sirname[20];
float avg_grade;
};
//Binary Tree Node
struct node{
node_data *data;
node *parent;
node *left;
node *right;
};
//Binary Tree
struct binary_tree{
node *root;
};
binary_tree* init_bt();
node *findMax(node* root);
node* bt_search(char *query, node *root);
node* create_btree();
void print_node(node* n);
void add(node* leaf, node* root);
void delete_element(char *query, node *root);
void inorder(node* bt);
void overwrite(node* A, node* B);
void add_node(char *identity, char *name, char *sirname, float avg_grade, binary_tree* bt);
void read_db_to_tree(binary_tree *);
void print_preorder(node* bt);
int identity(){
binary_tree *tree = init_bt();
read_db_to_tree(tree);
node* tmp;
char buffer[50];
float fbuff;
int cmd;
FILE *f;
while(1){
printf("########################\n");
printf("# 1. Print In-Order #\n");
printf("# 2. Search #\n");
printf("# 3. Modify #\n");
printf("# 4. Delete #\n");
printf("# 5. Exit #\n");
printf("########################\n");
printf("Enter your choice: ");
scanf("%d",&cmd);
switch (cmd){
case 1:
inorder(tree->root);
break;
case 2:
printf("Enter IDENTITY NUMBER: ");
scanf("%s",&buffer);
tmp = bt_search(buffer, tree->root);
if (tmp != NULL) {
printf("\nResult: ");
print_node(tmp);
printf("\n");
}
break;
case 3:
printf("Enter IDENTITY: ");
scanf("%s", &buffer);
tmp = bt_search(buffer, tree->root);
if (tmp != NULL){
printf("Enter Name: ");
scanf("%s",&buffer);
strcpy(tmp->data->name, buffer);
printf("Enter Sirname: ");
scanf("%s",&buffer);
strcpy(tmp->data->sirname, buffer);
printf("Enter Average Grade: ");
scanf("%f",&fbuff);
tmp->data->avg_grade = fbuff;
printf("\n\n");
print_node(tmp);
}
fflush(stdin);
break;
case 4:
printf("Enter IDENTITY: ");
scanf("%s",&buffer);
delete_element(buffer, tree->root);
break;
case 5:
// Clear the previous database file
f = fopen("student_database.txt", "w");
fclose(f);
print_preorder(tree->root);
return 0;
default:
printf("Bad Command\n");
break;
}
}
}
//Binary Tree Initialization
binary_tree* init_bt(){
binary_tree* bt = (binary_tree*)malloc(sizeof(binary_tree));
bt->root = NULL;
return bt;
}
//Binary Tree Print Method
void inorder(node* bt){
if (bt != NULL){
inorder(bt->left);
print_node(bt);
inorder(bt->right);
}
}
//Add Node to Binary Tree (Client Side)
void add_node(char *identity, char *name, char *sirname,
float avg_grade, binary_tree* bt)
{
//Node Initialization
node* leaf = (node*)malloc(sizeof(node));
leaf->left = NULL;
leaf->right= NULL;
//Set Node Data
leaf->data = (node_data*)malloc(sizeof(node_data));
strcpy(leaf->data->identity, identity);
strcpy(leaf->data->name, name);
strcpy(leaf->data->sirname, sirname);
leaf->data->avg_grade = avg_grade;
if (bt->root == NULL){
bt->root = leaf;
}
else add(leaf, bt->root);
}
//Add Node to Binary Tree (Code Side)
void add(node* leaf, node* root){
if ( strcmp(leaf->data->identity, root->data->identity) == -1 ){
if (root->left == NULL){
root->left = leaf;
leaf->parent = root;
}
else{
add(leaf, root->left);
}
}
else{
if (root->right == NULL){
root->right = leaf;
leaf->parent = root;
}
else{
add(leaf, root->right);
}
}
}
//Search in the binary tree
node* bt_search(char *query, node *root){
if (root == NULL){
printf("\nStudent Record not found\n\n");
return NULL;
}
if (strcmp(root->data->identity, query) == 0){
return root;
}
else{
if ( strcmp(query, root->data->identity) == -1 ){
bt_search(query, root->left);
}
else{
bt_search(query, root->right);
}
}
}
//Element deletion
void delete_element(char *query, node *root){
if (root == NULL){
printf("Empty Tree\n");
return;
}
node* del = bt_search(query, root);
node* tmp;
if (del == NULL) return;
//The node has two children
if (del->left!=NULL && del->right!=NULL){
tmp = findMax(del->left);
overwrite(del, tmp);
//Checking if the current node is the left or right parent's child
if (tmp->parent->left == tmp) tmp->parent->left=NULL;
else tmp->parent->right = NULL;
free(tmp->data);
free(tmp);
}
//The node has not any childFILE* fo = fopen( "treedata.txt", "a+" );
else if (del->left==NULL && del->right==NULL){
//Checking if the current node is the left or right parent's child
if (del->parent->left == del) del->parent->left=NULL;
else del->parent->right = NULL;
free(del->data);
free(del);
}
//The node has one child
else{
if (del->left == NULL){
tmp = del->right;
del->right = NULL;
}
else if (del->right == NULL){
tmp = del->left;
del->left = NULL;
}
overwrite(del, tmp);
del->left = tmp->left;
del->right = tmp->right;
free(tmp->data);
free(tmp);
}
}
//Find Max Value(ID) in Binary Tree
node *findMax(node* root){
if (root == NULL){
return NULL;
}
if (root->right == NULL){
return root;
}
}
//Ovewrite the node A with the node B
void overwrite(node* A, node* B){
strcpy(A->data->identity,B->data->identity);
strcpy(A->data->name, B->data->name);
strcpy(A->data->sirname, B->data->sirname);
A->data->avg_grade = B->data->avg_grade;
}
//Full print of the node
void print_node(node* n)
{
printf("%s -- %s -- %s -- %.2f\n", n->data->identity, n->data->name, n->data->sirname, n->data->avg_grade);
//printf("%s\n", n->data->identity);
}
//Read text database to in-memory binary tree
void read_db_to_tree(binary_tree* tree){
FILE *fh, *test;
fh = fopen("student_database.txt","rw");
if (fh == NULL){
printf("NULL");
}
size_t line_size = 50;
char buffer[50];
char *identity = malloc(9*sizeof(char));
char *name = malloc(15*sizeof(char));
char *sirname = malloc(20*sizeof(char));
float avg_grade;
int i;
while(fgets(buffer, line_size, fh)!=NULL){
identity = strtok(buffer," ");
printf("%s\n", identity);
name = strtok(NULL," ");
sirname = strtok(NULL," ");
avg_grade = atof(strtok(NULL, " "));
add_node(identity, name, sirname, avg_grade, tree);
}
fclose(fh);
}
//Write back the in-memory binary tree to text database in pre-order traversal
void print_preorder(node* bt )
{
if( !bt )
{
return;
}
FILE *fo = fopen("student_database.txt", "a+");
fprintf(fo, "%s %s %s %.2f\n", bt->data->identity, bt->data->name, bt->data->sirname, bt->data->avg_grade);
print_preorder(bt->left);
print_preorder(bt->right);
fclose(fo);
}