-1

i am writing right now program thats should encode/decode data from file.

Alphabet: 8-bit ASCII codes
So i have n = 256 symbols in this alphabet, max number of heaps is 2n-1 = 511

I understand algorithm of adaptive huffman coding but i have some problems with implement this.

I wrote first part of my code, but i need some help to go on good way.

#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 */

}

Here is the link to procedure: Algorithm

  1. Is my tree structure is correct?
  2. How i should store the alphabet? I wrote a structure about code but i dont know what to do with list of symbols -> struct huffcode
  3. I know how i can increment weight of heaps etc. but i cant go further. I have problem with "Seen this symbol before/Is the first time of this symbol in tree"
  • Your tree store symbols First time / already seen => just walk into the tree to search the symbol For example http://en.wikipedia.org/wiki/Tree_traversal. – Ôrel Mar 28 '15 at 05:14

1 Answers1

0

You can store for each symbol the node.

huffheap  sym[256];

You initialize it.

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

If parent is null, this is the first time then you insert it into the tree

Ôrel
  • 7,044
  • 3
  • 27
  • 46