早教吧 育儿知识 作业答案 考试题库 百科 知识分享

给定权值(7,18,3,32,5,26,12,8),构造相应的哈夫曼树如题,麻烦写出过程,谢谢!原题我看过,不过不够细,可否细一些

题目详情
给定权值(7,18,3,32,5,26,12,8),构造相应的哈夫曼树
如题,麻烦写出过程,谢谢!
原题我看过,不过不够细,可否细一些
▼优质解答
答案和解析
这还不够细?
3+5=8,此时序列为8 7 8 12 18 26 32
7+8=15,此时序列为15 8 12 18 26 32
8+12=20,此时序列为15 20 18 26 32
……每一步都挑最小的两个相加.
图见下面.
多看书,baidu上不好画图,打这些东西很累.
------------------
原答题者:plause
按权值大小排列后 3 5 7 8 12 18 26 32
只要按照将最小的两个合并, 合并后的值再入列中(最小的两个出列), 至到列中只有一个值.
按上面要求构造哈夫曼树如下:
/////树列完后, 可取左树编码 为0, 右为 1, (左为 1, 右为 0 亦可)
[3]`````[5]`````````[7]``````[8]
``\`````/`````````````\``````/
`0`\```/`1```````````0`\````/`1
````\`/`````````````````\``/
````(8)`````[12]````````(15)`````[18]
``````\``````/`````````````\``````/
`````0`\````/`1```````````0`\````/`1
````````\``/`````````````````\``/
````````(20)``````[26]```````(33)``````[32]
```````````\``````/`````````````\``````/
``````````0`\````/`1```````````0`\````/`1
`````````````\``/`````````````````\``/
`````````````(46)`````````````````(65)
````````````````\`````````````````/
```````````````0`\```````````````/`1
``````````````````\`````````````/
```````````````````````(111)
则按上面的树可得到各权值所对应的编码:
//// 其编码是从树顶到该权值点所经过的 1 或 0 的序列
[`7]:``1`0`0`0
[18]:``1`0`1
[`3]:``0`0`0`0
[32]:``1`1
[`5]:``0`0`0`1
[26]:``0`1
[12]:``0`0`1
[`8]:``1`0`0`1