반응형

이전 게시물에서 이진 탐색 트리의 개념을 설명하였다.


코드로 옮기기 전, insert에 대한 코드는 아래와 같이 작성하면 된다.


insert(number X, node N)

if X가 노드 N에 있는 숫자보다 작다면

if N의 왼쪽 자식이 없다면

X를 포함하는 새 노드를 만든 뒤, N의 왼쪽 자식으로 만든다.


else

insert(X, N의 왼쪽 자식)


else X가 노드 N에 있는 숫자보다 크다면

if N의 오른쪽 자식이 없다면

X를 포함하는 새 노드를 만든 뒤, N의 오른쪽 자식으로 만들기


else

insert(X, N의 오른쪽 자식)




소스코드는 다음과 같다.


소스 코드를 검증하기 위해 순회 코드를 넣어 두었다.



< BinarySearchTree.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
#include <stdio.h>
#include <stdlib.h>
 
typedef struct _map
{
    int key;
    struct _map *left;
    struct _map *right;
}map;
 
typedef struct _point
{
    map *root;
}point;
 
 
int init(int val, point *pt)
{
    map *newNode = (map*)malloc(sizeof(map));
    newNode->key = val;
    newNode->left = NULL;
    newNode->right = NULL;
    pt->root = newNode;
    return 1;
}
 
int insert(int val, point *pt)
{
    point *temp = pt;
 
    if (val < temp->root->key)
    {
        if (temp->root->left == NULL)
        {
            map *newNode = (map*)malloc(sizeof(map));
            temp->root->left = newNode;
            newNode->key = val;
            newNode->left = NULL;
            newNode->right = NULL;
        }
 
        else
        {
            insert(val, (point*&(temp->root->left));
        }
    }
 
    // val이 노드 N에 있는 숫자보다 크다면
    else if (val > temp->root->key)
    {
        if (temp->root->right == NULL)
        {
            map *newNode = (map*)malloc(sizeof(map));
            temp->root->right = newNode;
            newNode->key = val;
            newNode->left = NULL;
            newNode->right = NULL;
        }
        else
        {
            insert(val, (point*&(temp->root->right));
        }
        
    }
 
    return 1;
}
 
typedef void VisitFuncPtr(int data); // == void (*VisitFuncPtr)(BTData data);
void ShowData(int data)
{
    printf("%d ", data);
}
 
void PreorderTraverse(point *bt, VisitFuncPtr action)
{
    if (bt->root == NULL)
        return;
 
    action(bt->root->key);
    PreorderTraverse((point*)&(bt->root->left), action);
    PreorderTraverse((point*)&(bt->root->right), action);
}
 
void InorderTraverse(point *bt, VisitFuncPtr action)
{
    if (bt->root == NULL)
        return;
 
    InorderTraverse((point*)&(bt->root->left), action);
    action(bt->root->key);
    InorderTraverse((point*)&(bt->root->right), action);
}
 
void PostorderTraverse(point *bt, VisitFuncPtr action)
{
    if (bt->root == NULL)
        return;
 
    PostorderTraverse((point*)&(bt->root->left), action);
    PostorderTraverse((point*)&(bt->root->right), action);
    action(bt->root->key);
}
 
int main()
{
    point pt;
    int n,val, cnt = 0;
 
    // 몇개의 트리를 넣을지 입력
    scanf("%d"&n);
 
    for (int i = 0; i < n; i++)
    {
        scanf("%d"&val);
 
        if (i == 0)
            init(val, &pt);
        else
            insert(val, &pt);
    }
 
    printf("전위 순회 :: ");
    PreorderTraverse(&pt, ShowData);
    printf("\n");
 
    printf("중위 순회 :: ");
    InorderTraverse(&pt, ShowData);
    printf("\n");
 
    printf("후위 순회 :: ");
    PostorderTraverse(&pt, ShowData);
    printf("\n");
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus







< 결과 화면 >















반응형

'Applied > 자료구조' 카테고리의 다른 글

벡터(Vector) STL 사용 방법 (1)  (2) 2016.11.03
레드 블랙 트리(Red Black Tree) 개념  (0) 2016.10.07
이진 탐색 트리(Binary Search Tree) 개념  (0) 2016.10.06
힙(Heap)개념 (2)  (0) 2016.07.08
힙(Heap) 개념 (1)  (0) 2016.07.07