-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice9_9.c
54 lines (46 loc) · 1.47 KB
/
practice9_9.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
#include<stdio.h>
#include<stdlib.h>
typedef int element; // 이진 탐색 트리 element의 자료형을 int로 정의
typedef struct treeNode {
element key; // 데이터 필드
struct treeNode* left; // 왼쪽 서브 트리 링크 필드
struct treeNode* right; // 오른쪽 서브 트리 링크 필드
} treeNode;
// 노드 x를 삽입하는 연산 : [예제 7-3] 26~41행
treeNode* insertNode(treeNode *p, element x) {
treeNode *newNode;
if (p == NULL) {
newNode = (treeNode*)malloc(sizeof(treeNode));
newNode->key = x;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
else if (x < p->key) p->left = insertNode(p->left, x);
else if (x > p->key) p->right = insertNode(p->right, x);
else printf("\n 이미 같은 키가 있습니다! \n");
return p;
}
// 이진 탐색 트리를 중위 순회하면서 경로를 출력하는 연산 : [예제 7-3] 101~108행
void displayInorder(treeNode* root) {
if (root) {
displayInorder(root->left);
printf("%d ", root->key);
displayInorder(root->right);
}
}
// 트리 정렬 연산
void treeSort(element a[], int n) {
treeNode* root = NULL; // 공백 이진 탐색 트리 생성
root = insertNode(root, a[0]); // a[0]을 공백 이진 탐색 트리의 루트로 삽입
for (int i = 1; i<n; i++) // a[1]~a[n-1]의 원소들을
insertNode(root, a[i]); // 삽입하면서 이진 탐색 트리 구성
displayInorder(root); // 이진 탐색 트리를 중위 순회한 경로 출력
}
int main() {
element list[8] = { 69, 10, 30, 2, 16, 8, 31, 22 };
int size = 8;
printf("\n <<<<< 트리 정렬 수행 >>>>>> \n\n ");
treeSort(list, size);
getchar();
}