반응형





< 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
30
31
#include <stdio.h>
#include "BinaryTree.h"
 
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
 
    printf("%d \n", GetData(GetLeftSubTree(tree1))); // 루트노드의 왼쪽노드 값
 
    printf("%d \n", GetData(GetLeftSubTree(GetRightSubTree(tree1)))); // 루트노드의 오른쪽노드의 왼쪽노드 값
 
    return 0;
}
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
#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;
}
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
#include <stdio.h>
#include "BinaryTree.h"
 
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
 
    printf("%d \n", GetData(GetLeftSubTree(tree1))); // 루트노드의 왼쪽노드 값
 
    printf("%d \n", GetData(GetLeftSubTree(GetRightSubTree(tree1)))); // 루트노드의 오른쪽노드의 왼쪽노드 값
 
    return 0;
}
Crocus




< 만들어지는 트리 구조 >




< printf에 의해 출력되는 결과 값 >



반응형