반응형




< BinaryTree.h >


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
#pragma once
 
/*
    구조체 및 함수들에 대한 정의를 수록
*/
typedef int BTData;
 
typedef struct _bTreeNode
{
    BTData data;
    struct _bTreeNode *left;
    struct _bTreeNode *right;
}BTreeNode;
 
BTreeNode *MakeBTreeNode(void);
BTData GetData(BTreeNode *bt);
void SetData(BTreeNode *bt, BTData data);
 
BTreeNode *GetLeftSubTree(BTreeNode *bt);
BTreeNode *GetRightSubTree(BTreeNode *bt);
 
void MakeLeftSubTree(BTreeNode *main, BTreeNode *sub);
void MakeRightSubTree(BTreeNode *main, BTreeNode *sub);
 
typedef void VisitFuncPtr(BTData data); // == void (*VisitFuncPtr)(BTData data);
 
void PreorderTraverse(BTreeNode *bt, VisitFuncPtr action); // 전위 순회
void InorderTraverse(BTreeNode *bt, VisitFuncPtr action); // 중위 순회
void PostorderTraverse(BTreeNode *bt, VisitFuncPtr action); // 후위 순회
Crocus





< BinaryTree.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
#include <stdio.h>
#include <stdlib.h>
#include "BinaryTree.h"
 
/*
    함수들의 실제 내용을 적어 놓은 곳
*/
 
BTreeNode *MakeBTreeNode(void)
{
    BTreeNode *nd = (BTreeNode *)malloc(sizeof(BTreeNode));
    nd->left = NULL;
    nd->right = NULL;
    return nd;
}
 
BTData GetData(BTreeNode *bt)
{
    return bt->data;
}
 
void SetData(BTreeNode *bt, BTData data)
{
    bt->data = data;
}
 
BTreeNode *GetLeftSubTree(BTreeNode *bt)
{
    return bt->left;
}
 
BTreeNode *GetRightSubTree(BTreeNode *bt)
{
    return bt->right;
}
 
void MakeLeftSubTree(BTreeNode *main, BTreeNode *sub)
{
    if (main->left != NULL)
        free(main->left);
 
    main->left = sub;
}
 
void MakeRightSubTree(BTreeNode *main, BTreeNode *sub)
{
    if (main->right != NULL)
        free(main->right);
 
    main->right = sub;
}
 
void PreorderTraverse(BTreeNode *bt, VisitFuncPtr action)
{
    if (bt == NULL)
        return;
 
    action(bt->data);
    PreorderTraverse(bt->left, action);
    PreorderTraverse(bt->right, action);
}
 
void InorderTraverse(BTreeNode *bt, VisitFuncPtr action)
{
    if (bt == NULL)
        return;
 
    InorderTraverse(bt->left, action);
    action(bt->data);
    InorderTraverse(bt->right, action);
}
 
void PostorderTraverse(BTreeNode *bt, VisitFuncPtr action)
{
    if (bt == NULL)
        return;
 
    PostorderTraverse(bt->left, action);
    PostorderTraverse(bt->right, action);
    action(bt->data);
}
Crocus





< main.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
#include <stdio.h>
#include "BinaryTree.h"
 
void ShowData(int data)
{
    printf("%d ", data);
}
int main(void)
{
    BTreeNode *tree1 = MakeBTreeNode(); // 노드 tree1 생성
    BTreeNode *tree2 = MakeBTreeNode(); // 노드 tree2 생성
    BTreeNode *tree3 = MakeBTreeNode(); // 노드 tree3 생성
    BTreeNode *tree4 = MakeBTreeNode(); // 노드 tree4 생성
    BTreeNode *tree5 = MakeBTreeNode(); // 노드 tree5 생성
    BTreeNode *tree6 = MakeBTreeNode(); // 노드 tree6 생성
 
    SetData(tree1, 1); // tree1에 data 1을 저장한다.
    SetData(tree2, 2); // tree2에 data 2를 저장한다.
    SetData(tree3, 3); // tree3에 data 3을 저장한다.
    SetData(tree4, 4); // tree4에 data 4를 저장한다.
    SetData(tree5, 5); // tree5에 data 5를 저장한다.
    SetData(tree6, 6); // tree6에 data 6을 저장한다.
 
    MakeLeftSubTree(tree1, tree2); // tree1의 왼쪽 자식노드 = tree2
    MakeRightSubTree(tree1, tree3); // tree1의 오른쪽 자식노드 = tree3
    MakeLeftSubTree(tree2, tree4); // tree2의 왼쪽 자식노드 = tree4
    MakeRightSubTree(tree2, tree5); // tree2의 오른쪽 자식노드 = tree5
    MakeLeftSubTree(tree3, tree6); // tree3의 왼쪽 자식노드 = tree6
 
 
    PreorderTraverse(tree1, ShowData);
    printf("\n");
    InorderTraverse(tree1, ShowData);
    printf("\n");
    PostorderTraverse(tree1, ShowData);
    printf("\n");
 
    return 0;
}
Crocus




< 생성되는 트리 구조 >





< 전위 순회, 중위 순회, 후위 순회 순서 >











반응형