C 自适应(动态)哈夫曼编码:对文件中的数据进行编码和解码

C 自适应(动态)哈夫曼编码:对文件中的数据进行编码和解码,c,huffman-code,data-compression,C,Huffman Code,Data Compression,我现在正在写一个程序,它应该对文件中的数据进行编码/解码 字母表:8位ASCII码 在这个字母表中有n=256个符号,最大堆数是2n-1=511 我了解自适应哈夫曼编码的算法,但我在实现这一点上有一些问题 我写了我代码的第一部分,但我需要一些帮助才能顺利进行 #define FIRST_NYT 511 struct huffcode { int nbits; int code; }; typedef struct huffcode code; struct huffheap

我现在正在写一个程序,它应该对文件中的数据进行编码/解码

字母表:8位ASCII码
在这个字母表中有n=256个符号,最大堆数是2n-1=511

我了解自适应哈夫曼编码的算法,但我在实现这一点上有一些问题

我写了我代码的第一部分,但我需要一些帮助才能顺利进行

#define FIRST_NYT 511

struct huffcode {
    int nbits;
    int code;
};

typedef struct huffcode code;

struct huffheap {
    char symbol;    /* character contained in tree node */
    int weight; /* number of times symbol has occured in file so far */
    int order;  /* ordering system, root has order number 511 */

    struct huffheap *left;
    struct huffheap *right;
    struct huffheap *parent;

};

typedef struct huffheap *heap;


static heap heap_create(int symbol, struct heap *root){
    /* if tree is empty, add root */
    if(heap = NULL) {
        heap = (huffheap*)malloc(sizeof(*heap));
        heap- >symbol = /* i dont know what i should when heap doesnt has a symbol like NYT */
        heap- >weight = 0;
        heap- >order = FIRST_NYT;
        heap- >left = NULL;
        heap- >right = NULL;
        heap- >parent = NULL;
    }

    /* TO_DO: check -> is the first time of this symbol ? */

    /* if yes -> add node */

}
以下是程序的链接:

  • 我的树结构正确吗
  • 我应该如何存储字母表?我写了一个关于代码的结构,但我不知道如何处理符号列表->struct huffcode
  • 我知道如何增加堆的重量等,但我不能走得更远。我对“以前见过此符号/是此符号第一次出现在树中”有疑问

  • 可以为每个符号存储节点

    huffheap  sym[256];
    
    你可以初始化它

    for (int i = 0; i < 256; i++) {
        sym[i].symbol = i;
    }
    
    for(int i=0;i<256;i++){
    sym[i].symbol=i;
    }
    

    如果parent为null,则这是您第一次将其插入树中

    您的树存储符号first time/ready seen=>例如,只需走进树中搜索符号即可。