哈夫曼树各种函数

哈夫曼树各种函数,第1张

哈夫曼树各种函数

HuffmanTree.h

#ifndef HUFFMANTREE_H
#define HUFFMANTREE_H
#include
#include
using namespace std;
struct HuffmanNode
{
	char data;
	double weight;
	int parent,lchild,rchild;
};
class HuffmanTree
{
private:
	vector hufftree;
	int n;
public:
	HuffmanTree(vector&leafs);
	~HuffmanTree(){}
	vector GetCode(int i);
	string Decode(vector &source);
	void SelectSmall(int &least,int &less,int n);
};
#endif

HuffmanTree.cpp

#include
#include
#include
#include"HuffmanTree.h"
using namespace std;
void HuffmanTree::SelectSmall(int &least,int &less,int n)
{
	int i = 0;
	while (hufftree[i].parent!=-1 && i=least && hufftree[j].weight &leafs)
{
	int i;
	int n=leafs.size();
	hufftree.resize(2*n-1);
	for(i=0;i HuffmanTree::GetCode(int i)
{
	vector code;
	int p=i;
	int parent=hufftree[i].parent;
	while(parent!=-1)
	{
		if (hufftree[parent].lchild==p)
			code.insert(code.begin (),0);
		else
			code.insert (code.begin (),1);
		p=parent;
		parent=hufftree[parent].parent;
	}
	return code;
}
string HuffmanTree::Decode(vector&source)
{
	string target="";
	int root=hufftree.size ()-1;
	int p=root;
	int i;
	for(i=0;i 

main.cpp

#include
#include
#include
#include"HuffmanTree.h"
using namespace std;
void main()
{
	vector  a;
	HuffmanNode a1;
	int i,j;
	char c;
	double w;
	cout<<"请输入元素值(5个数据与权值):"<>c>>w;
		
		a1.data = c;
		a1.weight = w;
		a.push_back(a1);
	}
	HuffmanTree t(a);
	vector num;
	string s;
	for (i =0;i<5;i++)
	{
		num = t.GetCode(i);
		cout<<"第"<					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691989.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存