数据结构——阿夫曼树及其应用

 

目录

1.哈夫曼树的基本概念 

2.哈夫曼树的构造算法 

1.构造哈夫曼树的口诀 

3.哈夫曼树构造算法的实现

1.结点类型定义 

代码示例:

 

2.哈夫曼树构造算法的实现 

代码示例:

 

4.哈夫曼编码 

1.哈夫曼编码的概念

2.哈夫曼编码的算法实现 

代码示例:

 

3.文件的编码与解码——应用举例 

5.总的代码


64c40f29605d4a079ffcf41866d6638d.png

 

5c8f00ea44fc455386ac98a5ea6546b8.png

1.哈夫曼树的基本概念 

8322605e30f14cdb8668048eccba20e4.png

d49f9186fcd3496dac040a54554b715a.png

f5970cff0b8e4580b58c64a0c4b7e409.png

102a7488f0bf4419a38bd7045cd62d78.png

065f5bf9bc32416aa2b4df05fd001254.png

9e0d7a15b5394cf982796236dded4dd6.png

5c25984eb1534b8eabaefb5848bc8f57.png

c4b956d5b38b4b9c99984aca4e46cf6f.png

ab2f44bf3a4540ab9d68719e48b0a41f.png

0daf5a3ffd33455a9b58939131c23cba.png

2.哈夫曼树的构造算法 

4c46c80a89e7407a82d73f7c4df2aaf2.png

1.构造哈夫曼树的口诀 

5ce38c62a59e47d38b2f4f93168b5732.png

ce8f0569dc254eb78be4deebd11e54f8.png

871c3ef8afec4388ae0d0b363c3b3c54.png

61c7e15fe52b48b1b58df173eb87f640.png

cace7118b7074084bd2f45a445ab7fa3.png

3.哈夫曼树构造算法的实现

1.结点类型定义 

6e83956e894648c3ac1641f82b275c86.png

代码示例:

typedef struct{
	int weight;
	int parent,lch,rch;
}htnode,*huffmantree;

 

fa7778661c334917926357a37a64063f.png

629e99b29c854ce8af7d06b2bad55a65.png

2.哈夫曼树构造算法的实现 

632826365fb747d3a76dd4e7651e2821.png

e77bf01fcbf5488e91228ae7158df2a2.png

代码示例:

 

void createhuffmantree(huffmantree ht,int n)
{
	if(n <= 1) return;
	int m = 2 * n - 1;
	ht = new htnode[m + 1];
	for(int i = 1; i <= m; i++)
	{
		ht[i].lch = 0;
		ht[i].rch = 0;
		ht[i].parent = 0;
	}
	for(int i = 1; i <= n; i++)
	{
		cin >> ht[i].weight;
	}
	for(int i = n + 1; i <= m; i++)
	{
		select(ht,i - 1,s1,s2);
		ht[s1].parent = i,ht[s2].parent = i;
		ht[i].lch = s1,ht[i].rch = s2;
		ht[i].weight = ht[s1].weight + ht[s2].weight;
	}
}

f77952db0a6e4e88bd77ff2ed7d1ea46.png

7a86ba7585334ccda87db93aff67b107.png

df6f63bc54f7401098ecb80e47665228.png

4.哈夫曼编码 

1.哈夫曼编码的概念

abd3618168134a65acbe1c9e81b96517.png

24322f262d6943dea1c85bb3d33eadbd.png

0fb62fe1433d4894b583fa592af2ff30.png

c22299dd908240288022bdcfd27f0a82.png

ccf03a3cb27a4ab484b1cc108a8d16b8.png

777c841c583743a29076c058f1246e12.png

eb2159e968b3431e8f869d75def131da.png

50eeed61bd144c6ab372b41a57e8094a.png

2.哈夫曼编码的算法实现 

980accc2af51406ea1365fcf8e548c78.png

e0511ba28eda4af29916e559aae85031.png

748808965af3486f9532961cc13fc424.png

0aa778f6a2354c89af7df3ad6c59c9e0.png

代码示例:

void createhuffmancode(huffmantree ht,huffmancode &hc,int n)
{
	hc = new char[n + 1];
	char *cd;
	cd = new char[n];
	cd[n - 1] = '\0';
	for(int i = 1; i <= n; i++)
	{
		int start = n - 1;
		int c = i;
		int f = ht[i].parent;
		while(f != 0)
		{
			start--;
			if(ht[f].lch == c) cd[start] = '0';
			else cd[start] = '1';
			c = f,f = ht[f].parent;
		}
		hc[i] = new char[n - start];
		strcpy(hc[i],&cd[start]);
	}
	delete cd;
}

 

3.文件的编码与解码——应用举例 

6cb15c7552544cefaec3afbc29aeec1f.png

a11bdeac0247408eb84b6e37d1fedaef.png

a0688e8e2aef4463822c12ea9812a9ee.png

57c98f2f3fc14b0a9ff2aeb97d2d1346.png

3c3fb9e1ec244b0ab382f16f1e498135.png

d2b1191630864a9ea4453ce88d468a89.png

d0e0ed5438844f189c2970c6e10963c1.png

5.总的代码

typedef struct{
	int weight;
	int parent,lch,rch;
}htnode,*huffmantree;

void createhuffmantree(huffmantree ht,int n)
{
	if(n <= 1) return;
	int m = 2 * n - 1;
	ht = new htnode[m + 1];
	for(int i = 1; i <= m; i++)
	{
		ht[i].lch = 0;
		ht[i].rch = 0;
		ht[i].parent = 0;
	}
	for(int i = 1; i <= n; i++)
	{
		cin >> ht[i].weight;
	}
	for(int i = n + 1; i <= m; i++)
	{
		select(ht,i - 1,s1,s2);
		ht[s1].parent = i,ht[s2].parent = i;
		ht[i].lch = s1,ht[i].rch = s2;
		ht[i].weight = ht[s1].weight + ht[s2].weight;
	}
}

void createhuffmancode(huffmantree ht,huffmancode &hc,int n)
{
	hc = new char[n + 1];
	char *cd;
	cd = new char[n];
	cd[n - 1] = '\0';
	for(int i = 1; i <= n; i++)
	{
		int start = n - 1;
		int c = i;
		int f = ht[i].parent;
		while(f != 0)
		{
			start--;
			if(ht[f].lch == c) cd[start] = '0';
			else cd[start] = '1';
			c = f,f = ht[f].parent;
		}
		hc[i] = new char[n - start];
		strcpy(hc[i],&cd[start]);
	}
	delete cd;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

柏箱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值