資料結構 Huffman樹

2022-05-18 11:38:26 字數 1357 閱讀 2635

參照書上寫的huffman樹的** 結構用的是線性儲存的結構 不是二叉鍊錶 裡面要用到查詢最小和第二小 理論上錦標賽法比較好 但是實現好麻煩啊 考慮到資料量不是很大 就直接用比較笨的先找最小 去掉最小再找第二小的方法了。

#include #include 

#include

typedef

struct

htnode, *huffmantree;

typedef

char **huffmancode;

int select(huffmantree ht, int n, int &s1, int &s2)

else

if(ht[i].parent == 0 && ht[i].weight < ht[s1].weight && s1 != 0

)

}for(i = 1; i <= n; i++)

else

if(s2 == 0 && ht[i].parent == 0

)

else

if(ht[i].parent == 0 && ht[i].weight < ht[s2].weight && s2 != 0

) }}

return0;

}void huffmancoding(huffmantree &ht, huffmancode &hc, int * w, int

n)

for( ; i <= m; i++, p++)

for(i = n + 1; i <= m; i++) //

建huffman樹

hc = (huffmancode)malloc((n + 1) * sizeof(char *));

char * cd = (char *)malloc(n * sizeof(char

)); cd[n - 1] = '\0'

;

for(i = 1; i <= n; i++)

}hc[i] = (char *)malloc((n - start) * sizeof(char

)); strcpy(hc[i], &cd[start]);

}free(cd);

}int

main()

; huffmantree ht;

huffmancode hc;

huffmancoding(ht, hc, w, n);

inti, j;

for(i = 1; i <= n; i++)

printf("\n

");}

return0;

}

資料結構 Huffman樹

參照書上寫的huffman樹的 結構用的是線性儲存的結構 不是二叉鍊錶 裡面要用到查詢最小和第二小 理論上錦標賽法比較好 但是實現好麻煩啊 考慮到資料量不是很大 就直接用比較笨的先找最小 去掉最小再找第二小的方法了。include include include typedef struct htn...

oj資料結構 Huffman樹

對輸入的英文大寫字母進行統計概率 然後構建哈夫曼樹,輸出是按照概率降序排序輸出huffman編碼。大寫字母個數 n 第乙個字母 第二個字母 第三個字母 第n個字母。字母1 出現次數 huffman編碼 字母2 出現次數 huffman編碼 字母3 出現次數 huffman編碼 字母n 出現次數 hu...

資料結構Huffman樹及編碼

一 實驗目的 構造乙個哈夫曼樹,並根據所構造的哈夫曼樹求其哈夫曼樹的編碼 二 基本思路 將每個英文本母依照出現頻率由小排到大,最小在左,組成乙個序列 每個字母都代表乙個終端節點 葉節點 比較每個字母的出現頻率,將最小的兩個字母頻率相加合成乙個新的節點,將兩個字母從序列中刪除,將生成的節點加入到字母佇...