Help on Huffman trees 2 
Author Message
 Help on Huffman trees 2

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.


Tue, 25 Apr 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. HELP with Huffman Tree

2. HELP with Huffman Tree Traversal - huff2.c (1/1)

3. HELP with Huffman Tree Traversal - huff2.c (1/1)

4. HELP with Huffman Tree Traversal - huff2.c (0/1)

5. Help on Huffman trees

6. Huffman coding/huffman tree

7. HELP Huffman Tree Traversal

8. Smallest Huffman Binary Decoding Tree Header

9. huffman tree source code

10. huffman tree/coding

11. Need Help! Huffman Code

12. Help with AVL tree (balanced tree)

 

 
Powered by phpBB® Forum Software