給定權值(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)
圖真不好畫,詳細可以參考