数据结构(C语言)课设3——哈夫曼树(编码译码)
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef struct
{
char ch;
int weight;
char shu[1000];
int parent, lchild, rchild;
}HTNode, *HuffmanTree;
void Select(HuffmanTree &HT, int n, int &l, int &r)
{
int MIN = 10000;
for(int i = 0; i <= n; i++) {
if(HT[i].parent == 0)
if(HT[i].weight < MIN) {
MIN = HT[i].weight; l = i;
}
}
MIN = 10000;
for(int i = 0; i <= n; i++) {
if(HT[i].parent == 0)
if(HT[i].weight < MIN && i != l) {
MIN = HT[i].weight; r = i;
}
}
}
int CreatHuffmanTree(HuffmanTree &HT, char ch[])
{
int i, j, n1 = strlen(ch), n2 = 0;
int *a = new int[n1];
char *c = new char[n1];
for(i = 0; i < n1; i++) {
for(j = 0; j < n2 && ch[i] != c[j]; j++);
if(j == n2) {
c[n2] = ch[i]; a[j] = 1; n2++;
}
else a[j]++;
}
if(n2 <= 1)
return 0;
int m = 2 * n2 - 1, l, r;
HT = new HTNode[m + 1];
for(i = 0; i < m; i++)
{
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
if(i < n2)
{
HT[i].ch = c[i];
HT[i].weight = a[i];
}
}
for(i = n2; i < m; i++)
{
Select(HT, i-1, l, r);
HT[l].parent = i; HT[r].parent = i;
HT[i].lchild = l; HT[i].rchild = r;
HT[i].weight = HT[l].weight + HT[r].weight;
}
delete c;
delete a;
return n2;
}
void BianMa(HuffmanTree &H, int n)
{
int i, start, c, f;
char *cd;
cd = new char[n];
cd[n - 1] = '\0';
for(i = 0; i < n; i++)
{
start = n - 1;
c = i;
f = H[i].parent;
while(f != 0)
{
--start;
if(H[f].lchild == c)
cd[start] = '0';
else
cd[start] = '1';
c = f;
f = H[f].parent;
}
strcpy(H[i].shu, &cd[start]);
}
delete cd;
}
int Long(HuffmanTree &H, int n)
{
int i, x = 0;
for(i = 0; i < n; i++)
x += H[i].weight * (strlen(H[i].shu));
return x;
}
void TranSlate(HuffmanTree &H, char p[], int n)
{
int x = strlen(p);
char *o = new char[x];
int c = 0, i = 2 * n - 2, k = 0;
while(p[c] != '\0')
{
if(p[c] == '0')
{
i = H[i].lchild;
}
else
{
i = H[i].rchild;
}
if(H[i].lchild == 0 && H[i].rchild == 0)
{
o[k++] = H[i].ch;
i = 2 * n - 2;
}
c++;
}
o[k] = '\0';
cout << k <<endl;
}
int main()
{
char ch[1005]; cin >> ch;
HTNode *H;
int m = CreatHuffmanTree(H, ch);
BianMa(H, m);
int x = Long(H, m);
cout << x << endl;
char *p = new char[x];
for(int i = 0; i < strlen(ch); i++)
{
for(int j = 0; j < m; j++)
if(ch[i] == H[j].ch)
{
if(i == 0)
strcpy(p, H[j].shu);
else
p = strcat(p, H[j].shu);
}
}
p[x] = '\0';
TranSlate(H, p, m);
return 0;
}
此博文只用于博主记录学习过程(有问题可以评论)