컴공 일기157
게시글 주소: https://orbi.kr/00058025549

비선형 자료구조 Tree 구현
-LCRSTree.h
#include <stdio.h>
#include <stdlib.h>
typedef struct tagLCRSNode
{
struct tagLCRSNode* LeftChild;
struct tagLCRSNode* RightSibling;
int Data;
}LCRSNode;
LCRSNode* LCRS_CreateNode(int 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_PrintNodeAtLevel(LCRSNode* Root, int Level);
-LCRSTree.c
#include "LCRSTree.h"
LCRSNode* LCRS_CreateNode(int NewData)
{
LCRSNode* NewNode = (LCRSNode*)malloc(sizeof(LCRSNode));
NewNode->LeftChild = NULL;
NewNode->RightSibling = NULL;
NewNode->Data = NewData;
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->RightSibling = NULL;
Root->LeftChild = NULL;
LCRS_DestroyNode(Root);
}
void LCRS_AddChildNode(LCRSNode* Parent, LCRSNode* Child)
{
if (Parent->LeftChild == NULL)
{
Parent->LeftChild = Child;
}
else {
LCRSNode* TempNode = Parent->LeftChild;
while (TempNode->RightSibling != NULL)
TempNode = TempNode->RightSibling;
TempNode->RightSibling = Child;
}
}
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_PrintNodeAtLevel(LCRSNode* Root, int 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);
}
-main.c
#include "LCRSTree.h"
int main(void)
{
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);
LCRS_PrintNodeAtLevel(Root, 1);
LCRS_PrintNodeAtLevel(Root, 2);
LCRS_PrintNodeAtLevel(Root, 3);
LCRS_DestroyTree(Root);
return 0;
}
0 XDK (+0)
유익한 글을 읽었다면 작성자에게 XDK를 선물하세요.
c도 java처럼 다른 class를 불러올 수 있나요?
저는 한 class에 전부 작성했었는데
다른 class를 불러온다는 것이 어떤 의미인가요? 라이브러리 말씀하시는 건가요?
프로그래머의 관점에 따라서 여러 클래스가 나올 수 있죠. 하지만, 모든 것이 그렇듯 '정석'은 존재하는 것으로 알고 있어요. 주로, 객체지향에서는 GUI 부분 / 제어 부분 / 데이터 부분으로 나누어서 만들죠.
그래야, 유지/보수도 편리하기 때문이지요!
그렇죠, 동의합니다
#include "LCRSTree.h"
다른 class를 main class에 불러오는 부분이 java랑 비슷하다고 생각했어요 ㅎㅎ
좋은 내용 감사합니다!
아아 이건 C++이 아니라 절차지향언어에 해당하는 C로 작성된 코드인데, 주로 얘는 클래스라고 부르진 않고, 헤더 파일이라고 명명합니다.