版本一
#include <stdio.h>
#include<stdlib.h>
#define MAXBIT 100
#define MAXVALUE 10000
#define MAXLEAF 30
#define MAXNODE MAXLEAF*2 -1
typedef struct
{
int bit[MAXBIT];
int start;
} HCodeType;
typedef struct
{
int weight;
int parent;
int lchild;
int rchild;
int value;
} HNodeType;
void HuffmanTree (HNodeType HuffNode[MAXNODE], int n)
{
int i, j, m1, m2, x1, x2;
for (i=0; i<2*n-1; i++)
{
HuffNode[i].weight = 0;
HuffNode[i].parent =-1;
HuffNode[i].lchild =-1;
HuffNode[i].rchild =-1;
HuffNode[i].value=i;
}
for (i=0; i<n; i++)
{
printf ("Please input weight of leaf node %d: \n", i);
scanf ("%d", &HuffNode[i].weight);
}
for (i=0; i<n-1; i++)
{
m1=m2=MAXVALUE;
x1=x2=0;
for (j=0; j<n+i; j++)
{
if (HuffNode[j].weight < m1 && HuffNode[j].parent==-1)
{
m2=m1;
x2=x1;
m1=HuffNode[j].weight;
x1=j;
}
else if (HuffNode[j].weight < m2 && HuffNode[j].parent==-1)
{
m2=HuffNode[j].weight;
x2=j;
}
}
HuffNode[x1].parent = n+i;
HuffNode[x2].parent = n+i;
HuffNode[n+i].weight = HuffNode[x1].weight + HuffNode[x2].weight;
HuffNode[n+i].lchild = x1;
HuffNode[n+i].rchild = x2;
printf ("x1.weight and x2.weight in round %d: %d, %d\n", i+1, HuffNode[x1].weight, HuffNode[x2].weight);
printf ("\n");
}
}
void decodeing(char string[],HNodeType Buf[],int Num)
{
int i,tmp=0,code[1024];
int m=2*Num-1;
char *nump;
char num[1024];
for(i=0; i<strlen(string); i++)
{
if(string[i]=='0')
num[i]=0;
else
num[i]=1;
}
i=0;
nump=&num[0];
while(nump<(&num[strlen(string)]))
{
tmp=m-1;
while((Buf[tmp].lchild!=-1)&&(Buf[tmp].rchild!=-1))
{
if(*nump==0)
{
tmp=Buf[tmp].lchild ;
}
else tmp=Buf[tmp].rchild;
nump++;
}
printf("%d",Buf[tmp].value);
}
}
int main(void)
{
HNodeType HuffNode[MAXNODE];
HCodeType HuffCode[MAXLEAF], cd;
int i, j, c, p, n;
char pp[100];
printf ("Please input n:\n");
scanf ("%d", &n);
HuffmanTree (HuffNode, n);
for (i=0; i < n; i++)
{
cd.start = n-1;
c = i;
p = HuffNode[c].parent;
while (p != -1)
{
if (HuffNode[p].lchild == c)
cd.bit[cd.start] = 0;
else
cd.bit[cd.start] = 1;
cd.start--;
c=p;
p=HuffNode[c].parent;
}
for (j=cd.start+1; j<n; j++)
{
HuffCode[i].bit[j] = cd.bit[j];
}
HuffCode[i].start = cd.start;
}
for (i=0; i<n; i++)
{
printf ("%d 's Huffman code is: ", i);
for (j=HuffCode[i].start+1; j < n; j++)
{
printf ("%d", HuffCode[i].bit[j]);
}
printf(" start:%d",HuffCode[i].start);
printf ("\n");
}
printf("Decoding?Please Enter code:\n");
scanf("%s",&pp);
decodeing(pp,HuffNode,n);
getch();
return 0;
}
版本二
#include<iostream>
#include<cstring>
#include<cstdlib>
#define MAX_MA 1000
#define MAX_ZF 100
using namespace std;
typedef struct
{
int weight;
int parent, lchild, rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree HT, int n, int &s1, int &s2)
{
for (int i = 1; i <= n; i++)
{
if (HT[i].parent == 0)
{
s1 = i;
break;
}
}
for (int i = 1; i <= n; i++)
{
if (HT[i].weight < HT[s1].weight && HT[i].parent == 0)
s1 = i;
}
for (int i = 1; i <= n; i++)
{
if (HT[i].parent == 0 && i != s1)
{
s2 = i;
break;
}
}
for (int i = 1; i <= n; i++)
{
if (HT[i].weight < HT[s2].weight && HT[i].parent == 0 && i!= s1)
s2 = i;
}
}
void CreateHuffmanTree(HuffmanTree &HT, int n)
{
if (n <= 1)
return;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= m; ++i)
{
HT[i].parent = 0; HT[i].lchild = 0; HT[i].rchild = 0;
}
for (int i = 1; i <= n; ++i)
{
cin >> HT[i].weight;
}
int s1,s2;
for (int i = n + 1; i <= 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;
}
}
void CreatHuffmanCode(HuffmanTree HT, HuffmanCode &HC, int n)
{
HC = new char*[n + 1];
char *cd = new char[n];
cd[n - 1] = '\0';
for (int i = 1; i <= n; i++)
{
int start = n - 1;
int c = i;
int f = HT[c].parent;
while (f != 0)
{
--start;
if (HT[f].lchild == c) cd[start] = '0';
else cd[start] = '1';
c = f;
f = HT[f].parent;
}
HC[i] = new char[n - start];
strcpy(HC[i], &cd[start]);
}
delete cd;
}
void TranCode(HuffmanTree HT,char a[],char zf[],char b[],int n)
{
int q = 2*n-1;
int k = 0;
int i = 0;
for (i = 0; a[i] != '\0';i++)
{
if (a[i] == '0')
{
q = HT[q].lchild;
}
else if (a[i] == '1')
{
q = HT[q].rchild;
}
if (HT[q].lchild == 0 && HT[q].rchild == 0)
{
b[k++] = zf[q];
q = 2 * n - 1;
}
}
b[k] = '\0';
}
void menu()
{
cout << endl;
cout << " ┏〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓┓" << endl;
cout << " ┃ ★★★★★★★哈夫曼编码与译码★★★★★★★ ┃" << endl;
cout << " ┃ 1. 创建哈夫曼树 ┃" << endl;
cout << " ┃ 2. 进行哈夫曼编码 ┃" << endl;
cout << " ┃ 3. 进行哈夫曼译码 ┃" << endl;
cout << " ┃ 4. 退出程序 ┃" << endl;
cout << " ┗〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓┛" << endl;
cout << " <><注意:空格字符用'- '代替><>" << endl;
cout << endl;
}
int main()
{
int falg;
char a[MAX_MA];
char b[MAX_ZF];
char zf[MAX_ZF];
HuffmanTree HT = NULL;
HuffmanCode HC = NULL;
menu();
while (true)
{
int num;
cout << "<><请选择功能(1-创建 2-编码 3-译码 4-退出)><>: ";
cin >> num;
switch (num)
{
case 1 :
cout << "<><请输入字符个数><>:";
cin >> falg;
cout << "<><请依次输入" << falg << "个字符:><>: ";
for (int i = 1; i <= falg; i++)
cin >> zf[i];
cout << "<><请依次输入" << falg << "个字符的权值><>: ";
CreateHuffmanTree(HT, falg);
cout << endl;
cout << "<><创建哈夫曼成功!,下面是该哈夫曼树的参数输出><>:" << endl;
cout << endl;
cout << "结点i"<<"\t"<<"字符" << "\t" << "权值" << "\t" << "双亲" << "\t" << "左孩子" << "\t" << "右孩子" << endl;
for (int i = 1; i <= falg * 2 - 1; i++)
{
cout << i << "\t"<<zf[i]<< "\t" << HT[i].weight << "\t" << HT[i].parent << "\t" << HT[i].lchild << "\t" << HT[i].rchild << endl;
}
cout << endl;
break;
case 2:
CreatHuffmanCode(HT, HC, falg);
cout << endl;
cout << "<><生成哈夫曼编码表成功!,下面是该编码表的输出><>:" << endl;
cout << endl;
cout << "结点i"<<"\t"<<"字符" << "\t" << "权值" << "\t" << "编码" << endl;
for (int i = 1; i <= falg; i++)
{
cout << i << "\t"<<zf[i]<< "\t" << HT[i].weight << "\t" << HC[i] << endl;
}
cout << endl;
break;
case 3:
cout << "<><请输入想要翻译的一串二进制编码><>:";
cin >> a;
TranCode(HT, a, zf, b, falg);
cout << endl;
cout << "<><译码成功!翻译结果为><>:" << b << endl;
cout << endl;
break;
case 4:
cout << endl;
cout << "<><退出成功!><>" << endl;
exit(0);
default:
break;
}
}
return 0;
}