哈夫曼编码

头文件Huffman.h

#ifndef _HUFFMAN_H_
#define _HUFFMAN_H_
     struct HTNode
{
	int weight;
	int parent;
	int lchild;
	int rchild;

};
typedef HTNode *HTree;
typedef char **HCode;
void Select(HTree HT,int n,int &s1,int &s2);
void HuffmanCoding(HTree &HT,HCode &HC,int *w,int n);

#endif

Huffman.cpp

 #include"Huffman.h"

#include <stdlib.h>
#include<iostream>
using namespace std;



void Select(HTree HT,int n,int &s1,int &s2)  
{  
    int minn=99999998,maxx=99999999;  
    s1=s2=0;  
    for(int i=1; i<=n; i++)  
    {  
        if(HT[i].parent==0)  
        {  
            if(HT[i].weight<minn)  
            {  
                minn=HT[i].weight;  
                s1=i;  
            }  
        }  
    }  
    int t=HT[s1].weight;  
    HT[s1].weight=maxx;  
    minn=99999998;  
    for(int i=1; i<=n; i++)  
    {  
        if(HT[i].parent==0)  
        {  
            if(HT[i].weight<minn)  
            {  
                minn=HT[i].weight;  
                s2=i;  
            }  
        }  
    }  
    HT[s1].weight=t;  
}



void HuffmanCoding(HTree &HT,HCode &HC,int *w,int n)  
{  
    int m,i,s1,s2;  
    if(n<=1)return;  
    m=2*n-1;  
    HT=(HTree)malloc((m+1)*sizeof(HTNode));  
    HTree p;  
    for(p=HT,i=1; i<=n; i++)  
    {  
        p[i].lchild=0;  
        p[i].rchild=0;  
        p[i].parent=0;  
        p[i].weight=w[i-1];  
		
    }  
    for(; i<=m; i++)  
    {  
        p[i].lchild=0;  
        p[i].rchild=0;  
        p[i].parent=0;  
        p[i].weight=0;  
    }  
    for(i=n+1; i<=m; i++) ///建哈弗曼树  
    {  
        Select(HT,i-1,s1,s2);  
    
        ///在HT[1...i-1]选择parent为0且weight最小的两个结点,其序号分别为s1,s2  
        HT[s1].parent=i;  
        HT[s2].parent=i;  
        HT[i].lchild=s1;  
        HT[i].rchild=s2;  
        HT[i].parent=0;  
        HT[i].weight=HT[s1].weight+HT[s2].weight;  
    }  
    ///---从叶子到根逆向求每个字符的哈弗曼编码---  
    char * cd;  
    int c,f,start;  
    HC=(HCode)malloc((n+1)*sizeof(char *));///分配n个字符编码的头指针向量  
    cd=(char*)malloc(n*sizeof(char));///分配求编码的工作空间  
    cd[n-1]='\0';///编码结束符  
    for(i=1; i<=n; i++) ///逐个字符求哈弗曼编码  
    {  
        start=n-1;///编码结束符位置  
        for(c=i,f=HT[i].parent; f!=0; c=f,f=HT[f].parent) ///从叶子到根逆向求编码  
        {  
            if(HT[f].lchild==c) cd[--start]='0';  
            else cd[--start]='1';  
        }  
        HC[i]=(char*)malloc((n-start)*sizeof(char));///从cd复制编码到HC  
        strcpy(HC[i],&cd[start]);  
    }  
    free(cd);///释放工作空间  
}  

测试

  #include <iostream>
#include<string.h>
#include"Huffman.h"
using namespace std;
  int main(){
	  HTree HT=NULL;
	  HCode HC=NULL;
	  int w[10] = {1,2,3,4,5,6,7,8,9,10};
	  HuffmanCoding(HT, HC, w, 10);
	  for (int i = 1; i < 11; i++) {
		  cout << HC[i] << endl;
	  }
	  getchar();
  return 0;
  }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值