Soft Ware/자료구조 및 알고리즘

- 트리 - LCRS 원하는 높이 부분만 출력

달려가보자 2012. 4. 17. 02:17

#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;

typedef struct  LCRSNode
{
 struct LCRSNode * LeftChild;
 struct LCRSNode * RightSibling;

 ElementType Data;
}LCRSNode;

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

void LCRS_AddChildNode( LCRSNode * Parent, LCRSNode * Child);
void LCRS_PrintTree( LCRSNode * Node, int Depth );

void LCRS_PrintNodesAtLevel(LCRSNode * Node, int Depth,int Level);

int main()
{
 int level = 0;

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

 printf("How Level : ");
 scanf("%d",&level );

 LCRS_PrintNodesAtLevel( Root,0,level);

 LCRS_DestroyTree( Root );

 return 0;
}

LCRSNode* LCRS_CreateNode(ElementType NewData)
{
 LCRSNode * NewNode = NULL;

 NewNode = (LCRSNode *)calloc(1, 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 * Parent, LCRSNode * Child)
{
 LCRSNode * Temp = NULL;

 if( Parent->LeftChild == NULL) Parent->LeftChild = Child;

 else
 {
  Temp = Parent->LeftChild;
  while(Temp->RightSibling != NULL) Temp = Temp->RightSibling;
  Temp->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_PrintNodesAtLevel(LCRSNode * Node, int Depth,int Level)
{
 
 int i =0;

 for( i =0; i<Depth; i++);

 if(Depth == Level) printf("%3c",Node->Data);
 
 if(Node->LeftChild != NULL) LCRS_PrintNodesAtLevel(Node->LeftChild, Depth+1,Level);

 if(Node->RightSibling != NULL) LCRS_PrintNodesAtLevel(Node->RightSibling, Depth,Level);
}

'Soft Ware > 자료구조 및 알고리즘' 카테고리의 다른 글

합병 정렬  (0) 2013.04.10
트리에서 전위순회, 중위순회, 하위순회  (0) 2012.04.17
세이커 정렬입니다  (0) 2012.04.16
버블 정렬입니다  (0) 2012.04.16
정렬 !! -선택정렬-  (0) 2012.04.16