Huffman编码的c语言实现

huffman.h文件
#include
#include
#include
struct huff_node
{
	int id;
	int appear_count;
	huff_node *leftnext;
	huff_node *parent;
	huff_node *rightnext;
};
void initFile();
void count_probability(int *word_count);
huff_node *huffman_tree(int *word_count);
void encoded(char code[26][32],huff_node *head_node);
void huffman_main();


huffman.cpp文件

#include"huffman.h"
void initFile()
{
	FILE *file_p = fopen("sample_input.txt","w");
	for(int i=0;i<100000;i++)
		fputc(rand()%26+65,file_p);
	fclose(file_p);
}
void count_probability(int *word_count)
{
	int eitm=0;
	for(int i=0;i<26;i++)
		word_count[i]=0;
	FILE *file_p = fopen("sample_input.txt","r");
	while((eitm=fgetc(file_p))>0)
	{
		word_count[(eitm-65)%26]++;
	}
	fclose(file_p);
}
huff_node *huffman_tree(int *word_count)
{
	int first_min=0,sec_min=0,ctrct_cnt=0;
	huff_node *huff_arr[26],*temp_node;
	memset(huff_arr,NULL,26*sizeof(huff_node *));
	for (int i=0;i<26;i++)
	{
		if (word_count[i]>0)
		{
			huff_arr[i] =(huff_node *)malloc(sizeof(huff_node));
			huff_arr[i]->appear_count=word_count[i];
			huff_arr[i]->id=i;
			huff_arr[i]->leftnext=NULL;
			huff_arr[i]->rightnext=NULL;
		}
	}
	while (true)
	{
		ctrct_cnt=0;
		for (int i=0;i<26;i++)
		{
			if (huff_arr[i]!=NULL)
			{
				ctrct_cnt++;
			}
		}
		if (ctrct_cnt>1)
		{
			first_min=0,sec_min=0;
			for (int i=0;i<26;i++)
			{
				if (huff_arr[i]!=NULL)
				{
					if (huff_arr[first_min]==NULL)
					{
						first_min=i;
					}
					if (huff_arr[first_min]->appear_count>=huff_arr[i]->appear_count)
					{
						sec_min=first_min;
						first_min=i;
					}
					if (first_min==sec_min)
					{
						sec_min=i;
					}
				}
			}
			temp_node = (huff_node *)malloc(sizeof(huff_node));
			temp_node->appear_count=huff_arr[first_min]->appear_count+huff_arr[sec_min]->appear_count;
			temp_node->id=-1;
			temp_node->leftnext=huff_arr[sec_min];
			huff_arr[sec_min]->parent = temp_node;
			temp_node->rightnext=huff_arr[first_min];
			huff_arr[first_min]->parent=temp_node;
			huff_arr[sec_min]=0;
			ctrct_cnt++;
			huff_arr[first_min]=temp_node;
		}
		else
			break;
	}
	for (int i=0;i<26;i++)
	{
		if (huff_arr[i]!=NULL)
		{
			return huff_arr[i];
		}
	}
	return NULL;
}
void encoded(char code[26][32],huff_node *head_node)
{
	memset(code,'\0',26*32*sizeof(char));
	char trace[32];
	int fromend=0,trace_cursor =0,huff_cursor =0,access_left=0;
	huff_node *huff_stack[32]={0};
	huff_node *temp_node=head_node;
	huff_stack[huff_cursor] = head_node;
	memset(trace,'\0',32*sizeof(char));
	do
	{
		if (access_left==0&&huff_stack[huff_cursor]->leftnext!=NULL)
		{
			huff_stack[++huff_cursor] = huff_stack[huff_cursor-1]->leftnext;
			trace[trace_cursor++] ='1';
		}
		else if(huff_stack[huff_cursor]->rightnext!=NULL)
		{
			access_left =0;
			huff_stack[++huff_cursor] = huff_stack[huff_cursor-1]->rightnext;
			trace[trace_cursor++] ='0';
		}
		else if(huff_stack[huff_cursor]->rightnext==NULL&&huff_stack[huff_cursor]->leftnext==NULL)
		{
			for(int i=0;trace[i]!='\0';i++)
				code[huff_stack[huff_cursor]->id][i]=trace[i];
			while(huff_cursor>-1)
			{
				if((huff_cursor-1)>=0&&huff_stack[huff_cursor-1]->leftnext==huff_stack[huff_cursor])
				{
					huff_stack[huff_cursor--]=NULL;
					trace[--trace_cursor]='\0';
					access_left =1;
					break;
				}
				else
				{
					if(trace_cursor>=1)
						trace[--trace_cursor]='\0';
					huff_stack[huff_cursor--]=NULL;
				}
			}
		}
	}while (huff_cursor>-1);
}
void prt_huff_tree(huff_node *head_node)
{
	if(head_node!=NULL)
	{
		prt_huff_tree(head_node->leftnext);
		prt_huff_tree(head_node->rightnext);
		printf("%d ",head_node->id);		
	}
	else
		return;
}
void huffman_main()
{
	huff_node * headnode=0;
	int word_count[26]={0};
	char code[26][32];
	initFile();
	count_probability(word_count);
	headnode = huffman_tree(word_count);
	encoded(code,headnode);
	for (int i=0;i<26;i++)
	{
		if(code[i][0]!='\0')
			printf("%c %s\n",65+i,code[i]);
	}
	prt_huff_tree(headnode);
}



你可能感兴趣的:(数据结构与算法,c语言,Huffman)