1
/**//*
2
이진 탐색 트리(Tree) : 배열 사용
3
*/
4
#include <stdio.h>
5
#include <string.h>
6
7
#define nil -1 //아무것도 가리키지 않는 포인터
8
#define MaxSize 10
9
10
struct Node
11
...{
12
int PrevNode; //왼쪽 부분 트리를 가리키는 포인터
13
char Name[25]; //이름 저장
14
int NextNode; //오른쪽 부분 트리를 가리키는 포인터
15
};
16
17
void main(void)
18
...{
19
struct Node tree[MaxSize] =
20
...{
21
...{1, "mm", 2},
22
...{3, "cc", 4},
23
...{5, "rr", nil},
24
...{nil, "aa", nil},
25
...{6, "ee", 7},
26
...{nil, "nn", nil},
27
...{nil, "dd", nil},
28
...{nil, "ll", nil}
29
};
30
31
char key[20];
32
int current;
33
34
printf("찾을 내용(aa~zz) : ");
35
scanf("%s", key);
36
current = 0;
37
while (current != nil)
38
...{
39
if (strcmp(key, tree[current].Name) == 0)
40
...{
41
printf("찾았습니다.\n");
42
break;
43
}
44
else if (strcmp(key, tree[current].Name) < 0)
45
...{
46
current = tree[current].PrevNode; //왼쪽 부분 트리로 이동
47
}
48
else
49
...{
50
current = tree[current].NextNode; //오른쪽 부분 트리로 이동
51
}
52
}
53
}
54
/**//*
55
찾을 내용(aa~hh) : aa
56
찾았습니다.
57
Press any key to continue
58
*/