给定权值(5,10,12,15,30,40),构造相应的哈夫曼树.要求写出构造步骤
按权值大小排列后 5,10,12,15,30,40
只要按照将最小的两个合并, 合并后的值再入列中(最小的两个出列), 至到列中只有一个值.
得到序列5+10=15, (12,15,15,30,40)
[5]`````[10]
\`````/
\```/`
\`/
` `(15)`
从(12,15,15,30,40)找两个最小的12+15=27
[5]`````[10]````````[12]``````[15]
``\`````/`````````````\``````/
` `\```/` ``````````` `\````/`
````\`/`````````````````\``/
````(15)````` ````````(27)`
依次从新的序列中取2个想加和最小的。
(15,27,30,40)-----取15+27=42;
(30,40,42)---------取30+40=70;
(42,70)-------------取42+70=112;
(112)就剩一个终止。
[5]`````[10]`[12]``````[15]
``\`````/``````\``````/
`0`\```/`1`````0\````/`1
````\`/``````````\``/
````(15)````` (27)``[30]`````[40]
``````\`````` /`````````\``````/
`````0`\```` /`1````````0\````/`1
````````\`` /`````````````\``/
```````` (42)````````````` (70)``````
```````````\`````` ````````````/
``````````0`\``````` ```````/
`````````````\ ```````````/
`````````````(46)``````````1/`
````````````````\``````````/``
```````````````0`\````````/``
``````````````````\``````/``
````````````````````(112)
图真不好画,详细可以参考