用Huffman树实现文本的压缩

</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//huffman.h
#ifndef HUFFMAN_H
#define HUFFMAN_H

#include<string>
#include<map>

using namespace std;

class huffman
{
public:
	huffman ();
	huffman ( char , int );
	huffman ( map<char , int> );
	huffman ( huffman* , huffman * );
	huffman ( const huffman& );

	friend void dfs ( huffman );

	int degree;
	string code;
	char data = 0;
	huffman *pLeft = nullptr;
	huffman *pRight = nullptr;

	friend bool operator<( const huffman huf1 , const huffman huf2 );
};

#endif

<pre name="code" class="cpp">//huffman.cpp
#include"huffman.h"
#include<queue>

huffman::huffman ()
{
	pLeft = nullptr;
	pRight = nullptr;
}

huffman::huffman ( const huffman& huf )
{
	this->code = huf.code;
	this->data = huf.data;
	this->degree = huf.degree;
	this->pLeft = huf.pLeft;
	this->pRight = huf.pRight;
}

huffman::huffman ( char dat , int deg )
{
	data = dat;
	degree = deg;
	pLeft = nullptr;
	pRight = nullptr;
}

huffman::huffman ( map<char , int> s )
{
	huffman *huf;
	huffman *huf1;
	huffman *huf2;
	priority_queue<huffman> que;
	for ( auto &i : s )
	{
		que.push ( huffman ( i.first , i.second ) );
	}

	while ( que.size () > 1 )
	{
		huf1 = new huffman ( que.top () );
		que.pop ();
		huf2 = new huffman ( que.top () );
		que.pop ();
		huf = new huffman ( huf1 , huf2 );
		huf1 = nullptr;
		huf2 = nullptr;
		que.push ( *huf );
	}

	huffman root ( que.top () );
	que.pop ();

	dfs ( root );
	*this = root;
}

huffman::huffman ( huffman* huf1 , huffman* huf2 )
{
	this->degree = huf1->degree + huf2->degree;
	if ( huf1->degree > huf2->degree )
	{
		this->pLeft = huf2;
		this->pRight = huf1;
	}
	else
	{
		this->pLeft = huf1;
		this->pRight = huf2;
	}
}

void dfs ( huffman huf )
{
	if ( huf.pLeft&& huf.pLeft->degree > 0 )
	{
		huf.pLeft->code = huf.code + "0";
	}
	if ( huf.pRight && huf.pRight->degree > 0 )
	{
		huf.pRight->code = huf.code + "1";
	}
	if ( huf.pLeft&&huf.pLeft->degree > 0 )
	{
		dfs ( *huf.pLeft );
	}
	if ( huf.pRight&& huf.pRight->degree > 0 )
	{
		dfs ( *huf.pRight );
	}
}

bool operator<( const huffman huf1 , const huffman huf2 )
{
	return huf1.degree > huf2.degree;
}

<pre name="code" class="cpp">//hc:Huffman Compression
//main.cpp
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include"huffman.h"

using namespace std;

string btod ( string bin );
void getcode ( huffman huf );

map<char , string> mret;

int main ( void )
{
	ifstream input ( "input.txt" , ifstream::in );
	ofstream output ( "output.hc" );

	map<char , int> charcount;
	map<char , string> charcode;

	char ch;
	while ( input >> ch )
	{
		charcount [ ch ]++;
	}
	input.sync ();
	input.clear ();
	input.close ();
	input.open ( "input.txt" , ifstream::in );
	huffman huf ( charcount );
	getcode ( huf );
	string tot;
	//	charcode = mret;
	while ( input >> ch )
	{
		tot += mret [ ch ];
	}
	output << btod ( tot );


	system ( "pause" );
	return 0;
}

string btod ( string bin )
{
	while ( bin.size () % 7 != 0 )
	{
		bin = bin + "0";
	}

	string decs;

	for ( auto it = bin.begin (); it != bin.end (); )
	{
		int decn = 0;
		int len = 7;
		for ( int i = 0; i < 7; i++ , it++ )
		{
			if ( *it == '1' )
			{
				decn += pow ( 2 , len - 1 );
			}
			len--;
		}
		decs += decn;
	}
	return decs;
}

void getcode ( huffman huf )
{
	if ( huf.pLeft == nullptr && huf.pRight == nullptr )
	{
		mret [ huf.data ] = huf.code;
	}
	else
	{
		if ( huf.pLeft )
		{
			getcode ( *huf.pLeft );
		}
		if ( huf.pRight )
		{
			getcode ( *huf.pRight );
		}
	}
}
点赞