Quote:
> How could I build a table of codes from a Huffman's tree?
> (the tree is a binary tree built with pointers.)
> After have built the table, how do I set the different bits together
> in a file? Please send me a function in Borland-C
> or a theoretical base or a site where I could find useful
> information.
> Thanks,
> Giovanni
> *****************************
> * Giovanni *
> * Catapano *
> *****************************
Simple: output 0-bit for left-branch, 1 bit for right branch
(or the other way around, whatever you like).
And here's a nice way for writing bits:
#include <stdio.h>
#include <limits.h>
void output_bit(FILE *fp, int bit, int reset)
{
static unsigned char byte;
static int bit_count;
switch (reset) {
case 0: /* Initialising */
byte = 0;
bit_count = 0;
break;
case 1: /* Write a bit */
byte = byte << 1;
byte = byte | (bit != 0);
bit_count++;
if (bit_count == CHAR_BIT) {
fwrite (&byte, 1, 1, fp);
byte = 0;
bit_count = 0;
}
break;
case 2: /* Closing down */
if (bit_count != 0) {
byte = byte << (CHAR_BIT - bitcount);
fwrite (&byte, 1, 1, fp);
byte = 0;
bit_count = 0;
}
break;
}
Quote:
}
Marco.
---------------------
Swearing is the only language spoken proficiently by programmers.