#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//输入字符频度的最大字符数
#define MaxSize 51
//待编码语句的最大长度
#define MaxWordSize 201
typedef struct{
unsigned int weight;
unsigned int lchild, rchild, parent;
}Hnode, *Htree;
void Create_Htree(Htree &HT, int &n, char ch[]);
void Encode_Htree(Htree HT, char** &HC, int n, char ch[]);
int Decode(char** HC, Htree HT, char ch[], int n);
int get_conf(char ch[], int frequency[]);
void Display_Htree(Htree HT, int end);
void find_min_node(Htree HT,int &min1,int &min2,int end);
void Encode(char** HC, char ch[], int n);
int main()
{
int operate_num, n;
Htree HT;
char ch[MaxSize];
char **HC;
int flag = 0;
printf("[1]创建哈夫曼树 [2]编码 [3]译码\n[4]显示哈夫曼树 [0]退出\n请输入操作:");
scanf("%d", &operate_num);
while(operate_num)
{
if(operate_num == 1)
{
Create_Htree(HT, n, ch);
Encode_Htree(HT, HC, n, ch);
flag = 1;
}
else if(operate_num == 2)
{
if(flag == 1)
{
//Encode_Htree(HT, HC, n, ch);
Encode(HC, ch, n);
flag = 1;
}
else
printf("请先建立哈夫曼树!\n");
}
else if(operate_num == 3)
{
int code_not_exist = 0;
if(flag == 1)
{
code_not_exist = Decode(HC, HT, ch, n);
if(code_not_exist)
printf("缺少待译码文件请先编码!\n");
}
else
printf("请先建立哈夫曼树!\n");
}
else if(operate_num == 4)
if(flag == 1)
Display_Htree(HT, 2 * n - 1);
else
printf("请先建立哈夫曼树!\n");
else if(operate_num == 0)
break;
else
printf("输入有误请重新输入\n");
printf("[1]创建哈夫曼树 [2]编码 [3]译码\n[4]显示哈夫曼树 [0]退出\n请输入操作:");
scanf("%d", &operate_num);
}
printf("已退出");
system("pause");
return 0;
}
//创建哈夫曼树
void Create_Htree(Htree &HT, int &n, char ch[])
{
int frequency[MaxSize];
int m;
//读入字符权重的个数
n = get_conf(ch, frequency);
if(!n)
return;
m = 2 * n - 1;
//第0个结点不存内容
HT = (Htree)malloc((m + 1) * sizeof(Hnode));
Htree ptr;
ptr = HT;
int i;
//初始化n个叶子结点
for(i = 1; i <= n; i++) { (ptr + i)->weight = frequency[i - 1];
(ptr + i)->parent = 0;
(ptr + i)->lchild = 0;
(ptr + i)->rchild = 0;
}
//初始化除叶子结点外的结点
for(i = n + 1; i <= m; i++) { (ptr + i)->weight = 0;
(ptr + i)->parent = 0;
(ptr + i)->lchild = 0;
(ptr + i)->rchild = 0;
}
for(i = n + 1; i <= 0 m; i++) { 找到最小的两个叶子结点,并且min1要小于min2 int min1, min2; find_min_node(ht, min2, i - 1); ht[i].weight="HT[min1].weight" + ht[min2].weight; ht[min1].parent="HT[min2].parent" = i; ht[i].lchild="min1;" ht[i].rchild="min2;" } printf("哈夫曼树已经创建\n"); 从文件中读取字符频度 get_conf(char ch[], frequency[]) file *fp="fopen("Conf.txt"," "r"); fgets(ch, maxsize, fp); for(i="0;" fscanf(fp, "%d", &frequency[i]) !="EOF;" i++); fclose(fp); return 创建哈夫曼编码表 void encode_htree(htree ht, char** &hc, n, char ch[]) hc="(char**)malloc(n" * sizeof(char*)); *temp="(char*)malloc(n" sizeof(char)); temp[n 1]="\0" ; i, j, j_parent, start; <="n;" 从第一个结点开始向上求哈夫曼编码 j="i;" j_parent="HT[j].parent;" 因为是从叶子结点向根节点,所以要倒序的存入权重 start="n" 1; while(j_parent) if(j="=" ht[j_parent].lchild) temp[--start]="0" else ht[j_parent].rchild) hc[i start) strcpy(hc[i 1], &temp[start]); temp="NULL;" free(temp); 对文本进行哈夫曼编码 encode(char** hc, n) *fp1="fopen("ToBeTran.txt"," word[maxwordsize]; fgets(word, maxwordsize, fp1); fclose(fp1); length="strlen(word);" 给数组初始化初值,防止拼接的时候出现错误 encoder[maxwordsize 4]="{0};" j; for(j="0;" j++) if(word[i]="=" ch[j]) strcat(encoder, hc[j]); *fp2="fopen("CodeFile.txt"," "w"); fputs(encoder, fp2); fclose(fp2); printf("编码为:%s\n", encoder); 用哈夫曼树译码 decode(char** htree if(!fp1) code[maxwordsize fscanf(fp1, "%s", code); n k="0;" decoder[maxwordsize]="{0};" 从根结点开始向下,0则进左孩子,1则进右孩子 一直到叶子结点输出 if(code[i]="=" '0') '1') if(ht[j].lchild="=" && ht[j].rchild="=" 0) decoder[k]="ch[j" 1]; k++; printf("译码为:%s\n", decoder); 0; 在终端上显示创建的哈夫曼树 display_htree(htree end) printf("序号\t权值\t左孩子\t右孩子\t双亲\n"); printf("%d\t%d\t%d\t%d\t%d\n", ht[i].weight, ht[i].lchild, ht[i].rchild, ht[i].parent); 找到树中权值最小的两个结点 find_min_node(htree ht,int &min1,int &min2,int 先找min1 if(!ht[i].parent) min1="i;" break; for(; if(!ht[i].parent ht[min1].weight> HT[i].weight)
min1 = i;
}
//再找min2
for(i = 1; i <= end; i++) { 并且要保证不能和min1重复 if(!ht[i].parent && i !="min1)" min2="i;" break; } for(i="1;" <="end;" ht[min2].weight> HT[i].weight && i != min1)
min2 = i;
}
}</=></=></=></=></stdlib.h></string.h></stdio.h>
Original: https://www.cnblogs.com/chr1ce/p/16536923.html
Author: chris599
Title: C语言建立哈夫曼树编码译码
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/712614/
转载文章受原作者版权保护。转载请注明原作者出处!