给定字符串,求哈弗曼编码表和相应的huffman编码
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
typedef struct{
int weight;
int parent, lchild, rchild;
}HTNode, *HuffmanTree;
const int N=1e3+5;
typedef char** HuffmanCode;
map<char, int> num;
char arr[N];
map<char, int> pos;
char s[N];
HuffmanTree CreatHuffmanTree(HuffmanTree &HT, int n)
{
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++)
{
HT[i].weight=num[arr[i]];
}
for(int i=n+1; i<=m; i++)
{
int s1=0, s2=0;
int p=1;
for( ; p<i; p++)
{
if(HT[p].parent==0)
{
if(s1==0)
{
s1=p;
}
else
{
s2=p;
if(HT[s1].weight>HT[s2].weight)
{
int temp=s1;
s1=s2;
s2=temp;
}
break;
}
}
}
for(int j=p+1; j<i; j++)
{
if(HT[j].weight<HT[s1].weight && HT[j].parent==0) s2=s1, s1=j;
else if(HT[j].weight>HT[s1].weight && HT[j].weight<HT[s2].weight && HT[j].parent==0) s2=j;
}
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;
}
return HT;
}
char* cd;
HuffmanCode HC;
HuffmanTree HT=NULL;
HuffmanCode CreatHuffmanCode(HuffmanTree HT, HuffmanCode &HC, int n)
{
HC=new char*[n+1];
cd = new char[n];
cd[n-1]='\0';
int c,f,start;
for(int i=1; i<=n; i++)
{
start=n-1;
c=i;
f=HT[i].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;
return HC;
}
int main()
{
cin>>s;
int len=strlen(s);
int n=0;
for(int i=0; i<len; i++)
{
if(!num[s[i]])
{
n++;
arr[n]=s[i];
pos[s[i]]=n;
}
num[s[i]]++;
}
HT=CreatHuffmanTree(HT, n);
HC=CreatHuffmanCode(HT, HC, n);
cout<<"编码表"<<endl;
for(int i=1; i<=n; i++)
{
cout<<arr[i]<<':'<<HT[i].weight<<' '<<HC[i]<<endl;
}
cout<<"哈弗曼编码:"<<endl;
for(int i=0; i<len; i++)
{
cout<<HC[pos[s[i]]];
}
return 0;
}