#include<stdio.h>
#include<queue>
struct Node{
int weight;
char c;
int flag;
Node *lchild;
Node *rchild;
};
using namespace std;
priority_queue<int ,vector<int> ,greater<int> > Q;
int wpl=0;
int code[20];
//创建哈夫曼树
Node *CreatTree(Node *tem,int n){
int m1,m2,m,i,j,count=0;
while(Q.size()>1){
m1=Q.top();
Q.pop();
m2=Q.top();
Q.pop();
m=m1+m2;
Q.push(m);
//printf("%d %d %d",m1,m2,m);
for(i=0;i<2*n-1;i++){
if(tem[i].weight==m1&&tem[i].flag==0){//判定条件忘了判断是否已经查找过 flag
tem[i].flag=1;
break;
}
}
for(j=0;j<2*n-1;j++){
if(tem[j].weight==m2&&tem[j].flag==0){
tem[j].flag=1;
break;
}
}
tem[n+count].weight=m;
tem[n+count].flag=0;
tem[n+count].lchild=tem+i;
tem[n+count].rchild=tem+j;
count++;
}
/
详解哈夫曼编码+C语言
最新推荐文章于 2024-07-26 10:34:47 发布