C語言實現哈夫曼編碼

2021-10-21 17:55:09 字數 1124 閱讀 1022

#include

#include

#define n 50

//葉子結點數

#define m 2*n-1

//樹中結點總數

typedef

struct

htnode;

typedef

struct

hcode;

void

createht

(htnode ht,

int n)

//由ht的葉子結點構造完整的哈夫曼樹

else

if(ht[k]

.weight} ht[lnode]

.parent=i;ht[rnode]

.parent=i;

//合併兩個最小和次小的結點

ht[i]

.weight=ht[lnode]

.weight+ht[rnode]

.weight;

ht[i]

.lchild=lnode;ht[i]

.rchild=rnode;}}

void

createhcode

(htnode ht[

],hcode hcd,

int n)

//由哈夫曼樹ht構造哈夫曼編碼hcd

hc.start++

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

hcd[i]

=hc;}}

void

disphcode

(htnode ht[

],hcode hcd,

int n)

//輸出哈夫曼編碼

printf

("\n");

}}intmain()

;int fnum=

; htnode ht[m]

; hcode hcd[n]

;for

(i=0

;i)createht

(ht,n)

;createhcode

(ht,hcd,n)

;disphcode

(ht,hcd,n)

;return1;

}

C語言實現哈夫曼編碼

include define n5 葉子數目 define m 2 n 1 結點總數 define maxval 10000.0 define maxsize100 哈夫曼編碼的最大位數 typedef struct hufmtree typedef struct codetype void huf...

哈夫曼編碼 C語言實現

c語言實現哈夫曼編碼 程式功能 提供一段字串,輸出哈夫曼編碼壓縮後的總位元數 計入小寫字母和空格 include include 定義二叉樹結構 typedef struct node bitnode,bitree 定義棧結構,此處的棧用來儲存二叉樹節點 typedef structstack 操作...

哈夫曼樹編碼C語言實現

哈夫曼樹編碼c語言實現,實現哈夫曼樹編碼的演算法可分為兩大部分 1 構造哈夫曼樹 2 在哈夫曼樹上求葉結點的編碼 哈夫曼樹構造演算法 1 由給定的n個權值構造n棵只有乙個葉結點的二叉樹,從而得到乙個二叉樹的集合f 2 在f中選取根結點的權值最小和次小的兩棵二叉樹作為左,右子樹構造一棵新的二叉樹,這棵...