C语言数据结构创建哈夫曼树-Huffman coding

该博客介绍了如何使用C语言构建哈夫曼树,包括创建树、合并最小节点、进行编码和解码的详细步骤。
摘要由CSDN通过智能技术生成

/*
*创建哈弗曼树
*创建树
*每次遍历最小的两个节点
*译码
*遍历树(解码的过程)
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define n  4//树的叶子节点数
#define m  (2*n-1)//树的节点总数
#define MAXVALUE 1000 //权重最大值
typedef struct htreetype
{
    int weight;//权重
    int parent;//双亲节点
    int Rchild;
    int Lchild;//左右孩子节点
    char name[5];//节点名称

}HtreeType;//树节点的结构体
typedef struct hcodetype
{
    char bit[n];//用于存放编码的数组
     char name[5];//储存节点的名称
     int start;//编码开始的下标

}HcodeType;//编码节点的结构体
void select_htree(HtreeType tree[],int position,int *node1,int *node2);//选择最小的两个
void htree_code(HtreeType tree[],HcodeType code[]);//译码的过程
void creat_htree(HtreeType tree[]);//哈夫曼树的创建
void show_htree(HcodeType code[] );//输出
int main()
{
    HtreeType tree[m];
    HcodeType code[n];
    htree_code(tree,code);
    show_htree(code);
    return 0;


}
void creat_htree(HtreeType tree[])//哈夫曼树的创建
{
    /*
      *初始化树的节点
      *给树的叶子节点赋值
      *利
  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值