#include<iostream>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
struct Element
{
int parent;
int lchild;
int rchild;
int weight;
int num;
char value;
Element(){};
Element(int a,int b,int c,int d)
{
weight=a;
lchild=b;
rchild=c;
num=d;
}
bool operator < (const Element & e)const
{
return weight>e.weight;
}
} ht[1000];
struct Code
{
int bit[1000];
int start;
} code[1000];
void Huffman(Element ht[],int n)
{
priority_queue<Element> Q;
for(int i=1; i<=n; i++)
Q.push(Element(ht[i].weight,ht[i].lchild,ht[i].rchild,ht[i].num));
for(int i=1; i<=n-1; i++)
{
Element s1=Q.top();
Q.pop();
Element s2=Q.top();
Q.pop();
ht[s1.num].parent=i+n;
ht[s2.num].parent=i+n;
ht[i+n].lchild=s1.num;
ht[i+n].rchild=s2.num;
ht[i+n].weight=s1.weight+s2.weight;
ht[i+n].num=i+n;
Q.push(Element(ht[i+n].weight,ht[i+n].lchild,ht[i+n].rchild,ht[i+n].num));
}
}
void Huffcode(int n)
{
Code cd;
int Parent;
for(int i=1; i<=n; i++)
{
cd.start=n;
int c=i;
Parent = ht[i].parent;
while(Parent!=-1)
{
if(ht[Parent].lchild==c)
cd.bit[cd.start--]=0;
else
cd.bit[cd.start--]=1;
c=Parent;
Parent=ht[Parent].parent;
}
code[i].start=1;
for(int j=cd.start+1; j<=n; j++)
code[i].bit[code[i].start++]=cd.bit[j];
}
}
void Print(Element ht[],int n)
{
cout<<"index weight parent lchild rchild"<<endl;
cout<<left;
for(int i=1; i<=2*n-1; i++)
{
cout<<setw(5)<<i<<" ";
cout<<setw(6)<<ht[i].weight<<" ";
cout<<setw(6)<<ht[i].parent<<" ";
cout<<setw(6)<<ht[i].lchild<<" ";
cout<<setw(6)<<ht[i].rchild<<endl;
}
}
int main()
{
int n;
cout<<"输入顶点的个数"<<endl;
cin>>n;
for(int i=1; i<=2*n-1; i++)
{
ht[i].lchild=-1;
ht[i].rchild=-1;
ht[i].parent= -1;
ht[i].weight=0;
}
cout<<"输入初始的权重"<<endl;
for(int i=1; i<=n; i++)
{
cin>>ht[i].value>>ht[i].weight;
ht[i].num=i;
}
Huffman(ht,n);
Print(ht,n);
Huffcode(n);
cout<<"哈夫曼编码如下:"<<endl;
for(int i=1; i<=n; i++)
{
cout<<ht[i].value<<"编码为:";
for(int j=1; j<code[i].start; j++)
cout<<code[i].bit[j]<<" ";
cout<<endl;
}
return 0;
}
初始方法
#include<iostream>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
struct Element
{
int parent;
int lchild;
int rchild;
int weight;
} ht[1000];
void Select(Element a[],int n,int &s1,int &s2)
{
int temp=inf;
for(int i=1; i<=n; i++)
{
if(a[i].parent==-1&&a[i].weight<temp)
{
temp=a[i].weight;
s1=i;
}
}
int tempp=inf;
for(int j=1; j<=n; j++)
{
if(a[j].parent==-1&&a[j].weight<tempp&&j!=s1)
{
tempp=a[j].weight;
s2=j;
}
}
}
void Huffman(Element ht[],int n)
{
for(int i=1; i<=2*n-1; i++)
{
ht[i].parent=-1;
ht[i].lchild=-1;
ht[i].rchild=-1;
}
for(int k=n+1; k<=2*n-1; k++)
{
int i1,i2;
Select(ht,k-1,i1,i2);
ht[i1].parent=k;
ht[i2].parent=k;
ht[k].weight=ht[i1].weight+ht[i2].weight;
ht[k].lchild=i1;
ht[k].rchild=i2;
}
}
void Print(Element ht[],int n)
{
cout<<"index weight parent lchild rchild"<<endl;
cout<<left;
for(int i=1; i<=2*n-1; i++)
{
cout<<setw(5)<<i<<" ";
cout<<setw(6)<<ht[i].weight<<" ";
cout<<setw(6)<<ht[i].parent<<" ";
cout<<setw(6)<<ht[i].lchild<<" ";
cout<<setw(6)<<ht[i].rchild<<endl;
}
}
int main()
{
int n;
cout<<"输入顶点的个数"<<endl;
cin>>n;
cout<<"输入初始的权重"<<endl;
for(int i=1; i<=n; i++)
cin>>ht[i].weight;
Huffman(ht,n);
Print(ht,n);
return 0;
}
第二种方法
#include<iostream>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
struct Element
{
int parent;
int lchild;
int rchild;
int weight;
char value;
} ht[1000];
struct Code
{
int bit[1000];
int start;
}code[1000];
void Huffman(Element ht[],int n)
{
int s1,s2,temp1,temp2;
for(int i=1;i<=n-1;i++)
{
s1=s2=0;
temp1=temp2=inf;
for(int j=1;j<n+i;j++)
{
if(ht[j].weight<temp1&&ht[j].parent==-1)
{
temp2=temp1;
s2=s1;
s1=j;
temp1=ht[j].weight;
}
else if(ht[j].weight<temp2&&ht[j].parent==-1)
{
temp2=ht[j].weight;
s2=j;
}
}
ht[s1].parent=i+n;
ht[s2].parent=i+n;
ht[i+n].lchild=s1;
ht[i+n].rchild=s2;
ht[i+n].weight=ht[s1].weight+ht[s2].weight;
}
}
void Huffcode(int n)
{
Code cd;
int Parent;
for(int i=1;i<=n;i++)
{
cd.start=n;
int c=i;
Parent = ht[i].parent;
while(Parent!=-1)
{
if(ht[Parent].lchild==c)
cd.bit[cd.start--]=0;
else
cd.bit[cd.start--]=1;
c=Parent;
Parent=ht[Parent].parent;
}
code[i].start=1;
for(int j=cd.start+1;j<=n;j++)
code[i].bit[code[i].start++]=cd.bit[j];
}
}
void Print(Element ht[],int n)
{
cout<<"index weight parent lchild rchild"<<endl;
cout<<left;
for(int i=1; i<=2*n-1; i++)
{
cout<<setw(5)<<i<<" ";
cout<<setw(6)<<ht[i].weight<<" ";
cout<<setw(6)<<ht[i].parent<<" ";
cout<<setw(6)<<ht[i].lchild<<" ";
cout<<setw(6)<<ht[i].rchild<<endl;
}
}
int main()
{
int n;
cout<<"输入顶点的个数"<<endl;
cin>>n;
for(int i=1;i<=2*n-1;i++)
{
ht[i].lchild=-1;
ht[i].rchild=-1;
ht[i].parent= -1;
ht[i].weight=0;
}
cout<<"输入初始的权重"<<endl;
for(int i=1; i<=n; i++)
cin>>ht[i].value>>ht[i].weight;
Huffman(ht,n);
Huffcode(n);
cout<<"哈夫曼编码如下:"<<endl;
for(int i=1;i<=n;i++)
{
cout<<ht[i].value<<"编码为:";
for(int j=1;j<code[i].start;j++)
cout<<code[i].bit[j]<<" ";
cout<<endl;
}
Print(ht,n);
return 0;
}
测试数据
8
5 29 7 8 14 23 3 11
1 5
2 29
3 7
4 8
5 14
6 23
7 3
8 11