3.哈夫曼编码/译码系统(树应用)
include
include
define LEN 16
define MAXLEAF 8 //最大叶子结点数目
define MAXNODE (MAXLEAF*2)-1
typedef char DataType;
typedef struct //哈夫曼树结点类型
{
DataType data;
int parent;
int LChild;
int RChild;
int weight;
}HNode;
typedef HNode Huffman[MAXLEAF * 2 - 1];
typedef struct //哈夫曼编码结构体
{
int start; //存放编码的起始位置从右到左
char bit[LEN]; //存放哈夫曼编码
}HCode;
typedef HCode HuffCode[MAXLEAF];
void Weight(char *DW, char *dw, int *w, int &n) //电文字符权值统计
{
int i = 0, j, k = 0;
while (DW[i] != ‘\0’)
{
for (j = 0; j
这里写代码片3.哈夫曼编码/译码系统(树应用)
#include<iostream>
#include<string.h>
using namespace std;
#define LEN 16
#define MAXLEAF 8 //最大叶子结点数目
#define MAXNODE (MAXLEAF*2)-1
typedef char DataType;
typedef struct //哈夫曼树结点类型
{
DataType data;
int parent;
int LChild;
int RChild;
int weight;
}HNode;
typedef HNode Huffman[MAXLEAF * 2 - 1];
typedef struct //哈夫曼编码结构体
{
int start; //存放编码的起始位置从右到左
char bit[LEN]; //存放哈夫曼编码
}HCode;
typedef HCode HuffCode[MAXLEAF];
void Weight(char *DW, char *dw, int *w, int &n) //电文字符权值统计
{
int i = 0, j, k = 0;
while (DW[i] != '\0')
{
for (j = 0; j<k; j++)
{
if (DW[i] == dw[j])
{
w[j]++;
break; //防止j++导致j==k;
}
}
if (j == k)
{
dw[k] = DW[i];
w[k]++;
k++;
}
i++;
}
n = k;
dw[k] = '\0';
}
//哈夫曼树的构造
void createHuffmanTree(Huffman h, int leaves, int *weight, char *dw)
{
int i, j;
for (i = 0; i<leaves; i++)
{
(h + i)->data = dw[i];
(h + i)->parent = -1;
(h + i)->LChild = -1;
(h + i)->RChild = -1;
(h + i)->weight = *(weight + i); //给叶子赋权值
cout << endl << "哈夫曼树的第" << i + 1 << "个叶子结点 [" << (h + i)->data << "] 频率为" << (h + i)->weight;
}
for (i = leaves; i<leaves * 2 - 1; i++) //初始化哈夫曼树
{
(h + i)->parent = -1;
(h + i)->LChild = -1;
(h + i)->RChild = -1;
(h + i)->weight = 0;
}
cout << endl;
for (i = 0; i<leaves - 1; i++)
{
int m1, m2;
int m1_pos, m2_pos;
m1 = m2 = 65536; //m1存放最小的权值,m2存放次小的权值
m1_pos = m2_pos = 0;
for (j = 0; j<leaves + i; j++)
{
if ((h + j)->weight<m1 && (h + j)->parent == -1)
{
m2 = m1;
m1 = (h + j)->weight;
m2_pos = m1_pos;
m1_pos = j;
}
else if ((h + j)->weight<m2 && (h + j)->parent == -1)
{
m2 = (h + j)->weight;
m2_pos = j;
}
}
(h + leaves + i)->parent = -1; //生成的新根,没有双亲
(h + leaves + i)->LChild = m1_pos; //新根左孩子的下标
(h + leaves + i)->RChild = m2_pos; //新根左孩子的下标
(h + m1_pos)->parent = leaves + i;
(h + m2_pos)->parent = leaves + i;
(h + leaves + i)->weight = m1 + m2;
cout << endl << "第" << m1_pos + 1 << "个叶子和第" << m2_pos + 1 << "个叶子生成新根第" << leaves + i + 1 << "个结点,";
cout << "权值为:" << (h + leaves + i)->weight;
}
cout << endl;
}
//哈夫曼编码
void huffmancode(Huffman h, HuffCode code, int leaves)
{
int i, p, c;
HCode *hf;
hf = new HCode;
hf->bit[LEN - 1] = '\0';
for (i = 0; i<leaves; i++) //从叶子结点向上计算出哈夫曼编码
{
c = i;
p = h[i].parent;
hf->start = LEN - 1;
while (p != -1)
{
if (h[p].LChild == c)
{
hf->bit[--hf->start] = '0';
}
else
{
hf->bit[--hf->start] = '1';
}
c = p;
p = h[c].parent;
}
strcpy_s(code[i].bit, &hf->bit[hf->start]);
cout << endl << "第" << i + 1 << "个叶子结点 [" << (h + i)->data << "] 的哈夫曼编码为:";
cout << (code + i)->bit;
}
cout << endl;
delete hf;
}
void BianMa(HuffCode code, char *DW, char *dw, char *bm)
{
int m1, m2 = 0;
int i = 0, j;
while (DW[i] != '\0')
{
j = 0;
while (dw[j] != '\0')
{
if (DW[i] == dw[j])
{
m1 = 0;
while (code[j].bit[m1] != '\0')
bm[m2++] = code[j].bit[m1++];
break; //找到相等打破循环进行DW[i++]
}
j++;
}
i++;
}
bm[m2++] = '\0';
}
void YiMa(Huffman h, int leaves, char *bm)
{
int T = 2 * leaves - 1; //根节点的位序;
int m = T - 1;
for (int i = 0; bm[i] != '\0'; i++)
{
if (h[m].LChild == -1)
{
cout << h[m].data;
m = T - 1;
}
if (bm[i] == '0')
m = h[m].LChild;
else
m = h[m].RChild;
}
cout << h[m].data << endl;
}
int main()
{
Huffman HN;
HuffCode HC;
char DW[200], dw[100], bm[1000], BM[1000];
int w[100], n, i; //能统计100个权值
cout << endl << '\t' << '\t' << "=========哈夫曼编码系统=========" << endl;
cout << endl << "请输入电文字符信息:";
cin >> DW;
for (i = 0; i<100; i++) w[i] = 0;
Weight(DW, dw, w, n);
createHuffmanTree(HN, n, w, dw); //构造哈夫曼树
huffmancode(HN, HC, n); //哈夫曼编码的实现
cout << endl;
BianMa(HC, DW, dw, bm);
cout << "电文字符的编码:";
cout << bm << endl;
cout << "被翻译过的译码:";
YiMa(HN, n, bm);
cout << endl;
loop:
cout << "请输入你要翻译的编码:";
cin >> BM;
cout << "翻译的编码为:";
YiMa(HN, n, BM);
while (1)
{
cout << "你还需要继续翻译吗?<Y or N>:";
char c;
cin >> c;
cout << endl;
if (c == 'Y' || c == 'y')
goto loop;
else
break;
}
system("pause");
return 0;
}