哈夫曼树的简单操作-构造哈夫曼树与哈夫曼编码(C语言)


哈夫曼树的概念:
给定N个权值作为N个叶子结点,构造一棵二叉树,若该树的带权路径长度达到最小,称为哈夫曼树(Huffman Tree),也称这样的二叉树为最优二叉树。哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。

哈夫曼树的结构定义

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
typedef struct HTNode {
	int weight;
	int parent;
	int lch, rch;
}HTNode;
typedef struct HTNode* HuffmanTree;
typedef char** HuffmanCode;

构造哈夫曼树

/* 在HT[k](1<=k<=i-1)中选择两个双亲域为 0 ,且权值最小的
结点,返回该两个结点在HT的序号*/
void Select(HuffmanTree HT, int length, int* e1, int* e2)
{
	int minweight1, minweight2;
	minweight1 = minweight2 = MAX; //MAX为大于所有weight的一个值
	int index1, index2;     //用来表示下标
	index1 = index2 = 0;
	for (int i = 1; i < length + 1; i++)
	{
		if (HT[i].parent == 0)
		{
			if (HT[i].weight < minweight1) //有比原来最小的权出现
			{
				//原来最小的权变为第二小的权
				minweight2 = minweight1;   
				index2 = index1;
				//新的最小的权
				minweight1 = HT[i].weight;
				index1 = i;
			}
			else if (HT[i].weight < minweight2)  //出现了第二小的权
			{
				minweight2 = HT[i].weight;
				index2 = i;
			}
		}
	}
	*e1 = index1;
	*e2 = index2;
}
/* 构造哈夫曼树 */
void CreateHuffmanTree(HuffmanTree* HT, int n)
{
	if (n <= 1) return;
	//数组有 2n-1 个元素
	int m = 2 * n - 1; 

	//0号位置不存元素
	*HT = (HuffmanTree)malloc((m + 1)*sizeof(HTNode));
	
	//初始化
	for (int i = 1; i < m + 1; i++)
	{
		(*HT)[i].lch = (*HT)[i].rch = 0;
		(*HT)[i].parent = 0;
	}
	for (int i = 1; i <  n + 1; i++)
		scanf("%d", &(*HT)[i].weight);

	//表示权值第一小和第二小的下标
	int minweightindex1, minweightindex2;

	for (int i = n + 1; i < m + 1; i++)
	{
		Select(*HT, i - 1, &minweightindex1, &minweightindex2);
		
		//合并,产生新的HT[i]
		(*HT)[minweightindex1].parent = (*HT)[minweightindex2].parent = i;
		(*HT)[i].lch = minweightindex1;
		(*HT)[i].rch = minweightindex2;
		(*HT)[i].weight = (*HT)[minweightindex1].weight + (*HT)[minweightindex2].weight;

	}
}

哈夫曼编码的算法实现

/* 从叶子到根逆向求每个字符的哈夫曼编码,存储在编码表HC中 */
void CreateHuffmanCode(HuffmanTree HT, HuffmanCode*HC, int n)
{
	 (*HC) = (char**)malloc((n + 1) * sizeof(char*)); //与哈夫曼树一样,0号位置不存元素
	char* cd = (char*)malloc(n * sizeof(char));//该数组用0号位置
	cd[n - 1] = '\0';

	for (int i = 1; i < n + 1; i++)
	{
		int f = HT[i].parent; //用于向上回溯
		int current = i;    //当前结点
		int start = n - 1;  //数组中最后一个位置
		while (f != 0)
		{
			
			if (current == HT[f].lch) //如果当前结点是双亲的左孩子
				cd[--start] = '0';
			else                      //如果当前结点是双亲的右孩子
				cd[--start] = '1';   
			//继续回溯
			current = f;
			f = HT[f].parent;
		}
		//(*HC)[i]的长度为末位置-初位置+1 : (n - 1) - start + 1 = n - start
		(*HC)[i] = (char*)malloc((n - start) * sizeof(char));
		strcpy((*HC)[i], &cd[start]); //将求得的编码从临时空间cd拷贝到HC当前行中
	}                                 //拷贝字符串,'\0'是停止拷贝,'\0'也将拷贝
	free(cd);  //释放
}
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
哈夫曼树是一种重要的数据结构,它常常被用来进行数据压缩和编码。下面是哈夫曼树构造及字符编码C语言实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NODE_NUM 1000 // 哈夫曼树节点最大数目 #define MAX_CODE_LEN 1000 // 哈夫曼编码最大长度 // 哈夫曼树节点结构体 typedef struct { int weight; // 权重 int parent, lchild, rchild; // 父亲节点、左儿子节点、右儿子节点 } HTNode; // 哈夫曼编码结构体 typedef struct { char ch; // 字符 char code[MAX_CODE_LEN]; // 编码 } HCode; // 建立哈夫曼树,并返回根节点的下标 int CreateHT(HTNode ht[], int n) { int i, j, k, x1, x2, min1, min2; for (i = 0; i < 2 * n - 1; i++) { ht[i].parent = ht[i].lchild = ht[i].rchild = -1; } for (i = n; i < 2 * n - 1; i++) { min1 = min2 = 0x7fffffff; // 设置最大值 x1 = x2 = -1; for (j = 0; j < i; j++) { if (ht[j].parent == -1) { if (ht[j].weight < min1) { min2 = min1; x2 = x1; min1 = ht[j].weight; x1 = j; } else if (ht[j].weight < min2) { min2 = ht[j].weight; x2 = j; } } } ht[x1].parent = i; ht[x2].parent = i; ht[i].lchild = x1; ht[i].rchild = x2; ht[i].weight = ht[x1].weight + ht[x2].weight; } return 2 * n - 2; } // 生成哈夫曼编码 void CreateHCode(HTNode ht[], HCode hcd[], int n) { char cd[MAX_CODE_LEN]; int i, j, c, p; for (i = 0; i < n; i++) { hcd[i].ch = i + 'a'; // 假设字符集为26个小写字母 cd[n - 1] = '\0'; c = i; while (ht[c].parent != -1) { p = ht[c].parent; if (ht[p].lchild == c) { cd[strlen(cd) - 1] = '0'; } else { cd[strlen(cd) - 1] = '1'; } c = p; strcat(cd, " "); } strcpy(hcd[i].code, strrev(cd)); } } int main() { int n, i, root; HTNode ht[MAX_NODE_NUM]; HCode hcd[MAX_NODE_NUM]; printf("请输入字符集的大小:"); scanf("%d", &n); printf("请输入各个字符的权重:"); for (i = 0; i < n; i++) { scanf("%d", &ht[i].weight); } root = CreateHT(ht, n); CreateHCode(ht, hcd, n); printf("字符\t权重\t哈夫曼编码\n"); for (i = 0; i < n; i++) { printf("%c\t%d\t%s\n", hcd[i].ch, ht[i].weight, hcd[i].code); } return 0; } ``` 以上程序中,`CreateHT`函数用于建立哈夫曼树,`CreateHCode`函数用于生成哈夫曼编码。在这个例子中,我们假设字符集为26个小写字母。对于每个字符,我们首先计算它的权重。然后利用`CreateHT`函数建立哈夫曼树,再利用`CreateHCode`函数生成哈夫曼编码。最后输出每个字符的权重和哈夫曼编码。 需要注意的一点是,哈夫曼编码是从叶子节点到根节点的,因此我们需要将最后得到的编码字符串反转。这里我们使用了一个`strrev`函数,它可以将一个字符串反转。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mirror_zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值