CH7(2)哈夫曼树 (C++类模板)

Huffman.h

#pragma once
#ifndef HUFFMAN_H
#define HUFFMAN_H

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

struct HuffmanNode
{
	char data;
	double weight;
	int parent, lchild, rchild;
	vector<char>code;
};
class HuffmanTree
{
	vector<HuffmanNode>hufftree;
	int n;
public:
	HuffmanTree(vector<HuffmanNode>& leafs);
	void SelectSmall(int& least, int& less, int n);
	void GetCode();
	void Printcode();
	string Decode(vector<char> code);
};

HuffmanTree::HuffmanTree(vector<HuffmanNode>& leafs)
{
	int least, less;
	hufftree = leafs;
	n = leafs.size();
	hufftree.resize(2 * n - 1);
	for (int i = n; i < 2 * n - 1; i++)
	{
		least = less = i;
		SelectSmall(least, less, i);
		hufftree[least].parent = i;
		hufftree[less].parent = i;
		hufftree[i].weight = hufftree[least].weight + hufftree[less].weight;
		hufftree[i].lchild = least;
		hufftree[i].rchild = less;
		hufftree[i].parent = -1;
	}
}

void HuffmanTree::SelectSmall(int& least, int& less, int n)
{
	hufftree[n].weight = 100;
	for (int i = 0; i < n; i++)
	{
		if (hufftree[i].parent == -1)
		{
			if (hufftree[i].weight < hufftree[least].weight)
			{
				less = least;
				least = i;
			}
			else
				if (hufftree[i].weight < hufftree[less].weight)
					less = i;
		}
	}
}

void HuffmanTree::GetCode()
{
	for (int i = 0; i < (hufftree.size() + 1) / 2; i++)
	{
		int parent = hufftree[i].parent;
		int temp = i;
		while (parent != -1)
		{
			if (hufftree[parent].lchild == temp)
				hufftree[i].code.insert(hufftree[i].code.begin(), '0');
			else
				hufftree[i].code.insert(hufftree[i].code.begin(), '1');
			temp = parent;
			parent = hufftree[parent].parent;
		}
	}
}

void HuffmanTree::Printcode()
{
	for (int i = 0; i < (hufftree.size() + 1) / 2; i++)
	{
		cout << "符号" << hufftree[i].data << "的哈夫曼编码为:";
		for (int j = 0; j < hufftree[i].code.size(); j++)
			cout << hufftree[i].code[j];
		cout << endl;
	}
	cout << endl;
}

string HuffmanTree::Decode(vector<char> code)
{
	string target = "";
	int root = hufftree.size() - 1;
	int p = root;
	for (int i = 0; i < code.size(); i++)
	{
		if (code[i] == '0')
			p = hufftree[p].lchild;
		else
			p = hufftree[p].rchild;
		if (hufftree[p].lchild == -1 && hufftree[p].rchild == -1)
		{
			target = target + hufftree[p].data;
			p = root;
		}
	}
	return target;
}


#endif // !HUFFMAN_H


main.cpp

#include "Huffman.h"
using namespace std;

int main()
{
	int n;
	HuffmanNode node;
	vector<HuffmanNode> leafs;
	while (1)
	{
		cout << "请输入字母 enter 数字 enter,以@表示输入完成" << endl;
		cout << "1. EXIT" << endl;
		cin >> node.data;
		if (node.data == '1')
			exit(0);
		while (node.data != '@')
		{
			cin >> node.weight;
			node.parent = node.lchild = node.rchild = -1;
			leafs.push_back(node);
			cin >> node.data;
		}
		HuffmanTree tree(leafs);
		tree.GetCode();
		tree.Printcode();
		while (1)
		{
			cout << "Please Input to Decode" << endl;
			string str;
			int i = 0;
			cin >> str;
			vector<char> code;
			while (i < str.length())
			{
				code.push_back(str.at(i));
				i++;
			}
			cout << tree.Decode(code) << endl;
			code.clear();
			int x;
			cout << "2.Menu   3.New Decode" << endl;
			cin >> x;
			if (x == 2)
				break;
		}
		leafs.clear();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈哈笑死哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值