#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
struct Node *Left;
struct Node *Right;
char Data;
}Node;
Node * MakeTree(char Data);
void deleteTree(Node * Data);
void DestroyTree(Node * Data);
void ConnectTree(Node * Data);
void PreorderPrint(Node *Data);
void InorderPirnt(Node *Data);
void PostPrint(Node *Data);
int main()
{
Node * A = MakeTree('A');
Node * B = MakeTree('B');
Node * C = MakeTree('C');
Node * D = MakeTree('D');
Node * E = MakeTree('E');
Node * F = MakeTree('F');
Node * G = MakeTree('G');
ConnectTree(A,B);
ConnectTree(B,C);
ConnectTree(B,D);
ConnectTree(A,E);
ConnectTree(E,F);
ConnectTree(E,G);
PreorderPrint(A);
printf("\n");
InorderPirnt(A);
printf("\n");
PostPrint(A);
return 0;
}
Node * MakeTree(char Data)
{
Node * TreeData = NULL;
TreeData = (Node *)calloc(1,sizeof(Node));
TreeData->Data = Data;
TreeData->Left = NULL;
TreeData->Right = NULL;
return TreeData;
}
void deleteTree(Node * Data)
{
free(Data);
}
void DestroyTree(Node *Data)
{
if( Data == NULL) return ;
if( Data->Left != NULL ) DestroyTree(Data->Left);
if( Data->Right != NULL ) DestroyTree(Data->Right);
if( Data != NULL ) deleteTree(Data);
}
void ConnectTree(Node * Head,Node * Data)
{
if( Head->Left == NULL) Head->Left = Data;
else Head->Right = Data;
}
void PreorderPrint(Node *Data) // 전위 순회 출력
{
if(Data == NULL) return ;
printf("%2c",Data->Data);
if(Data->Left != NULL) PreorderPrint(Data->Left);
if(Data->Right != NULL) PreorderPrint(Data->Right);
}
void InorderPirnt(Node *Data) // 중위 순회 출력
{
if(Data == NULL) return ;
if(Data->Left != NULL) InorderPirnt(Data->Left);
printf("%2c",Data->Data);
if(Data->Right != NULL) InorderPirnt(Data->Right);
}
void PostPrint(Node *Data) // 하위 순회 출력
{
if(Data == NULL) return ;
if(Data->Left != NULL) PostPrint(Data->Left);
if(Data->Right != NULL) PostPrint(Data->Right);
printf("%2c",Data->Data);
}
'Soft Ware > 자료구조 및 알고리즘' 카테고리의 다른 글
합병 정렬 (0) | 2013.04.10 |
---|---|
- 트리 - LCRS 원하는 높이 부분만 출력 (0) | 2012.04.17 |
세이커 정렬입니다 (0) | 2012.04.16 |
버블 정렬입니다 (0) | 2012.04.16 |
정렬 !! -선택정렬- (0) | 2012.04.16 |