第十周專案4哈夫曼編碼的演算法驗證

2021-08-10 12:00:31 字數 1565 閱讀 9073

/* 

* 檔名稱:cpp1. 

* 作    者:劉思源

* 完成日期:2017 年 11 月 8 日 

* 版 本 號:v1.0 

* * 問題描述: 實現二叉樹的哈夫曼編碼的演算法。 

* 輸入描述:無需輸入 

* 程式輸出:實現各種演算法的函式的測試結果 */

[cpp]view plain

copy

#include 

#include 

#define n 50        //葉子結點數

#define m 2*n-1     //樹中結點總數

//哈夫曼樹的節點結構型別

typedef

struct

htnode;  

//每個節點哈夫曼編碼的結構型別

typedef

struct

hcode;  

//構造哈夫曼樹

void

createht(htnode ht,

intn)  

else

if(ht[k].weight

}  ht[i].weight=ht[lnode].weight+ht[rnode].weight;  

ht[i].lchild=lnode;  

ht[i].rchild=rnode;  

ht[lnode].parent=i;  

ht[rnode].parent=i;  

}  }  

//實現哈夫曼編碼

void

createhcode(htnode ht,hcode hcd,

intn)  

hc.start++;     //start指向哈夫曼編碼最開始字元

hcd[i]=hc;  

}  }  

//輸出哈夫曼編碼

void

disphcode(htnode ht,hcode hcd,

intn)  

m+=ht[i].weight;  

sum+=ht[i].weight*j;  

printf("\n"

);  

}  printf("\n  平均長度=%g\n"

,1.0*sum/m);  

}  int

main()  

;  double

fnum= ;  

htnode ht[m];  

hcode hcd[n];  

for(i=0; i

printf("\n"

);  

createht(ht,n);  

createhcode(ht,hcd,n);  

disphcode(ht,hcd,n);  

printf("\n"

);  

return

0;  

}  

執行結果:

第十周 專案一哈夫曼編碼的演算法驗證 (4)

main.cpp include include define n 50 葉子結點數 define m 2 n 1 樹中結點總數 哈夫曼樹的節點結構型別 typedef struct htnode 每個節點哈夫曼編碼的結構型別 typedef struct hcode 構造哈夫曼樹 void cre...

第十周專案1(4) 哈夫曼編碼的演算法驗證

問題及 檔名稱 xxy.作 者 徐瀟雨 完成日期 2017 年 11 月 2 日 版 本 號 v1.0 問題描述 驗證哈弗曼編碼 輸入描述 無 程式輸出 哈弗曼編碼 include include define n 50 葉子結點數 define m 2 n 1 樹中結點總數 哈夫曼樹的節點結構型別...

第十周專案1(4) 哈夫曼編碼的演算法驗證

include include define n 50 葉子結點數 define m 2 n 1 樹中結點總數 哈夫曼樹的節點結構型別 typedef struct htnode 每個節點哈夫曼編碼的結構型別 typedef struct hcode 構造哈夫曼樹 void createht htn...