//最小堆求哈弗曼编码
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
#define MAX 100
struct Node{
int weight; //权重
char c; //字符
string code; //解码结果
Node *parent,*left,*right;
//初始化
Node(){
parent = left = right = NULL;
code="";
}
};
//定义指针比较函数
struct PCmp
{
bool operator () (Node const *x, Node const *y)
{
return x->weight > y->weight;
}
};
priority_queue<Node *,vector<Node *>,PCmp>Q; //最小堆
//构造HuffmanTree
void CreatHuffmanTree(Node *a[],int n){
for(int i=0;i<n;i++){
Q.push(a[i]);
}
Node *parent;
Node *s1,*s2;
while(Q.empty()==false){
s1 = Q.top();
Q.pop();
if(Q.empty()==true) break; //最后队列中只有一个元素,也就是已经生成HuffmanTree
s2 = Q.top();
Q.pop();
parent=new Node();
parent->c =' ';
parent->weight = s1->weight+s2->weight; //设置权重
//设定子节点
parent->right = s2;
parent->left = s1;
//设定父节点
s1->parent = parent;
s2->parent = parent;
Q.push(parent);
}
}
//Huffman解码
void HuffmanCode(Node *a[],int n){
for(int i=0;i<n;i++){
Node *cur = a[i];
Node *parent = a[i]->parent;
string code="";
while(parent!=NULL){ //到达根节点的时候解码结束
if(parent->left==cur) code = "0"+code;
else code = "1"+code;
//结点向上一层
cur = parent;
parent = parent->parent;
}
a[i]->code = code;//赋值编码
}
}
int main(){
Node *initNode[MAX] ;//存储叶子节点信息
//freopen("HuffmanTreeData.txt","r",stdin);
int n;
scanf("%d",&n);
//输入 信息
for(int i=0;i<n;i++){
Node *newNode = new Node();
int weight ;
char c;
scanf(" %c%d",&c,&weight);
newNode->c = c;
newNode->weight = weight;
initNode[i] = newNode;
}
CreatHuffmanTree(initNode,n);
HuffmanCode(initNode,n);
//输出信息
for(int i=0;i<n;i++){
cout<<initNode[i]->c<<" "<<initNode[i]->code<<endl;
}
return 0;
}
//测试数据
6
a 45
b 13
c 12
d 16
e 9
f 5
//结果
a 0
b 101
c 100
d 111
e 1101
f 1100
//*下面是建树求哈弗曼编码*//
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10 // 带编码字符的个数,即树中叶结点的最大个数
#define M (2*N-1) // 树中总的结点数目
class HTNode{ // 树中结点的结构
public:
unsigned int weight;
unsigned int parent,lchild,rchild;
};
class HTCode{
public:
char data; // 待编码的字符
int weight; // 字符的权值
char code[N]; // 字符的编码
};
void Init(HTCode hc[], int *n){
// 初始化,读入待编码字符的个数n,从键盘输入n个字符和n个权值
int i;
printf("input n = ");
scanf("%d",&(*n));
printf("\ninput %d character\n",*n);
fflush(stdin);
for(i=1; i<=*n; ++i)
scanf("%c",&hc[i].data);
printf("\ninput %d weight\n",*n);
for(i=1; i<=*n; ++i)
scanf("%d",&(hc[i].weight) );
fflush(stdin);
}//
void Select(HTNode ht[], int k, int *s1, int *s2){
// ht[1...k]中选择parent为0,并且weight最小的两个结点,其序号由指针变量s1,s2指示
int i;
for(i=1; i<=k && ht[i].parent != 0; ++i){
; ;
}
*s1 = i;
for(i=1; i<=k; ++i){
if(ht[i].parent==0 && ht[i].weight<ht[*s1].weight)
*s1 = i;
}
for(i=1; i<=k; ++i){
if(ht[i].parent==0 && i!=*s1)
break;
}
*s2 = i;
for(i=1; i<=k; ++i){
if(ht[i].parent==0 && i!=*s1 && ht[i].weight<ht[*s2].weight)
*s2 = i;
}
}
void HuffmanCoding(HTNode ht[],HTCode hc[],int n){
// 构造Huffman树ht,并求出n个字符的编码
char cd[N];
int i,j,m,c,f,s1,s2,start;
m = 2*n-1;
for(i=1; i<=m; ++i){
if(i <= n)
ht[i].weight = hc[i].weight;
else
ht[i].parent = 0;
ht[i].parent = ht[i].lchild = ht[i].rchild = 0;
}
for(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;
}
cd[n-1] = '\0';
for(i=1; i<=n; ++i){
start = n-1;
for(c=i,f=ht[i].parent; f; c=f,f=ht[f].parent){
if(ht[f].lchild == c)
cd[--start] = '0';
else
cd[--start] = '1';
}
strcpy(hc[i].code, &cd[start]);
}
}
int main()
{
int i,m,n,w[N+1];
HTNode ht[M+1];
HTCode hc[N+1];
Init(hc, &n); // 初始化
HuffmanCoding(ht,hc,n); // 构造Huffman树,并形成字符的编码
for(i=1; i<=n; ++i)
printf("\n%c---%s",hc[i].data,hc[i].code);
printf("\n");
return 0;
}
//测试数据
6
abcdef
45
13
12
16
9
5
//结果
a---0
b---101
c---100
d---111
e---1101
f ---1100
最小堆和建树分别求哈弗曼编码
最新推荐文章于 2021-12-10 21:12:27 发布