473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Huffman encoding in C

Hello everyone, i'm writing a C program that will read a text file and then encode the characters using huffman algorithm. So far i've read the files, built a tree, and now I'm building the strings to be used in encoding, Where i'm having problem is my BuildStrings function, instead of printing the 0s and 1s I appended to the array it outputs a series of 0s and 1s. Below is the BuildStrings function and the main function.

Expand|Select|Wrap|Line Numbers
  1. /*This funtion will build strings to be used in Huffman encoding
  2. It is recursive in nature and will traverse the tree while appending 
  3. 0 to left paths and 1 to right paths. */
  4.  
  5. void BuildStrings(sTreenode *r)
  6. {
  7.     //printf("I depeth is: %i\n",r->weight);    
  8.     if ((r->LChild == NULL) && (r->RChild == NULL))        //found a leaf yet?
  9.     {strcpy(cStrings[(((int) r->data) & 0xff)],cPath);    //if yes, copy the path to the string to the 2D array cStrings
  10.     iDepth--;                                            //go back up a level
  11.     cPath[iDepth] = '\0';
  12.         return;
  13.     }
  14.     else                                                //
  15.     {
  16.         iDepth++;                                        //otherwise go down a level
  17.         cPath[iDepth] = '0';                            //append 0 before taking the left path
  18.         BuildStrings(r->LChild);                        //calls the left child
  19.         cPath[iDepth] = '\0';
  20.  
  21.         iDepth++;                                        //
  22.         cPath[iDepth] = '1';                            //append 1 before taking the right path
  23.         BuildStrings(r->RChild);                        //calls the right child
  24.         cPath[iDepth] = '\0';
  25.     }
  26.     iDepth--;
  27.     return;                                                //return to the main function
  28. }
  29.  
  30.  
  31. /*Main program*/
  32. int main()
  33. {
  34.     iDepth = 0;                                            // tells us what level of the tree we're currently in
  35.     for (int i=0; i<=255; i++)    cPath[i]='\0';            //initializing cPath to nulls
  36.     for (int i=0; i<=255; i++) for(int j=0; j <= 255; j++)    
  37.         cStrings[i][j]='\0';                            //see above
  38.     float percent[255];                                    //array to hold the percentage frequency of each xter
  39.  
  40.     total = CountChars(filename);                        //calls CountChars which counts the number of xters and their weight
  41.  
  42.     printf("\nThe Total is: %d\n", total);                //Prints the total no of characters read
  43.     printf("\n N    Frequency   Percentage\n");
  44.     printf(" ---  ---------   ----------\n");
  45.  
  46.     for(int x= 0; x < 255;x++)                            //loop to print out all the characters
  47.     {
  48.         percent[x] = (iCharCount[x]* 100.0)/total ;        //computes the frequency percentage of each character
  49.         if(iCharCount[x]){                                //if xter is non-zero, print it
  50.         //printf("\n%3d  %8d   %8.3f",x,iCharCount[x],percent[x]);    //prints the frequency of each array index
  51.         }
  52.     }
  53.  
  54.     BuildTree();                                        //calls the funtion to build trees    
  55.     BuildStrings(root);
  56.     //traverse(root);    
  57.     printf("\n\nThe huffman code for the characters is as follows:\n");
  58.  
  59.     for(int f= 0; f <= 255; f++)
  60.     {
  61.         printf("\n%3i    %4i", f, cStrings[(((int) f) & 0xff)]);
  62.     }
  63.     return 0;    
  64. }
  65.  
I'd appreciate your assistance.
Oct 10 '08 #1
0 5364

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dot | last post by:
Hi all, I have written a Python huffman Encoding Module, for my own amusement. I thought it might be educational/entertaining for other people, so I've put it on my website and wrote about it a...
8
by: dirgesh | last post by:
I am having a hard time making a Program in C/C++ that uses the Huffman Compression to compress a file. I have a file "Hello World" That i need to compress. can someone please give me an...
8
by: james | last post by:
I have ran into a little snag on an old database application I am converting. Out of 63 tables(all seperate files) , two of them are compressed using Huffman Compression. I have been searching all...
15
by: aarklon | last post by:
Hi all, this is the program which I saw in my colleagues book. the program was run on turbo C++ 3.0 compiler /*beginning of header file huff.h*/ #ifndef _HUFF_H_ #define _HUFF_H_
4
by: A_StClaire_ | last post by:
hi all, I'm trying to implement Huffman coding on letters of a string. I've written functions to define the initial leaf nodes (letters and their frequencies) and to sort them. I've also put...
3
by: Udhay | last post by:
Sir, I am udhay. I am a student and i am working on Audio compression for my exam. I want to know how does the compression take place in audio?? Can anyone suggest a link for the novice to...
11
by: KWSW | last post by:
The other thread got too long so decided to start a new one. Got my huffman tree working and am now moving onto encoding and decoding my given text file. My idea now is to print out all the leaf...
5
by: recordlovelife | last post by:
So i have written a code to encode a string of a select amount of letters into huffman code. #include <iostream> #include <string> using namespace std; string Huffman(char letter);...
1
by: Esqulax | last post by:
Hiya guys. im after a bit of guidance to be honest My task: Create a program that generates huffman codewords for a 16 symbol alphabet, the input should be the probabilities of each symbol...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.