给定权值(7,18,3,32,5,26,12,8),构造相应的哈夫曼树.
我快啊,快考试了.
参考答案:按权值大小排列后 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