#46511: C++使用者請注意


ck1090932@gl.ck.tp.edu.tw (陳邦仁)

學校 : 臺北市立建國高級中學
編號 : 131859
來源 : [140.112.24.194]
最後登入時間 :
2025-10-07 15:41:17

此題目是要解huffmann code
要用到min heap,可用 priority queue 書寫
要用 #include<queue>;#include<vector>;#include<functional>;然後輸出資料要注意大小要用 long才夠,int會出問題。

#46512: Re: C++使用者請注意


ck1090932@gl.ck.tp.edu.tw (陳邦仁)

學校 : 臺北市立建國高級中學
編號 : 131859
來源 : [140.112.24.194]
最後登入時間 :
2025-10-07 15:41:17

此題目是要解huffmann code
要用到min heap,可用 priority queue 書寫
要用 #include;#include;#include;然後輸出資料要注意大小要用 long才夠,int會出問題。

Huffman's Algorithm 如下:
Huffman(C)
 // C: input characters; Q: min-priority queue
 1. n=|C|
 2. Q =C
 3. for i=1 to n -1
 4.       Allocate a new node z
 5.       z.left=x=Extract-Min(Q)
 6.       z.right=y=Extract-Min(Q)
 7.       z.freq=x.freq+ y.freq
 8.       Insert(Q, z)
 9. return Extract-Min(Q)  //return the root of the tree