Click here to Skip to main content
15,358,440 members
Articles / Programming Languages / C#
Posted 20 Sep 2005


23 bookmarked

Implementing the Huffman algorithm as a C# library

Rate me:
Please Sign up or sign in to vote.
2.59/5 (15 votes)
20 Sep 20052 min read
Flexible HuffmanAlgorithm object, based on streams data forms.

Sample Image - Huffman_algorithm.jpg


Huffman algorithm is quite simple (In theory at least), the idea is based to the fact that in
most files, some bytes(characters if you will)  probably appears more times them others.

Main steps:

- Scan the data source from the begining till the end, list in a table bytes that appears and
  how many times(that is their value in the table).

-Now we need to build some kind of tree(you'll get it later), take the 2 bytes that appeared
 less times in the data source than others, create a parent node to both of them,
 remove them from the list and add the parent node to the list instead(the parent value will 
 be the sum of times both his childs values). Continue this process until the list is completely
 empty. The last parent you've created is the root node of the tree.

-We will give each byte that was in the file different value, the number of right and   
 left turns when walking from the root of the tree to a leaf is the number of bits that we
 will use as a new value to that leaf(byte) we will say left turn = 0 right turn = 1
 (or vice versa). All we  left to do is to replace raech byte in several bits (left and right 
 urns) in most cases this should cost less space.

-Extracting is easier (Save the original table we made as the start, first of all before 
  archiving).  Read the table, rebuild the tree from the table, read the bits and start taking
  right and left turns down the tree root, when getting to a leaf, read the original byte,
  save it somewhere else and start over from the tree root, reading the next bit...

HuffmanAlgorithm object

-Uses huffman algorithm to extract\archive any types of data stream.
 Archived data contains info about the original data size, version, password and more.

-Each extracting\archiving function has vesion thats pops event handler each time  one 
 percent of the process is over.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer Rontal Applications
Israel Israel
Born and raised in Israel, I've caught the programming virus at the age of 15.
Since than I can't stop coding.

Comments and Discussions

QuestionCopyright? Pin
User-12p3945623487623487522-Mar-13 4:59
MemberUser-12p3945623487623487522-Mar-13 4:59 
Questionhow to run whole project? Pin
swati.sonawane412-Apr-10 21:18
Memberswati.sonawane412-Apr-10 21:18 
QuestionHow to archived more than 1 file? Pin
pinochio_jc12-Jan-10 6:23
Memberpinochio_jc12-Jan-10 6:23 
AnswerRe: How to archived more than 1 file? Pin
JadBenAutho12-Jan-10 7:29
MemberJadBenAutho12-Jan-10 7:29 
GeneralRe: How to archived more than 1 file? Pin
pinochio_jc12-Jan-10 15:12
Memberpinochio_jc12-Jan-10 15:12 
Questionhow to run project Pin
farheen mauthoor28-Dec-09 20:49
Memberfarheen mauthoor28-Dec-09 20:49 
AnswerRe: how to run project Pin
JadBenAutho29-Dec-09 7:29
MemberJadBenAutho29-Dec-09 7:29 
GeneralPlz Help me Pin
MustafaZaidi7-Aug-09 21:32
MemberMustafaZaidi7-Aug-09 21:32 
AnswerRe: Plz Help me Pin
JadBenAutho8-Aug-09 6:00
MemberJadBenAutho8-Aug-09 6:00 
GeneralQuestion Pin
Trance Junkie21-Sep-05 4:14
MemberTrance Junkie21-Sep-05 4:14 
GeneralRe: Question Pin
JadBenAutho21-Sep-05 6:02
MemberJadBenAutho21-Sep-05 6:02 
Questionfiles? Pin
Huisheng Chen21-Sep-05 3:38
MemberHuisheng Chen21-Sep-05 3:38 
AnswerRe: files? Pin
JadBenAutho21-Sep-05 6:05
MemberJadBenAutho21-Sep-05 6:05 

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.