Algorithm第四版算法 C++实现(二十九)——霍夫曼(Huffman)压缩(文件压缩机制)

霍夫曼压缩真的式一个非常经典又非常优雅的算法。不知道大家是否都会求霍夫曼树。哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。如果清楚的话,建议先去看一下霍夫曼树。他的原理是,将数据从小到大排序,将最小的两个生成一棵树,其权为二者的和。然后将其加入到队列中与其他的比较并继续此过程。
然后我们可以根据生成的树来获取前缀码。
在这里插入图片描述
然后用这些代码代替原本的数据。

class huffman
{
private:
	const static int r = 256;
	int freq[256] = {};
	std::string st[256];
	struct node
	{
		int index;
		int times;
		node *right;
		node *left;
		node()
		{

		};
		node(int a, int b, node *r, node *l)
		{
			index = a;
			times = b;
			right = r;
			left = l;
		}
	};
	node *create_node(int a, int b, node *r, node *l)
	{
		node *newnode = new node;
		newnode->index = a;
		newnode->times = b;
		newnode->right = r;
		newnode->left = l;
		return newnode;
	}
	node *x, *y,*p;
	static bool comp(node a, node b)
	{
		return a.times > b.times;
	}
	node built_tire()
	{
		std::vector<node> pq;
		for (int i = 0; i < r; i++)
		{
			if (freq[i] > 0)
			{
				//printf("%d ", i);
				node newnode(i,freq[i],nullptr,nullptr);
				pq.push_back(newnode);
			}
		}
		//printf("%d", pq.size());
		std::sort(pq.begin(), pq.end(), comp);
		while (pq.size()>1)
		{
			x = &pq.back();
			pq.pop_back();
			y = &pq.back();
			pq.pop_back();
			p = create_node(0, x->times + y->times, x, y);
			pq.push_back(*p);
			std::sort(pq.begin(), pq.end(), comp);
		}
		return pq.back();
	}
	void built_code(node x, std::string s)
	{
		if (x.left == nullptr&&x.right == nullptr)
		{
			st[x.index] = s;
			std::cout << x.index << std::endl;
			return;
		}
		//printf("%d %d %d", x.times, x.left->times,x.right->times);
		built_code( *x.left, s + '0');
		built_code( *x.right, s + '1');
	}
public:
	void compress(std::string s)
	{
		
		for (int i = 0; i < s.length(); i++)
		{
			freq[s[i]]++;
		}
		std::vector<node> pq;
		for (int i = 0; i < r; i++)
		{
			if (freq[i] > 0)
			{
				//printf("%d ", i);
				node newnode(i, freq[i], nullptr, nullptr);
				pq.push_back(newnode);
			}
		}
		//printf("%d", pq.size());
		std::sort(pq.begin(), pq.end(), comp);
		while (pq.size() > 1)
		{
			x = create_node(pq.back().index, pq.back().times, pq.back().left, pq.back().right);
			pq.pop_back();
			y = create_node(pq.back().index, pq.back().times, pq.back().left, pq.back().right);
			pq.pop_back();
			p=new node(0, x->times + y->times, x, y);
			pq.push_back(*p);
			std::sort(pq.begin(), pq.end(), comp);
		}
		node root = pq.back();
		//node root = built_tire();
		//node *t = root.left;
		built_code(root,"");
		std::cout << s << std::endl;
		for (int i = 0; i < s.length(); i++)
		{
			std::string code = st[s[i]];
			std::cout << code;
		}
	}
};
文本
  1. destructors of objects with static storage duration are called in reverse order of completion of their constructors or the completion of their dynamic initialization, and the functions passed to std::atexit are called in reverse order they are registered (last one first).
    a) any static objects whose initialization was completed before the call to std::atexit for some function F will be destroyed after the call to F during program termination.
    b) any static objects whose construction began after the call to std::atexit for some function F will be destroyed before the call to F during program termination (this includes the case where std::atexit was called from the constructor of the static object)

P.S.上述文字出自C++reference。

运行结果

在这里插入图片描述
看在孩子今天敲代码敲了四个多小时到23.26的面子上,给个赞吧

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值