-1

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

字母表: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 */

}

这是程序的链接:算法

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

1 回答 1

0

您可以为每个符号存储节点。

huffheap  sym[256];

你初始化它。

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

如果 parent 为空,这是第一次,然后将其插入树中

于 2015-03-28T05:25:55.150 回答