Click here to Skip to main content
14,933,402 members
Articles / Programming Languages / C++
Posted 2 Jun 2004


54 bookmarked

Huffman Coding Class

Rate me:
Please Sign up or sign in to vote.
4.17/5 (24 votes)
6 Jun 2004CPOL2 min read
A file compressor class based on the Huffman Tree

Image 1



This version of file encoder and decoder program is based on the Huffman coding method. It explicitly demonstrates the details of the files during the encoding and decoding. The algorithm is encapsulated in a class En_Decode by the standard C++ language. Here is a demonstration project for the class.

Using the code

This class is implemented by standard C++, so it is very easy to use.

  1. Create an MFC application program as usual.
  2. Add the source file En_Decode.cpp and En_Decode.h to the project.
  3. Add the header file to your project by:
    //    #include "En_Decode.h"

    Define your own coder object, such as:

    //    En_Decode Coder;
  4. Use the member function of the class to set the source file name and destination encoded file name, then call Encode() to encode the source file.
    //    Coder.SetInputFileName("C:/test.txt", "txt");
    //    Coder.SetOutputFileName("C:/encoded");
    //    Coder.Encode();

In the same way, we can decode the encoded files to exactly the source file.

Class Members


En_DecodeConstructs a En_Decode object.

File Name Functions

SetInputFileNameSet the input file name.
SetOutputFileNameSet the output file name.
GetExtNameGet the ext name of the encoded file when decoding.

Paraneter Functions

GetMinFreqGet the minimum frequency of the symbols in the source file.
GetMaxFreqGet the maximum frequency of the symbols in the source file.
GetFreq(idx)Get the frequency value of a symbol in the source file.
GetActiveSymbolsGet the number of active symbols in the source file.
GetActualDataLengthGet the bytes number of the encoded file not including head.
GetUncodedFileLengthGet the length of the source file.
GetEncodedFileCodeLengthGet the average code length of the encoded file.
GetSourceFileEntropyGet the entropy of the source file.

Thanks to...

Derek Lakin for his help with CTabCtrlSSL in the demo project.

Contacting the Authors

This is the first time I share my code here. It is done in a hurry, so exception handling is ignored. Any one who is interested in it can enhance it freely, and please let me know about any improvement, thanks!

I am a junior student in Huangzhong University of Science and Technology, China. I am interested in C++ and MFC, and any coding project. I am glad to share my codes here with any one, though it is not that good.

Please contact me via email (


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
China China
I am a junior student in Huangzhong University of Science and Technology, China. I am interested in C++ and MFC, and any coding project.
I am glad to share my codes here with any one, though it is not that good. Please contact me via email (

Comments and Discussions

Questionfind some error Pin
bansal29454-Dec-08 9:09
Memberbansal29454-Dec-08 9:09 
GeneralMFC coding Pin
cuty_bujji22-Jul-08 1:53
Membercuty_bujji22-Jul-08 1:53 
QuestionIs there any other compressing algorithm more efficient than huffman coding Pin
wavebai9-May-07 22:05
Memberwavebai9-May-07 22:05 
QuestionHow to compress many files in folder? Pin
Newcc11-Oct-06 18:12
MemberNewcc11-Oct-06 18:12 
First, thank for your code.
But I don't known how to compress many files in a folder into one file compressed only, with extension of file is..., example: *.huf (similar as *.zip) . Program can allow you view files compressed in file *.huf and choice one file in that to uncompress it. It's similar Winzip or Winzar but it's simpler.
Can you help me?.
GeneralTab.h Pin
beardy janggut14-Mar-06 14:45
Memberbeardy janggut14-Mar-06 14:45 
GeneralRe: Tab.h Pin
cristitomi10-Mar-07 2:17
Membercristitomi10-Mar-07 2:17 
QuestionStreaming Compression? Pin
andrew235814-Jun-04 5:06
Memberandrew235814-Jun-04 5:06 
AnswerRe: Streaming Compression? Pin
kdkd14-Jun-04 6:14
Memberkdkd14-Jun-04 6:14 
GeneralRe: Streaming Compression? Pin
handychang15-Jun-04 18:19
Memberhandychang15-Jun-04 18:19 
AnswerRe: Streaming Compression? Pin
John M. Drescher16-Jun-04 5:39
MemberJohn M. Drescher16-Jun-04 5:39 
GeneralRe: Streaming Compression? Pin
Anonymous23-Jun-05 5:06
MemberAnonymous23-Jun-05 5:06 
GeneralRe: Streaming Compression? Pin
John M. Drescher23-Jun-05 5:34
MemberJohn M. Drescher23-Jun-05 5:34 
AnswerRe: Streaming Compression? Pin
Pharago19-Oct-06 11:44
MemberPharago19-Oct-06 11:44 
GeneralSuggestion ! Pin
NewGhost11-Jun-04 20:46
MemberNewGhost11-Jun-04 20:46 
GeneralRe: Suggestion ! Pin
handychang12-Jun-04 20:46
Memberhandychang12-Jun-04 20:46 
GeneralRe: Suggestion ! Pin
leandrobecker27-Aug-04 9:51
Memberleandrobecker27-Aug-04 9:51 
GeneralRe: Suggestion ! Pin
jazzwithshaz13-Jan-05 18:30
Memberjazzwithshaz13-Jan-05 18:30 
QuestionHow does Huffman coding work? Pin
AORD7-Jun-04 16:40
MemberAORD7-Jun-04 16:40 
AnswerRe: How does Huffman coding work? Pin
handychang7-Jun-04 20:55
Memberhandychang7-Jun-04 20:55 
GeneralRe: How does Huffman coding work? Pin
Amirhosein15-Dec-07 21:15
MemberAmirhosein15-Dec-07 21:15 
GeneralPicky!!! Pin
Paul Charles3-Jun-04 5:49
MemberPaul Charles3-Jun-04 5:49 
GeneralRe: Picky!!! Pin
Vadim Tabakman3-Jun-04 13:30
MemberVadim Tabakman3-Jun-04 13:30 
GeneralRe: Picky!!! Pin
Anonymous3-Jul-05 18:22
MemberAnonymous3-Jul-05 18:22 
GeneralRe: Picky!!! Pin
Vadim Tabakman3-Jul-05 18:45
MemberVadim Tabakman3-Jul-05 18:45 
GeneralRe: Picky!!! Pin
handychang3-Jun-04 15:20
Memberhandychang3-Jun-04 15:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.