哈夫曼树+哈夫曼树编码

 哈夫曼树

思路

实现方法为

1,构造全是根的森林

2,选择两棵权重较小的根构造出一棵新树

3,把权重较小的两棵树删除

4,重复2,3操作

 代码

#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
#define Maxsize 100

typedef struct	HFT
{
	int weight;
	int parent, Ltree, Rtree;
}HFT,*PHFT;

void Select(PHFT hf, int n, int& s1, int& s2)
{
	for (int i = 1; i < n; i++)//表示从前n个中寻找最小的,此处n的取值范围为n+1到2*n
		if (hf[i].parent == 0)//先找到父节点是0的即可以合并的
		{
			s1 = i;
			break;
		}
	for(int i=1;i<n;i++)
		if (hf[i].parent == 0&&hf[s1].weight>hf[i].weight)
		{
			s1 = i; 
		}

	for (int i = 1; i < n; i++)
		if (hf[i].parent == 0 && i != s1)//找到第二个要合并的节点的下标
		{
			s2 = i; break;
		}
	for (int i = 1; i < n; i++)//找到权值最小的,贪心策略
		if (hf[i].parent == 0 && hf[s2].weight > hf[i].weight && i != s1)
			s2 = i;
}

void InitHFT(PHFT& H, int n)
{
	if (n <= 1)return;
	int m = 2 * n - 1;
	H = new HFT[m+1];
	for (int i = 1; i <= m; i++)
	{
		H[i].parent = 0;
		H[i].Ltree = 0;
		H[i].Rtree = 0;//初始化根节点,左右子节点的索引为0,表示是一个森林,即一个元素是一棵树
	}
	cout << "请输入每个节点的权重:" << endl;
	for (int i = 1; i <= n; i++)
		cin >> H[i].weight;
	puts("");
	for (int i = n + 1; i <= m; i++)
	{
		int s1, s2;
		Select(H, i, s1, s2);//找到权值最小的两个节点下标
		H[s1].parent = i;
		H[s2].parent = i;
		H[i].Ltree = s1;
		H[i].Rtree = s2;//相当于删除这两个节点
		H[i].weight = H[s1].weight + H[s2].weight;//求出总的权值,继续进行合并
	}

}

void showHFT(PHFT& H, int n)
{
	cout << "index  weight  parent  LTree  RTree" << endl;
	cout << left;//输出左边对其
	int m = 2 * n - 1;
	for (int i=1; i <= m; i++)
	{
		cout << setw(5) << i << " ";//setw()设置间距
		cout << setw(6) << H[i].weight << " ";
		cout << setw(6) << H[i].parent << " ";
		cout << setw(6) << H[i].Ltree << " ";
		cout << setw(6) << H[i].Rtree << endl;
	}
}

int main()
{
	PHFT HFT;
	int n = 0;
	cout << "请输入节点的数量:";
	cin >> n;
	InitHFT(HFT, n);
	showHFT(HFT, n);
	return 0;
}

测试用例

输入

请输入节点的数量:7
请输入每个节点的权重:
7
19
2
6
32
3
21

输出

哈夫曼编码

思路 

        对于编码,我们先预处理出每个元素的出现频率,即权重,如上述构造,我们要让权重越大的离根越近。于是步骤与上述相同。只是此时我们需要多加维护一个路径的数组。

1,构建哈夫曼树表
2,一般规定左子树路径为0,右子树路径为1,按哈夫曼树表寻找parent结点直到为0(根节点),我们先创建一个辅助数组,先将其最后一个元素定为’\0’,因为此时是从叶子节点开始的,但是我们编码是从根节点开始,然后创建临时结点记录当前处理的结点。
3,将辅助数组值赋值给HC表,同时销毁辅助数组临时内存。
 

代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<stack>
#include<string>
using namespace std;
#define Maxsize 100

typedef struct	HFT
{
	float weight;
	int parent, Ltree, Rtree;
	string name;
}HFT,*PHFT;

void Select(PHFT hf, int n, int& s1, int& s2)
{
	for (int i = 1; i < n; i++)//表示从前n个中寻找最小的,此处n的取值范围为n+1到2*n
		if (hf[i].parent == 0)//先找到父节点是0的即可以合并的
		{
			s1 = i;
			break;
		}
	for(int i=1;i<n;i++)
		if (hf[i].parent == 0&&hf[s1].weight>hf[i].weight)
		{
			s1 = i; 
		}

	for (int i = 1; i < n; i++)
		if (hf[i].parent == 0 && i != s1)//找到第二个要合并的节点的下标
		{
			s2 = i; break;
		}
	for (int i = 1; i < n; i++)//找到权值最小的,贪心策略
		if (hf[i].parent == 0 && hf[s2].weight > hf[i].weight && i != s1)
			s2 = i;
}

void InitHFT(PHFT& H, const int& n)
{
	if (n <= 1)return;
	int m = 2 * n - 1;
	H = new HFT[m+1];
	for (int i = 1; i <= m; i++)
	{
		H[i].parent = 0;
		H[i].Ltree = 0;
		H[i].Rtree = 0;//初始化根节点,左右子节点的索引为0,表示是一个森林,即一个元素是一棵树
	}
	cout << "请输入每个节点的权重以及编码名称:" << endl;
	for (int i = 1; i <= n; i++)
		cin >> H[i].weight>>H[i].name;
	puts("");
	for (int i = n + 1; i <= m; i++)
	{
		int s1, s2;
		Select(H, i, s1, s2);//找到权值最小的两个节点下标
		H[s1].parent = i;
		H[s2].parent = i;
		H[i].Ltree = s1;
		H[i].Rtree = s2;//相当于删除这两个节点
		H[i].weight = H[s1].weight + H[s2].weight;//求出总的权值,继续进行合并
	}

}



void ch_crateHFMcode(const PHFT& hf, char** hc, const int& n)//char数组的
{
	char* temp = new char[n];
	temp[n - 1] = '\0';
	int start = 0, c = 0, father = 0;
	for (int i = 1; i <= n; i++)
	{
		start = n - 1;
		c = i;//当前处理的位置
		father = hf[i].parent;
		while (father != 0)//找到根节点就结束
		{
			if (hf[father].Ltree == c)temp[--start] = '0';//父节点的左子树是该节点,用0来表示
			else temp[--start] = '1';//不是左子树就是右子树
			c = father;//往上走
			father = hf[father].parent;//更新父节点
		}
		hc[i] = new char[n-start];//节省内存
		strcpy(hc[i], &temp[start]);
	}
	delete temp;//删除辅助节点
}

void str_crateHFMcode(PHFT& hf, string* hc, const int& n)
{
	string temp;
	stack<string>st;
	int cur = 0, father = 0;
	for (int i = 1; i <= n; i++)
	{
		cur = i;
		father = hf[i].parent;
		while (father != 0)
		{
			if (hf[father].Ltree == cur)st.push("0");//实际上就是一个栈,从末尾依次放进去,输出的时候从根节点依次输出
			else st.push("1");
			cur = father;
			father = hf[father].parent;
		}
		while (!st.empty())
		{
			temp += st.top();//拼接操作
			st.pop();
		}
		hc[i] = temp;
		temp.erase();//清除内存
	}
}

void showHFT(PHFT& H, char **HC,const int& n)
{
	//cout << "index  weight  parent  LTree  RTree" << endl;
	//cout << left;//输出左边对其
	//int m = 2 * n - 1;
	//for (int i = 1; i <= m; i++)
	//{
	//	cout << setw(5) << i << " ";//setw()设置间距
	//	cout << setw(6) << H[i].weight << " ";
	//	cout << setw(6) << H[i].parent << " ";
	//	cout << setw(6) << H[i].Ltree << " ";
	//	cout << setw(6) << H[i].Rtree << endl;
	//}
	//cout<<endl;
	cout << "  name  HFMcode" << endl;
	for (int i = 1; i <= n; i++)
	{
		cout << setw(5) << H[i].name << "  ";
		cout << setw(7) << HC[i] << "  " << endl;
	}

}
int main()
{
	PHFT HFT;
	int n = 0;
	cout << "请输入节点的数量:";
	cin >> n;
	char** HC = new char*[n];
	InitHFT(HFT, n);
	ch_crateHFMcode(HFT, HC, n);
	showHFT(HFT,HC,n);
	return 0;
}

测试用例 

输入 

节点数量   7

权重以及编码名称

0.4 A               
0.3 B       
0.15 C        
0.05 D          
0.04 E           
0.03 F        
0.03 G       

输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值