카테고리 없음
트리 with LCRS
4gats
2023. 4. 10. 22:00
트리의 기본 연산
Left Child - Right Sibling (LCRS)
1
2
3
4
5
6
7
8
|
typedef struct tagLCRSNode
{
struct tagLCRSNode* LeftChild;
struct tagLCRSNode* RightSibling;
char Data;
} LCRSNode;
|
cs |
자식 노드 연결
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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;
}
}
|
cs |
트리 출력
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
void LCRS_PrintTree( LCRSNode* Node, int Depth )
{
// 들여쓰기
int i=0;
for ( i=0; i<Depth-1; i++ )
printf(" "); // 공백 3칸
if (Depth > 0) // 자식 노드 여부 표시
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);
}
|
cs |