通過哈夫曼樹求解編碼,輸入字串,輸出編碼,**如下
#include
#include
#include
#include
typedef
char telemtype;
typedef
struct bitnode bitnode,
* bitree;
//樹節點
typedef
struct huffman huffman,
* huffnode;
//樹鍊錶
void
insertorder
(huffnode t, huffnode node)
;//順序插入鍊錶
huffnode getminnode
(huffnode t)
;//取出最小權重的字元節點
void
huffmangenerate
(huffnode t)
;//生成哈夫曼樹
void
getcode
(char
** codestorage, bitree t,
char
* cd,
int n)
;//計算編碼
void
coding
(char
* str)
;void
insertorder
(huffnode t, huffnode node)
t = t-
>next;
} t-
>next = node;
node-
>next =
null;}
huffnode getminnode
(huffnode t)
t->next = re-
>next;
return re;
}void
huffmangenerate
(huffnode t)
}void
getcode
(char
** codestorage, bitree t,
char
* cd,
int n)
if(t-
>lchild)
if(t-
>rchild)
}void
coding
(char
* str)
while
(*str !=
'\0'
) huffman t;
t.node =
null
; t.next =
null
; huffnode p =
&t;for
(int i =
0; i <
256; i++
)//生成鍊錶
}huffmangenerate
(&t)
;//生成哈夫曼樹
bitree huffmantree = t.next-
>node;
//得到哈夫曼樹根節點
int length =
strlen
(convertstr)
;char
** codestorage =
(char**
)malloc
(256
*sizeof
(char*)
);char
* cd =
(char*)
malloc
((length +1)
*sizeof
(char))
;getcode
(codestorage, huffmantree, cd,0)
;//生成編碼表
while
(*convertstr !=
'\0')}
intmain()
哈夫曼編碼 哈夫曼樹
1.定義 哈夫曼編碼主要用於資料壓縮。哈夫曼編碼是一種可變長編碼。該編碼將出現頻率高的字元,使用短編碼 將出現頻率低的字元,使用長編碼。變長編碼的主要問題是,必須實現非字首編碼,即在乙個字符集中,任何乙個字元的編碼都不是另乙個字元編碼的字首。如 0 10就是非字首編碼,而0 01不是非字首編碼。2....
哈夫曼樹 哈夫曼編碼
定義從a結點到b結點所經過的分支序列為從a結點到b結點的路徑 定義從a結點到b結點所進過的分支個數為從a結點到b結點的路徑長度 從二叉樹的根結點到二叉樹中所有結點的路徑長度紙盒為該二叉樹的路徑長度 huffman樹 帶權值路徑長度最小的擴充二叉樹應是權值大的外界點舉例根結點最近的擴充二叉樹,該樹即為...
哈夫曼編碼 哈夫曼樹
哈夫曼樹是乙個利用權值進行優化編碼的乙個比較奇怪的樹,他的實現比較簡單,用途也比較單一。哈夫曼樹的實現,實現要求 通過哈夫曼樹可以保證在編碼過程中不會出現例如 1000和100這樣的編碼規則,否則就會編碼失敗,因為1000和100在某些情況下的編碼會一模一樣。通過哈夫曼樹可以保證權值大的值進行編碼時...