#include <iostream>
using namespace std;
typedef struct huffmannode
{
char code;
int weight;
struct huffmannode* left;
struct huffmannode* right;
}Huffmannode;
typedef struct codelist
{
Huffmannode* codelist;
struct codelist* next;
}Codelist;
void Inser(Codelist* cl_head, Codelist* code_temp)
{
Codelist* temp = cl_head;
while (temp->next)
{
if (temp->next->codelist->weight < code_temp->codelist->weight)
temp = temp->next;
else
break;
}
code_temp->next = temp->next;
temp->next = code_temp;
}
Codelist* poplist(Codelist* cl_head)
{
Codelist* temp;
temp = cl_head->next;
if (cl_head->next)
{
temp = cl_head->next;
cl_head->next = temp->next;
}
return temp;
}
void print_list(Codelist* cl_head)
{
Codelist*temp = cl_head->next;
while (temp)
{
cout << temp->codelist->code <<":"<< temp->codelist->weight<<"----";
temp = temp->next;
}
cout << endl;
}
void PostOrderTraverse(Huffmannode * cl_head)
{
if (cl_head)
{
cout << cl_head->weight << "--";
PostOrderTraverse(cl_head->left);//遍歷左孩子
PostOrderTraverse(cl_head->right);//遍歷右孩子
}
}
void freetree(Huffmannode* root)
{
if (root)
{
freetree(root->left);
freetree(root->right);
delete root;
}
}
void freelist(Codelist* head)
{
Codelist* temp = head;
while (temp)
{
head = head->next;
delete temp;
temp = head;
}
}
int main()
{
char code[] = {'a','b','c','d','e','f'};
int weight[] = { 19,13,12,16,9,5 };
Codelist* cl_head = new Codelist;
cl_head->next = NULL;
for (int i = 0; i < 6; i++)
{
Huffmannode* temp = new Huffmannode;
temp->code = code[i];
temp->weight = weight[i];
temp->left = NULL;
temp->right = NULL;
Codelist* code_temp = new Codelist;
code_temp->codelist = temp;
code_temp->next = NULL;
Inser(cl_head, code_temp);
}
print_list(cl_head);
for (int i = 0; i < 5; i++)
{
Codelist* temp1 = poplist(cl_head);
Codelist* temp2 = poplist(cl_head);
Codelist* new_node_c = new Codelist;
new_node_c->codelist = new Huffmannode;
new_node_c->codelist->left = temp1->codelist;
new_node_c->codelist->right = temp2->codelist;
new_node_c->codelist->weight = temp1->codelist->weight + temp2->codelist->weight;
delete temp1;
delete temp2;
Inser(cl_head, new_node_c);
}
print_list(cl_head);
if (cl_head->next)
{
PostOrderTraverse(cl_head->next->codelist);
freetree(cl_head->next->codelist);
freelist(cl_head);
}
return 0;
}
哈夫曼編碼——貪心
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)爽彤,“玉大人养盗,你說(shuō)我怎么就攤上這事∈矢荩” “怎么了往核?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)嚷节。 經(jīng)常有香客問(wèn)我聂儒,道長(zhǎng)虎锚,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任衩婚,我火速辦了婚禮窜护,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘非春。我一直安慰自己柱徙,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布奇昙。 她就那樣靜靜地躺著护侮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪储耐。 梳的紋絲不亂的頭發(fā)上羊初,一...
- 那天,我揣著相機(jī)與錄音什湘,去河邊找鬼长赞。 笑死,一個(gè)胖子當(dāng)著我的面吹牛闽撤,可吹牛的內(nèi)容都是我干的得哆。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼腹尖,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼柳恐!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起热幔,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎讼庇,沒(méi)想到半個(gè)月后绎巨,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡蠕啄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年场勤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片歼跟。...
- 正文 年R本政府宣布骚秦,位于F島的核電站她倘,受9級(jí)特大地震影響璧微,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜硬梁,卻給世界環(huán)境...
- 文/蒙蒙 一前硫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧荧止,春花似錦屹电、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至瓷炮,卻和暖如春葱色,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背娘香。 一陣腳步聲響...
- 正文 我出身青樓淋昭,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親安接。 傳聞我的和親對(duì)象是個(gè)殘疾皇子翔忽,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 文章結(jié)構(gòu) 如何理解貪心算法 貪心算法實(shí)例分析 使用貪心算法實(shí)現(xiàn)哈夫曼編碼 源碼地址 說(shuō)明 算法中基本的算法思想有:...
- JAVA代碼實(shí)現(xiàn) Huffman 執(zhí)行程序 執(zhí)行截圖 注:代碼僅供參考 作者QQ:420318184郵箱:fy@0...
- 最小生成樹(shù) 連通圖:在無(wú)向圖中,若任意兩個(gè)頂點(diǎn)vi與vj都有路徑相通硫豆,則稱該無(wú)向圖為連通圖龙巨。強(qiáng)連通圖(Strong...
- Day29學(xué)習(xí)內(nèi)容 :貪心算法:如何用貪心算法實(shí)現(xiàn)Huffman壓縮編碼旨别? 1.如何理解貪心算法?貪心算法解決問(wèn)題...