일반 트리


  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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable:4996)

#define MAX 10

typedef struct ano * nodeptr;
typedef struct ano {
 char name[30];
 float gpa;
 char place[100];
 nodeptr links[MAX];
}nodetype;

nodeptr ROOT;
nodeptr stack[100];
int top = -1;

nodeptr search_node(nodeptr cur, char sname[]) {
 int i;
 nodeptr rptr;
 if (!cur)
  return NULL;
 if (strcmp(cur->name, sname) == 0) {
  return cur; //성공
 }
 else {
  for (i = 0; cur->links[i] != NULL; i++) {
   rptr = search_node(cur->links[i], sname);
   if (rptr)
    return rptr;
  }
  return NULL;
 }
} //search_node

int read_upper(FILE *fp, char buf[20][50]) {
 //read "parent-child" lines to creat the tree.
 int i, j, k;
 char line[400];
 char *cp;
 cp = fgets(line, 400, fp);
 if (!cp) {
  printf("impossible for the control to reach here.\n");
  return 0;
 }
 else {
  if (line[0] == '-')
   return 0; //첫 영역에 대한 읽기 종료

  i = 0;
  j = 0;

  while (1) {
   k = 0;
   while (line[j] != ' ' && line[j] != '\n') {
    buf[i][k] = line[j];
    j++;
    k++;
   }
   buf[i][k] = '\0'; //finish one name
   i++; //increase the name count
   if (line[j] == '\n')
    break;
   else {
    do
     j++; while (line[j] == ' ');
   }
  }
  return i;
 }
}

void read_store_lower_data(FILE *fp) {
 char rname[30], address[100];
 float rgpa;
 nodeptr np;
 int read_num;

 do {
  read_num = fscanf(fp, "%s%f%s", rname, &rgpa, address);
  if (read_num != 3)
   break;
  else {
   np = search_node(ROOT, rname);
   if (!np)
    printf("이런 이름을 가진 노드가 없습니다.\n");
   else {
    strcpy(np->name, rname);
    np->gpa = rgpa;
    strcpy(np->place, address);
   }
  }
 } while (1);
}

void print_general_tree(nodeptr curr) {
 int i;
 if (!curr)
  return;

 printf("이름:%s 학점:%5.2f 주소:%s \n", curr->name, curr->gpa, curr->place);
 for (i = 0; curr->links[i]; i++)
  print_general_tree(curr->links[i]);
}

void make_node_and_attach_to_parent(nodeptr parent, char *tname, int loc) {
 nodeptr np1;
 np1 = (nodeptr)malloc(sizeof(nodetype));
 strcpy(np1->name, tname);
 np1->links[0] = NULL;
 parent->links[loc] = np1;
}

void dfs_height(nodeptr cur, int par_height, int *tree_height) {
 int my_height = par_height + 1;
 int i;
 if (*tree_height < my_height)
  *tree_height = my_height;
 for (i = 0; cur->links[i]; i++)
  dfs_height(cur->links[i], my_height, tree_height);
 return;
}

int dfs_depth(nodeptr cur, int par_height, char *sname) {
 int my_height = par_height + 1;
 int i, res;
 if (strcmp(cur->name, sname) == 0) {
  printf("Height of the node of %s : %d\n", sname, my_height);
  return 1;
 }

 for (i = 0; cur->links[i]; i++) {
  res = dfs_depth(cur->links[i], my_height, sname);
  if (res)
   return 1;
 }
 return 0;
}

void push_stack(nodeptr nptr) {
 top++;
 stack[top] = nptr;
}

void pop_stack() {
 top--;
}

int dfs_ancestors(nodeptr cur, char *sname) {
 int i, res;
 if (strcmp(cur->name, sname) == 0) {
  printf("This person's ancestors : ");
  for (i = 0; i <= top; i++)
   printf("%s   ", stack[i]->name);
  printf("\n");

  return 1;
 }
 push_stack(cur);
 for (i = 0; cur->links[i]; i++) {
  res = dfs_ancestors(cur->links[i], sname);
  if (res)
   return 1;
 }

 pop_stack();
 return 0;
}

int num_child(nodeptr cur) {
 //if null - return
 int sum = 0;
 int i;
 
 if (!cur)
  return 0;
 for (i = 0; cur->links[i]; i++) {
  sum++;
 }
 for (i = 0; cur->links[i]; i++) {
  sum += num_child(cur->links[i]);
 }

 return sum;
}

void heyBro(nodeptr cur, char *sname) {
 int i,j;
 
 if (!cur)
  return;

 for (i = 0; cur->links[i]; i++)
  heyBro(cur->links[i], sname);

 for (i = 0; cur->links[i]; i++) {
  if (strcmp(cur->links[i]->name, sname) == 0) {//부모찾기
   printf("형제 : ");
   for (j = 0; cur->links[j]->name; j++) {
    if (i != j)
     printf("%s   ", cur->links[j]->name);
   }
   break;
  }
 }

}


void main() {
 char buf[20][50];
 int num_persons, k,i;
 nodeptr np;
 char command[300];
 char lines[300];
 char name[20];
 int res;

 FILE *fp;
 fp = fopen("tree_data.txt", "r");
 if (!fp) {
  printf("file open error.\n");
  return;
 }

 do {
  num_persons = read_upper(fp, buf);
  
  if (num_persons == 0)
   break;
  if (!ROOT) {
   
   np = (nodeptr)malloc(sizeof(nodetype));
   strcpy(np->name, buf[0]);
   ROOT = np;
   np->links[0] = NULL;
   for (k = 1; k < num_persons; k++)
    make_node_and_attach_to_parent(np, buf[k], k - 1);

   np->links[k - 1] = NULL;
  }

  else {
   np = search_node(ROOT, buf[0]);
   if (!np) {
    printf("Error: 2번째 줄 이하의 첫 이름의 노드가 없음.\n");
    getchar();
   }
   for (k = 1; k < num_persons; k++)
    make_node_and_attach_to_parent(np, buf[k], k - 1);
   np->links[k - 1] = NULL;

  }
 } while (1);

 read_store_lower_data(fp);

 print_general_tree(ROOT);

 do {
  printf("Type a command> ");
  gets(lines);

  i = 0;
  while (lines[i] == ' ' || lines[i] == '\t')i++;
  k = 0;
  while (!(lines[i] == ' ' || lines[i] == '\t' || lines[i] == '\0')) { //line
   command[k] = lines[i]; i++; k++;
  }
  command[k] = '\0';

  if (strcmp(command, "ex") == 0)
   break;
  else if (strcmp(command, "ht") == 0) {
   int tree_height = 0;
   dfs_height(ROOT, 0, &tree_height);
   printf("Height of the tree : %d \n", tree_height);
   continue;
  }
  else;

  k = 0;
  while (lines[i] == ' ' || lines[i] == '\t') i++;
  while (!(lines[i] == ' ' || lines[i] == '\t' || lines[i] == '\0')) {
   name[k] = lines[i]; 
   i++; 
   k++;
  }
  name[k] = '\0';

  if (strcmp(command, "se") == 0) { 
   np = search_node(ROOT, name);
   if (np)
    printf("Search success: %s %5.2f %s\n", np->name, np->gpa, np->place);
   else
    printf("Such a person does not exist.\n");

  }
  else if (strcmp(command, "dp") == 0) {
   res = dfs_depth(ROOT, 0, name);
   if (!res)
    printf("Such a name does not exist in the tree.\n");
  }
  else if (strcmp(command, "ac") == 0) {
   top = -1; //스택은 처음에 비어있음
   res = dfs_ancestors(ROOT, name);
   if (!res)
    printf("Such a name does not exist in the tree.\n");
  }

  else if (strcmp(command, "nm") == 0) { //자손 수 출력
   np = search_node(ROOT, name);
   if (np) {
    printf("%s의 자손 수 : %d \n", name, num_child(np));
   }
   else
    printf("그런 이름이 없습니다.\n");
  }
  
  else if (strcmp(command, "br") == 0) { //형제 출력  
   heyBro(ROOT, name);
   printf("\n");
  }
 
  else
   printf("Wrong Command.\n");

 } while (1);
}
/*
명령 종류
se 상현- 데이터 출력
ht -트리의 높이 출력
dp 은진 -  은진이 존재하는 깊이 출력
ac 명숙 - 조상 출력
nm 상현 - 자손들의 수 출력
br 미라 - 형제 출력 
ex 종료

*/

댓글