C语言:哈夫曼树的编码与译码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

#define N 100

typedef struct 
{
	int weight;
	int parent, lchild, rchild;
}hafuman;

typedef struct 
{
	char data[N]; //字符数据

	char copy[N][10*N];//编码
}bianma;

void display(); 
int input(int w[], bianma *bm);
void creat_hafuman(hafuman ht[], int w[], int n);
void select(hafuman ht[], int m, int *s1,  int *s2);
void encoding(hafuman ht[], bianma *bm, int n);
void coding(bianma *bm, int n);//译码
void coding_2(hafuman ht[], bianma *bm, int n);//译码
void output(bianma *bm, int n);
int i, j, k;

void display()
{
	printf("\n\n\n");
	printf("\t\t\t1.输出编码\n\n");
	printf("\t\t\t2.进行译码\n\n");
	printf("\t\t\t3.退出\n\n");
	printf("\t\t请选择(1~~3):  ");
}


int input(int w[], bianma *bm)
{
	int n=0;

	printf("\n请输入要进行编码的文章或句子(#结束)\n");

	while(1)
	{
		bm->data[n]=getchar();

		if(bm->data[n]=='#')
			break;
		n++;
	}

	for(i=0; i<n; i++)
	{
		w[i]=1;

		for(j=i+1; j<n; )
		{
			if( bm->data[i] == bm->data[j] )
			{
				w[i]++;

				for(k=j; k<n; k++)
				{ 
					bm->data[k]=bm->data[k+1];
				}	
				n--; //覆盖完之后n--;
			}
			else
				j++;
		}

	}

	printf("\n\n");	
	printf("不同的字符\n");
	for(i=0; i<n; i++)
	{
		printf("%c", bm->data[i]);
	}

	return n;
}
	

void creat_hafuman(hafuman ht[], int w[], int n)
{
	int s1, s2;
	int t;

	for(t=1; t<=n; t++)
	{
		ht[t].weight=w[t-1];
		ht[t].parent=0;
		ht[t].lchild=0;
		ht[t].rchild=0;
	}

	for(t=n+1; t<=2*n-1; t++)
	{
		ht[t].weight=0;
		ht[t].parent=0;
		ht[t].lchild=0;
		ht[t].rchild=0;
	}
	

	for(t=n+1; t<=2*n-1; t++)
	{
		select(ht, t-1, &s1, &s2);//前i-1中选双亲为0, 权值最小

		ht[t].weight=ht[s1].weight + ht[s2].weight;
	
		ht[t].lchild=s1, ht[t].rchild=s2;

		ht[s1].parent=t, ht[s2].parent=t;

	}
}

void select(hafuman ht[], int m, int *s1,  int *s2)
{
	int min1, min2, a, b;
	i=1;
	while( ht[i].parent != 0)
	{
		i++;
	}
	min1=ht[i].weight; 
	a=i;

	for(j=i+1; j<=m; j++)
	{
		if(min1 > ht[j].weight && ht[j].parent==0)
		{
			min1=ht[j].weight;
			a=j;
		}

	}


	i=1;
	while( ht[i].parent != 0 || a==i )
	{
			i++;
	}
	
	min2=ht[i].weight;
	b=i;

	for(j=i+1; j<=m; j++)
	{
		if(j==a)
			continue;

		if(min2 > ht[j].weight && ht[j].parent==0)
		{
			min2=ht[j].weight;
			b=j;
		}
	}
	*s1=a; *s2=b;

}

void encoding(hafuman ht[], bianma *bm, int n)//编码, *copy[]为复制编码
{
	int start, c, p;
	char *ch;
	ch=(char *)malloc( n*sizeof(char) );
	ch[n-1]='\0';

	for(i=1; i<=n; i++)//n个叶子节点
	{
		start=n-1;
	    c=i, p=ht[i].parent; //p为parent, c为child

		while(p!=0)
		{
			start--;
			if(ht[p].lchild==c)
				ch[start]='0';
			else
				ch[start]='1';

			c=p; p=ht[p].parent;	//printf("\n123\n");
		}

	    strcpy( bm->copy[i-1], &ch[start] );
		//printf("\n%s\n", bm->copy[i-1]);	
	}
	free(ch);

}

void coding_2(hafuman ht[], bianma *bm, int n)//译码
{
	char s[10*N];
	int p;

	printf("\n请输入要译码的字符\n");
	fflush(stdin);
	gets(s);
	
	printf("\n译码\n\n");
	p=2*n-1;
	for(i=0; s[i] != '\0'; i++)
	{	
		
		if(s[i]=='0')
			p=ht[p].lchild;

		else
			if(s[i]=='1')
			p=ht[p].rchild;	
			
		if(ht[p].lchild == 0 && ht[p].rchild == 0)
		{ 
				printf("%c", bm->data[p-1]);//p: 1~~2*n-1, bbm->data[0~~n-1]
				p=2*n-1;
				continue;
		}

	}
	puts("\n\n");
}


void output(bianma *bm, int n)
{
	printf("\n");
	for(i=0; i<n; i++)
	{	
		printf("%c\t", bm->data[i] );

		printf("%s\n", bm->copy[i]);
	}
}


void main()
{
	hafuman ht[N];
	bianma *bm;
	int w[N];
	int n, m;

	bm=(bianma *)malloc( sizeof(bianma) );
	n=input(w, bm);

	printf("\n\n不同字符总数: %d\n\n", n);

    creat_hafuman(ht, w, n); 
	
	encoding(ht, bm, n); 

	getch();
    system("cls");
	
loop:	display();
	scanf("%d",  &m);
	switch(m)
	{
	case 1:   	      
		output(bm, n);
		printf("\n\n请按任意键继续");
		getch();
		system("cls");
		goto loop;
		break;
	case 2:
		//coding(bm, n);//译码	
		coding_2(ht, bm, n);//译码
		printf("\n\n请按任意键继续");
		getch();
		system("cls");
		goto loop;
		break;
	case 3:
		break;
	default:
		system("cls");  
		goto loop;
	}
}





 



  • 10
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
哈夫曼编码是一种基于二叉树的编码方式,可以实现无损压缩数据的功能。下面是C语言实现哈夫曼树编码译码的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 哈夫曼树的节点结构体 typedef struct huffman_node { char data; // 数据 int freq; // 频率 struct huffman_node *left; // 左子节点 struct huffman_node *right; // 右子节点 } huffman_node_t; // 哈夫曼编码表的结构体 typedef struct huffman_table { char data; // 数据 char code[256]; // 编码 } huffman_table_t; // 构建哈夫曼树 huffman_node_t* build_huffman_tree(char* data, int* freq, int n) { huffman_node_t **nodes = (huffman_node_t**)malloc(n * sizeof(huffman_node_t*)); for (int i = 0; i < n; i++) { huffman_node_t *node = (huffman_node_t*)malloc(sizeof(huffman_node_t)); node->data = data[i]; node->freq = freq[i]; node->left = NULL; node->right = NULL; nodes[i] = node; } while (n > 1) { // 找出频率最小的两个节点 int min1 = 0, min2 = 1; if (nodes[0]->freq > nodes[1]->freq) { min1 = 1; min2 = 0; } for (int i = 2; i < n; i++) { if (nodes[i]->freq < nodes[min1]->freq) { min2 = min1; min1 = i; } else if (nodes[i]->freq < nodes[min2]->freq) { min2 = i; } } // 合并频率最小的两个节点 huffman_node_t *parent = (huffman_node_t*)malloc(sizeof(huffman_node_t)); parent->data = 0; parent->freq = nodes[min1]->freq + nodes[min2]->freq; parent->left = nodes[min1]; parent->right = nodes[min2]; if (min1 < min2) { nodes[min1] = parent; nodes[min2] = nodes[n - 1]; } else { nodes[min2] = parent; nodes[min1] = nodes[n - 1]; } n--; } return nodes[0]; } // 生成哈夫曼编码表 void generate_huffman_table(huffman_node_t *root, char* code, int depth, huffman_table_t* table) { if (root->left == NULL && root->right == NULL) { table[root->data].data = root->data; strcpy(table[root->data].code, code); return; } code[depth] = '0'; generate_huffman_table(root->left, code, depth + 1, table); code[depth] = '1'; generate_huffman_table(root->right, code, depth + 1, table); } // 哈夫曼编码 char* huffman_encode(char* data, huffman_table_t* table) { int n = strlen(data); char* code = (char*)malloc(n * 256 * sizeof(char)); int k = 0; for (int i = 0; i < n; i++) { strcat(code, table[data[i]].code); } return code; } // 哈夫曼解码 char* huffman_decode(char* code, huffman_node_t* root) { int n = strlen(code); char* data = (char*)malloc(n * sizeof(char)); huffman_node_t* p = root; int k = 0; for (int i = 0; i < n; i++) { if (code[i] == '0') { p = p->left; } else { p = p->right; } if (p->left == NULL && p->right == NULL) { data[k++] = p->data; p = root; } } return data; } int main() { char data[] = "ABCDAAAABBBCDEFG"; int freq[] = {3, 4, 3, 2, 2, 2, 1, 2, 1, 1}; int n = strlen(data); // 构建哈夫曼树 huffman_node_t* root = build_huffman_tree(data, freq, n); // 生成哈夫曼编码表 char code[256]; memset(code, 0, sizeof(code)); huffman_table_t table[256]; generate_huffman_table(root, code, 0, table); // 哈夫曼编码 char* encoded = huffman_encode(data, table); // 哈夫曼解码 char* decoded = huffman_decode(encoded, root); printf("原始数据:%s\n", data); printf("哈夫曼编码:%s\n", encoded); printf("哈夫曼解码:%s\n", decoded); return 0; } ``` 以上代码实现了哈夫曼树的构建、哈夫曼编码表的生成、哈夫曼编码和哈夫曼解码的功能。在实际应用中,需要根据具体的需求对代码进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值