详解哈夫曼编码+C语言

#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++;	
	}
	/
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值