Click here to Skip to main content
12,892,596 members (48,708 online)
Click here to Skip to main content

Tagged as


38 bookmarked
Posted 29 Aug 2010

Fast and Simple Huffman Compressor

, 2 Apr 2012 CPOL
Fast and simple huffman compressor
#include <memory.h>     // memset
#include <stdlib.h>     // qsort
typedef unsigned char BYTE;
class simple_Huffman{
	class node{
		node *left, *right;
		int count;       // ref. count
		int code;        // code in bits
		int codelength;  // code length
		BYTE chr;        // ASCII char, serves in compression and then in writing of frequencies
		node(void)  {memset(this, 0, sizeof(node));} // constructor, memset is faster than individ.
		~node() {
			if (left) {delete left; left = NULL;}
			if (right) {delete right; right = NULL;}
	node *trees[256];  // Array of trees, here will be only one tree at pos. 0 when finished with merging 
	node *leaves[256]; // array of leaves, each contains ASCII char,
	                   // ref. count, later code and codelength too. It is a reference to original trees
	node *trees_backup[256];
	int STEPS[256];    // as the tree is constructed, this stores exact steps (movings) of the last tree
	int stepscount;
	node nodes[256];

	void MakeTree();
	void MakeTreeD(); // maketree for decompression (no searching)
	int treescount;
	BYTE *allocatedoutput;
	void InitializeLeaves(node *, int, int);              // initialization of leaves with codes and code lengths
	static int comparefreq(const void *A, const void *B); // sorting, we want to sort descending
	void moveTreesRight(node **toTree);
	void moveTreesRight2(node **toTree);
	void tryToPop();
	void moveToTop();
	void performInternalCleanup();
	void initialize();
	int LastError;
	int Compress(BYTE *input, int inputlength);
	int Decompress(BYTE *input, int inputlength);
	int CompressFile(char *InFile, char *OutFile);
	int DecompressFile(char *InFile, char *OutFile);
	void Finalize(); // call after each stream compress/decompress to deallocate
	BYTE *getOutput(); // call after each stream compress/decompress to obtain output
	int getLastError();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Jan Mojzis
Software Developer (Junior) SOMI Systems
Slovakia Slovakia
Got 1st and 2nd degree from computer science in 2011 on university of Matej Bel, city of Banska Bystrica. Currently work for computer-oriented company SOMI Systems in Slovakia.
Working for SOMI Systems a.s.,

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 2 Apr 2012
Article Copyright 2010 by Jan Mojzis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid