#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENT 100
typedef struct TreeNode {
int weight;
struct TreeNode *left_child;
struct TreeNode *right_child;
} TreeNode;
typedef struct {
TreeNode *ptree;
int key;
} element;
typedef struct {
element heap[MAX_ELEMENT];
int heap_size;
} HeapType;
void init(HeapType *h) // 초기화 함수
{
h->heap_size = 0;
}
void insert_min_heap(HeapType *h, element item) // 삽입 함수
{
int i;
i = ++(h->heap_size);
while((i != 1) && (item.key < h->heap[i/2].key)) {
h->heap[i] = h->heap[i/2];
i /= 2;
}
h->heap[i] = item;
}
element delete_min_heap(HeapType *h) // 삭제 함수
{
int parent, child;
element item, temp;
item = h->heap[1];
temp = h->heap[(h->heap_size)--];
parent = 1;
child = 2;
while( child <= h->heap_size) {
if( ( child < h->heap_size) &&
(h->heap[child].key) > h->heap[child+1].key)
child++;
if( temp.key <= h->heap[child].key ) break;
h->heap[parent] = h->heap[child];
parent = child;
child *= 2;
}
h->heap[parent] = temp;
return item;
}
TreeNode *make_tree(TreeNode *left, TreeNode *right) // 이진 트리 생성 함수
{
TreeNode *node=
(TreeNode *)malloc(sizeof(TreeNode));
if( node == NULL) {
fprintf(stderr, "메모리 에러 \n");
exit(1);
}
node->left_child = left;
node->right_child = right;
return node;
}
void destroy_tree(TreeNode *root) // 이진 트리 제거 함수
{
if( root == NULL) return;
destroy_tree(root->left_child);
destroy_tree(root->right_child);
free(root);
}
TreeNode * huffman_tree(int freq[], int n) // 허프만 코드 생성 함수 // 원래 코드에선 void형 함수였음
{
int i;
TreeNode *node, *x;
HeapType heap;
element e, e1, e2;
init(&heap);
for(i=0;i<n;i++) {
node = make_tree(NULL, NULL);
e.key = node->weight = freq[i];
e.ptree = node;
insert_min_heap(&heap, e);
}
for(i=1;i<n;i++) { // 최솟값을 가지는 두 개의 노드를 삭제
e1 = delete_min_heap(&heap);
e2 = delete_min_heap(&heap);
x = make_tree(e1.ptree, e2.ptree); // 두 개의 노드를 합
e.key = x->weight = e1.key + e2.key;
e.ptree = x;
insert_min_heap(&heap, e);
}
e = delete_min_heap(&heap); // 최종 트리
return e.ptree; // 원래 함수는 void 형이었음, 원래 없던 코드
//destroy_tree(e.ptree); // 원래는 주석 처리 되지 않은 실행 코드
}
void print_code_table_recur(TreeNode *root, char *code, char *cur) // 처음 소스 코드엔 없던 함수, 새로 추가함
{
if(root == NULL)
return;
else if(root->left_child == NULL && root->right_child == NULL)
{
*cur = '\0';
printf("%c\t%s\n", root, code);
}
else
{
*cur = '0';
print_code_table_recur(root->left_child, code, cur+1);
// 왼쪽으로 감
*cur = '1';
print_code_table_recur(root->right_child, code, cur+1);
// 오른쪽으로 감
}
}
void print_code_table(TreeNode *root) // 처음 소스 코드엔 없던 함수, 새로 추가함
{
char code_buf[100];
print_code_table_recur(root, code_buf, code_buf);
}
void main()
{
int freq[] = {15, 12, 8, 6, 4};
TreeNode * h;
h=huffman_tree(freq, 5);
print_code_table(h);
}
위의 프로그램에서 생성된 허프만 코드에서 허프만 코드를 얻고 그것을 출력하려고 하는 코드를 추가하려고 합니다
기본 소스 코드에다가 검색 및 수정 좀 해서 짜봤는데 쓰레기 값이 출력됩니다
TreeNode * h; // 원래 없던 코드
print_code_table(h); // 원래 없던 코드
h=huffman_tree(freq, 5); 에서 h= // h= 만 원래 없던 코드
TreeNode * huffman_tree(int freq[], int n) 에서 TreeNode * 는 원래 void // 원래 void 형 함수였음
위의 부분을 수정하기 전 코드가 원래 코드인데, 원래 코드에서 생성된 허프만 코드에서 허프만 코드를 얻은 후 출력하는 것을 알고 싶습니다
그리고 수정된 코드에서도 가능하다면 뭐가 잘못됐는지 알고 싶습니다
http://dblack.tk
커뮤니티 사이트 입니다 많은 이용 부탁 드립니다.