#include <iostream.h>
#include <stdio.h>
#include <string.h>
#define OVERFLOW -1
typedef struct
{
char letter
int weight
int parent
int lchild
int rchild
}HTNode,*HuffmanTree
typedef char * *HuffmanCode
void Select(HuffmanTree &HT,int i,int &s1,int &s2)
{
/*选择森林中,根结点的链绝好权值最小和次宏咐小的两个树,
*将其根结点的下标号记入s1和s2中
*/
int j, k
for(k = 1k <ik++)
{
if(HT[k].parent != NULL)
continue
s1 = k/*init the number*/
break
}
for(j = 1j <ij++)
{
if(HT[j].parent != NULL)
continue
if(HT[j].weight <HT[s1].weight)
s1 = j
}
for(k = 1k <= ik++)
{
if(HT[k].parent != NULL || k == s1)
continue
s2 = k
break
}
for(j = 1j <ij++)
{
if(HT[j].parent != NULL)
continue
if(HT[j].weight <= HT[s2].weight &&j != s1)
s2 = j
}
}
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,char *zi,int *w,int n)
{
HuffmanTree p
int m,i,s1,s2,f,c
int Istart = 1
char *cd
if(n <= 1)
return
m = 2*n-1
if(!(HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode))))
exit(OVERFLOW)
for(p=HT+1,i=1i<=n++i,++zi,++p,++w)
{
/*生成独立棚铅的森林*/
p->parent = NULL
p->letter = *zi
p->lchild = NULL
p->rchild = NULL
p->weight = *w
}
for(i<=m++i,++p)
{
(*p).weight=0
(*p).parent=0
(*p).lchild=0
(*p).rchild=0
}
for(i=n+1i<=m++i)
{
Select(HT,i-1,s1,s2)
HT[s1].parent=i
HT[s2].parent=i
HT[i].lchild=s1
HT[i].rchild=s2
HT[i].weight=HT[s1].weight+HT[s2].weight
}
HC=(HuffmanCode)malloc((n+1)*sizeof(char *))
cd=(char*)malloc(n*sizeof(char))/*临时的code存储*/
cd[n-1]='\0'
for(i=1i<=n++i)
{
Istart = n - 1
/*按已生成的哈夫曼树,得到各个字符的哈夫曼编码
*/
for(c = i, f = HT[i].parentf != 0c = f, f = HT[f].parent)
if(HT[f].lchild == c)
cd[--Istart] = '0'
else
cd[--Istart] = '1'
HC[i] = (char *)malloc((n - Istart) * sizeof(char))
strcpy(HC[i], &cd[Istart])
}
free(cd)
}
void main()
{
HuffmanTree HT
HuffmanCode HC
int i,j,yu
char zi[9]={'A','B','C','D','E','F','G','H'}
int w[100]
char z
char c[100]
z='A'
cout<<endl
for(i=0i<=7i++)
{
cout<<"please input the weight for "<<z<<":"
cin>>w[i]
z++
}
HuffmanCoding(HT,HC,zi,w,8)
cout<<endl
cout<<"char weight huffmancode"<<endl
for(i=1i<=8i++)
cout<<HT[i].letter<<" "<<HT[i].weight<<" "<<HC[i]<<endl
cout<<"please input the text:"
cin>>c
cout<<"The code is:"
for(i=0i <strlen(c) i++)
/*根据字符的哈夫曼编码,将输入的文本(变量c表示的)翻译成电码。
*/
cout<<HC[(c[i] - 'A' + 1)]
cout<<endl
cout<<"Enter the code:"
cin>>c
j=strlen(c)
yu=15
i=1
cout<<"The text is:"
while(i <= j)
{
while(HT[yu].lchild != 0)/*因为是完全二叉树*/
{
if(c[i-1] == '0')
{
/*用哈夫曼树,将输入的电码(变量c表示的)翻译成文本,
说明:变量名c在程序中
*/
yu = HT[yu].lchild
i++
continue
}
if(c[i-1]== '1')
{
yu=HT[yu].rchild
i++
continue
}
}
/*显示由部分电码译码得到的字符,并准备对后面的电码进行译码*/
cout<<HT[yu].letter
yu = 15
}
cout<<endl
}
Matlab自带Huffman函数(ps:你拼写错了)huffmandeco Huffman decoder
huffmandict Generate Huffman code dictionary for a source with known probability model
huffmanenco Huffman encoder
密凳乎悉码生成:枣乎
symbols = [1 2 3]% Data symbols
p = [0.1 0.1 0.8]% Probability of each data symbol
dict = huffmandict(symbols,p) % Create the dictionary.
dict{1,:} % Show one row of the dictionary.
加密解顷纯密:
sig = repmat([3 3 1 3 3 3 3 3 2 3],1,50)% Data to encode
symbols = [1 2 3]% Distinct data symbols appearing in sig
p = [0.1 0.1 0.8]% Probability of each data symbol
dict = huffmandict(symbols,p)% Create the dictionary.
hcode = huffmanenco(sig,dict)% Encode the data.
dhsig = huffmandeco(hcode,dict)% Decode the code.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)