티스토리 뷰

반응형

* 트리 표현법

1. Nested Parenthesis (중첩된 괄호로 표현)

2. Nested Set(벤 다이어그램처럼 그려서 표현)

3. Indentation(들여쓰기, 윈도우즈 파일 탐색기) <--선택 


* 노드 표현법

1. N-Link (Data와 N개의 자식포인터. 단, 노드의 각 차수가 서로 다를 경우 비효율적)

2. Left Child - Right Sibling(Data,Left Child,Right Sibling, 단 2개의 포인터만 필요. 줄여서 LCRS라 칭하겠다) <--선택 





1. LCRSTree.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
#ifndef LCRS_TREE_H
 
#define LCRS_TREE_H
 
#include <stdio.h>
#include <stdlib.h>
 
typedef char ElementType;
 
typedef struct tagLCRSNode{
    ElementType Data;
    struct tagLCRSNode* LeftChild;
    struct tagLCRSNode* RightSibling;
}LCRSNode;
 
LCRSNode* LCRS_CreateNode(ElementType NewData);
void LCRS_DestroyNode(LCRSNode* Node);
void LCRS_DestroyTree(LCRSNode* Root);
 
void LCRS_AddChildNode(LCRSNode* ParentNode,LCRSNode* ChildNode);
void LCRS_PrintTree(LCRSNode* Node,int Depth);
 
void LCRS_PrintNodesAtLevel(LCRSNode* Node,int Level); // 비타민퀴즈 
 
#endif
cs




2. LCRSTree.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
#include "LCRSTree.h"
 
LCRSNode* LCRS_CreateNode(ElementType NewData){
    LCRSNode* NewNode=(LCRSNode*)malloc(sizeof(LCRSNode));
 
    NewNode->Data=NewData;
    NewNode->LeftChild=NULL;
    NewNode->RightSibling=NULL;
 
    return NewNode;
}
 
void LCRS_DestroyNode(LCRSNode* Node){
    free(Node);
}
 
void LCRS_DestroyTree(LCRSNode* Root){
    if(Root->RightSibling!=NULL){
        LCRS_DestroyTree(Root->RightSibling);
    }
 
    if(Root->LeftChild!=NULL){
        LCRS_DestroyTree(Root->LeftChild);
    }
 
    Root->LeftChild=NULL;
    Root->RightSibling=NULL;
 
    LCRS_DestroyNode(Root);
}
 
void LCRS_AddChildNode(LCRSNode* ParentNode,LCRSNode* ChildNode){
    if(ParentNode->LeftChild==NULL){
        ParentNode->LeftChild=ChildNode;
    }
    else{
        LCRSNode* TempNode=ParentNode->LeftChild;
        while(TempNode->RightSibling!=NULL){
            TempNode=TempNode->RightSibling;
        }
        TempNode->RightSibling=ChildNode;
    }
}
 
void LCRS_PrintTree(LCRSNode* Node,int Depth){
 
    int i=0;
    for(i=0;i<Depth;i++){
        printf(" ");
    }
 
    printf("%c\n",Node->Data);
 
    if(Node->LeftChild!=NULL){
        LCRS_PrintTree(Node->LeftChild,Depth+1);
    }
    if(Node->RightSibling!=NULL){
        LCRS_PrintTree(Node->RightSibling,Depth);
    }
}
    
void LCRS_PrintNodesAtLevel(LCRSNode* Node,int Level){
    
    if(Level>0){
        if(Node->LeftChild!=NULL){
            LCRS_PrintNodesAtLevel(Node->LeftChild,Level-1);
        }
        if(Node->RightSibling!=NULL){
            LCRS_PrintNodesAtLevel(Node->RightSibling,Level);
        }
    }
    else{
        while(Node!=NULL){
            printf("%c\t",Node->Data);
            Node=Node->RightSibling;
        }
    }
 
}
 
 
 
cs




3. Test_LCRSTree.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
#include "LCRSTree.h"
 
int main(){
    LCRSNode* Root=LCRS_CreateNode('A');
 
    LCRSNode* B=LCRS_CreateNode('B');
    LCRSNode* C=LCRS_CreateNode('C');
    LCRSNode* D=LCRS_CreateNode('D');
    LCRSNode* E=LCRS_CreateNode('E');
    LCRSNode* F=LCRS_CreateNode('F');
    LCRSNode* G=LCRS_CreateNode('G');
    LCRSNode* H=LCRS_CreateNode('H');
    LCRSNode* I=LCRS_CreateNode('I');
    LCRSNode* J=LCRS_CreateNode('J');
    LCRSNode* K=LCRS_CreateNode('K');
 
    LCRS_AddChildNode(Root,B);
    LCRS_AddChildNode(B,C);
    LCRS_AddChildNode(B,D);
    LCRS_AddChildNode(D,E);
    LCRS_AddChildNode(D,F);
 
    LCRS_AddChildNode(Root,G);
    LCRS_AddChildNode(G,H);
 
    LCRS_AddChildNode(Root,I);
    LCRS_AddChildNode(I,J);
    LCRS_AddChildNode(J,K);
 
    
    
    LCRS_PrintTree(Root,0);
    printf("\n\n");
    LCRS_PrintNodesAtLevel(Root,2);
 
    LCRS_DestroyTree(Root);
 
 
    return 0;
    
}
cs



실행결과





반응형

'개인 서재..* > 알고리즘' 카테고리의 다른 글

[C언어] 수식 트리 계산  (0) 2017.04.24
[C언어] 이진트리 순회 표현  (0) 2017.04.24
[C언어] 링크드 큐  (0) 2017.04.23
[C언어] 스택(feat.링크드 리스트)  (0) 2017.04.20
[C언어] 스택(feat.배열)  (0) 2017.04.14
댓글