반응형
1. Binary_tree.h
- #ifndef _BINARY_TREE_
- #define _BINARY_TREE_
- typedef struct BTree
- {
- int data; //정수형 Data
- struct BTree* pLeft_Child; //좌측 자식
- struct BTree* pRight_Child; //우측 자식
- }BTNode;
- BTNode* Create_Binary_Tree(int root_Data); //루트 노드 생성
- BTNode* Insert_Left_Child(BTNode* pParent, int child_Data); //좌측 노드 생성
- BTNode* Insert_Right_Child(BTNode* pParent, int child_Data); //우측 노드 생성
- void Print_Binary_Tree(BTNode* pNode, int level); //트리 출력
- void Destroy_Node(BTNode* pNode); //노드 삭제
- void Destroy_Binary_Tree(BTNode* pRoot); //이진트리 삭제
- #endif _BINARY_TREE_
2. Binary_tree.c
- #include<stdio.h>
- #include<stdlib.h>
- #include"Binary_tree.h"
- //================================================
- //Create_Binary_Tree
- //입력 인자 : 최초 생성된 루트 노드의 데이터
- //반환 인자 : 생성된 노드의 구조체 포인터
- //-최초 루트노드 생성시 필요.
- //================================================
- BTNode* Create_Binary_Tree(int root_Data)
- {
- BTNode *pTree = NULL;
- if(pTree != NULL) //메모리 할당 제대로 되었다면
- {
- //최초 루트 노드 생성시 자식 노드 없다.
- pTree->pLeft_Child=NULL;
- pTree->pRight_Child=NULL;
- pTree->data=root_Data;
- }
- else
- {
- return NULL;
- }
- //생성된 루트 노드 반환.
- return pTree;
- }
- //================================================
- //Insert_Left_Child
- //입력 인자 : 부모 노드와 자식노드의 Data
- //반환 인자 : 생성된 노드의 구조체 포인터
- //-좌측 노드 생성시 필요
- //================================================
- BTNode* Insert_Left_Child(BTNode* pParent, int child_Data)
- {
- BTNode *pChild = NULL;
- if(pChild != NULL) //메모리 할당 성공
- {
- if(pParent->pLeft_Child == NULL) // 부모의 좌측 자식 노드가 없다면
- {
- pParent->pLeft_Child = pChild; //좌측 자식 노드에 노드 추가
- pChild->data = child_Data; //자식 노드에 Data
- pChild->pLeft_Child=NULL;
- pChild->pRight_Child=NULL;
- }
- else
- {
- return NULL;
- }
- }
- else
- {
- return NULL;
- }
- return pChild; //생성된 자식 노드 반환.
- }
- //================================================
- //Insert_Right_Child
- //입력 인자 : 부모 노드와 자식노드의 Data
- //반환 인자 : 생성된 노드의 구조체 포인터
- //-우측 노드 생성시 필요
- //================================================
- BTNode* Insert_Right_Child(BTNode* pParent, int child_Data)
- {
- BTNode *pChild = NULL;
- if(pChild != NULL) //메모리 할당 성공
- {
- if(pParent->pRight_Child == NULL) // 부모의 우측 자식 노드가 없다면
- {
- pParent->pRight_Child = pChild; //우측 자식 노드에 노드 추가
- pChild->data = child_Data; //자식 노드에 Data
- pChild->pLeft_Child=NULL;
- pChild->pRight_Child=NULL;
- }
- else
- {
- return NULL;
- }
- }
- else
- {
- return NULL;
- }
- return pChild; //생성된 자식 노드 반환.
- }
- //================================================
- //Print_Binary_Tree
- //입력 인자 : 루트노드와 레벨
- //반환 인자 : 없음
- //-트리 구조 전체 출력 // 재귀이용한 출력
- //================================================
- void Print_Binary_Tree(BTNode* pNode, int level)
- {
- int i=0;
- for(i=0; i<level; i++)
- if(pNode->pLeft_Child != NULL)
- Print_Binary_Tree(pNode->pLeft_Child, level+1);
- if(pNode->pRight_Child != NULL)
- Print_Binary_Tree(pNode->pRight_Child, level+1);
- }
- //================================================
- //Destroy_Binary_Tree
- //입력 인자 : 루트노드
- //반환 인자 : 없음
- //-트리 구조 전체 삭제 // 재귀이용한 출력
- //================================================
- void Destroy_Binary_Tree(BTNode* pRoot)
- {
- if(pRoot->pLeft_Child != NULL)
- Destroy_Binary_Tree(pRoot->pLeft_Child);
- if(pRoot->pRight_Child != NULL)
- Destroy_Binary_Tree(pRoot->pRight_Child);
- }
- void Destroy_Node(BTNode* pNode)
- {
- if(pNode->pLeft_Child==NULL && pNode->pRight_Child==NULL)
- }
3. 테스트 코드
- #include<stdio.h>
- #include<stdlib.h>
- #include"Binary_tree.h"
- int main()
- {
- BTNode* pRoot;
- BTNode* A= NULL;
- BTNode* B= NULL;
- BTNode* C= NULL;
- BTNode* D= NULL;
- pRoot=Create_Binary_Tree(0);
- A=Insert_Left_Child(pRoot, 1);
- B=Insert_Right_Child(pRoot, 2);
- C=Insert_Left_Child(A, 3);
- D=Insert_Right_Child(A, 4);
- Destroy_Binary_Tree(pRoot);
- return 0;
- }
반응형
'ETC > Data Struct | Algorithm' 카테고리의 다른 글
순차 검색 (Sequential Search) (0) | 2014.05.15 |
---|---|
이진트리 순회 (0) | 2013.05.01 |
스택 - 배열구조 Array_Stack) (0) | 2012.09.15 |
정렬 - 삽입 정렬 (0) | 2012.09.11 |
정렬 - 퀵 정렬(Quick Sort) (0) | 2012.09.10 |