7.17.2017

자료구조 Tree

// 'LCRSTREE.h'라는 헤더 파일을 만든다.

#pragma once
#ifndef LCRS_TREE_H
#define LCRS_TREE_H

#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;

typedef struct tagLCRSNode {
        struct tagLCRSNode * LeftChild;
        struct tagLCRSNode * RightSibling;

        ElementType Data;
}LCRSNode;

LCRSNode* LCRS_CreateNode(ElementType NewData);
void LCRS_DestroyNode(LCRSNodeNode);
void LCRS_DestroyTree(LCRSNode* Root);

void LCRS_AddChildNode(LCRSNode* ParentNodeLCRSNode* ChildNode);
void LCRS_PrintTree(LCRSNode* Node, int Depth);
void LCRS_PrintNodeAtLevel(LCRSNode* Rootint Level);


#endif

===================================================================================

#include "LCRSTree.h"

LCRSNode* LCRS_CreateNode(ElementType Data) {
        LCRSNode* NewNode = (LCRSNode*)malloc(sizeof(LCRSNode));
        NewNode->LeftChild = NULL;
        NewNode->RightSibling = NULL;
        NewNode->Data = NewData;
        return NewNode;
}

void LCRS_DestroyNode(LCRSNodeNode) {
        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* ParentNodeLCRSNode* 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* Nodeint Depth) {
        for (int i = 0; i < Depth; i++)
                printf("  ");
        printf("%c\n", Node->Data);

        if (Node->LeftChild != NULL)
LCRS_PrintTree(Node->LeftChild, Depth + 1);
        if (Node->LeftChild != NULL)
                LCRS_PrintTree(Node->RightSibling, Depth );
}

void LCRS_PrintNodeAtLevel(LCRSNode* Rootint Level){
        if (Level== 0)
        printf("%c\n"Root->Data);

        if (Root->LeftChild != NULL&& Level>0)
                LCRS_PrintNodeAtLevel(Root->LeftChild, Level-1);

        if (Root->RightSibling != NULL)
                LCRS_PrintNodeAtLevel(Root->RightSibling, Level);
}

void 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);

        //1~3레벨 노드 출력
        printf("print level1 Nodes\n");
        LCRS_PrintNodeAtLevel(Root, 1);
        printf("print level2 Nodes\n");
        LCRS_PrintNodeAtLevel(Root, 2);
        printf("print level3 Nodes\n");
        LCRS_PrintNodeAtLevel(Root, 3);

        //트리 소멸시키기
        LCRS_DestroyTree(Root);

        //트리 출력하기
        LCRS_PrintTree(Root, 0);
}

===================================================================================
//출력


댓글 없음:

댓글 쓰기