C++哈夫曼编码一例

这篇博客详细介绍了如何使用C++编程实现哈夫曼编码的过程,通过实例展示了数据的压缩和解压缩。文章涵盖了哈夫曼树的构建、编码生成以及编码的读取和还原等关键步骤。
摘要由CSDN通过智能技术生成


#include<stdio.h>

#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define MAXVALUE 200           /*权值的最大值*/
#define MAXBIT  30             /*最大的编码位数*/
#define MAXNODE 30             /*初始的最大的结点数*/
struct haffnode
{
    char data;
    int weight;
    int flag;
    int parent;       /*双亲结点的下标*/
    int leftchild;    /*左孩子下标*/
    int rightchild;   /*右孩子下标*/
};
struct haffcode
{
    int bit[MAXNODE];
    int start;        /*编码的起始下标*/
    char data;
    int weight;       /*字符权值*/
};
/*函数说明*/
/************************************************************************/
void pprintf(struct haffcode haffcode[], int n);
/*输出函数*/
void haffmantree(int weight[], int n, struct haffnode hafftree[], char data[]);
/*建立哈夫曼树*/
void haffmancode(struct haffnode hafftree[], int n, struct haffcode haffcode[]);
/*求哈夫曼编码*/
/************************************************************************/




void haffmantree(int weight[], int n, struct haffnode hafftree[], char data[])
/*建立叶结点个数为n,权值数组为weight[]的哈夫曼树*/
{
    int i, j, m1, m2, x1, x2;
/*哈夫曼树hafftree[]初始化,n个叶结点共有2n-1个结点*/
    for(i = 0; i &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值