swustOJ哈弗曼編碼(985)

2021-06-22 02:38:59 字數 1190 閱讀 2379

這裡先把哈弗曼樹建立出來,在進行編碼,因為書上那個陣列做的**看得頭暈

#include

#include

#include

using namespace std;

typedef struct node

htnode;

htnode *h, *p, *st[200];

int n, n, val[200], mina = 0, minb = 0, flag[200] = , ha[100], top, flag_f;

void f(htnode *h, char idata)//輸出idata的編碼

if (h->lchild != null)

if (h->rchild != null)

}void putx(htnode *h)

int main()

if (n == 1)

int minnum;

while (1)

}//找出剩餘節點最小權值的那個點mina

flag[mina] = 1;//標記為已用

minnum = int_max;

for (int i = 0; i < n; i++)//再找乙個

}if (minnum == int_max)//當找不到第二個最小權值,則上面找那個mina已經是最後乙個根節點了

break;

flag[minb] = 1;

st[n] = (htnode*)malloc(sizeof(htnode));//乙個新節點

st[n]->datax = null;

val[n] = val[mina] + val[minb];

st[n]->data = val[n];

st[n]->lchild = st[mina];

st[n]->rchild = st[minb];

n++;

}for (int i = 0; i < n; i++)

return 0;

}再來個別處搞來的標準答案,陣列做的

#include

using namespace std;

typedef struct node1

htnode;

typedef struct node2

hcode;

void createht(htnode ht, int n)

哈弗曼編碼 哈弗曼樹

哈弗曼編碼是依賴於字元使用頻率來建立的一種編碼,通過把使用頻率低的字元分配相對較多的01編碼,而使用頻率高的分配相對較低的01編碼,來建立最小的帶權路徑長度的樹,來最大化的獲得編碼儲存空間的一種編碼規則。這個樹稱為哈弗曼樹,也稱為最優二叉樹。這樣可以確定每乙個字元的編碼不可能成為其他字元編碼的坐子串...

哈弗曼編碼

include include include define my strmax 100 define infinity 1000000 typedef struct htnode,huffmantree 動態分配陣列儲存赫夫曼樹 typedef char huffmancode 動態分配陣列儲存赫...

哈弗曼編碼

include iostream include queue include vector include string using namespace std struct huffmantree bool operator const huffmantree t const vector h 相...