Huffman 树

Huffman树:又称最优二叉树,是加权路径长度最短的二叉树    由于每次都要找最小的两个数,所以用最小堆方便求解
#pragma once
#include
#include
using namespace std;
template
struct Less
{
	bool operator()(const T& l,const T& r)
	{
		return l
struct Great
{
	bool operator()(const T& l,const T& r)
	{
		return l>r;
	}
};
template>
class Heap
{
public:
	Heap()
	{}
	Heap(T* a,int size)
	{
		a.reserve(size);
		for(int i=0;i=0;--i)
		{
			AdjustDown(i,size);
		}
	}
	void Push(const T& x)
	{
		a.push_back(x);
		AdjustUp(a.size()-1);//插入尾部,在从最后一个开始向上调整
	}
	void Pop()//取出最小的元素
	{
		assert(!a.empty());
		swap(a[0],a[a.size()-1]);
		a.pop_back();
		if(a.size()>1)
		{
			AdjustDown(0,a.size());
		}
	}
	size_t Size()
	{
		return a.size();
	}
	bool Empty()
	{
		return a.empty();
	}
	const T& Top()
	{
		assert(!a.empty());
		return a[0];
	}
protected:
	void AdjustDown(int root,int size)
	{
		assert(!a.empty());
		int parent=root;
		int child=parent*2+1;
		while(child0)
		{
			int parent=(child-1)/2;
			if(Compare()(a[child],a[parent]))
			{
				swap(a[child],a[parent]);
				child=parent;
			}
			else
			{
				break;
			}
		}
	}
private:
	vector a;
};

#pragma once
#include
#include"heap.cpp"
#include
using namespace std;
template
struct HuffmanNode
{
    T _weight;
	HuffmanNode* _left;
	HuffmanNode* _right;
	HuffmanNode* _parent;
	HuffmanNode(const T& weight)
		:_weight(weight)
		,_left(NULL)
		,_right(NULL)
		,_parent(NULL)
	{}
};
template
class HuffmanTree
{
public:
	HuffmanTree()
		:root(NULL)
	{}
	void CreateHuffmanTree(T *a,size_t size,const T& invalid=T())
	{
		//最小堆的比较方式
		struct Compare
		{
			bool operator()(HuffmanNode*& l,HuffmanNode*& r)
			{
				return l->_weight < r->_weight;
			}
		};
		//将所有值构造成结点放入最小堆中,仿函数做比较器
		Heap*,Compare> minHeap;
		for(size_t i=0;i* node=new HuffmanNode(a[i]);
				minHeap.Push(node);
			}
		}
		//根据Huffman算法,从堆里取出最小的两个数,删除,将这两个数构成的结点加入堆中
		HuffmanNode* frist=NULL;
		HuffmanNode* second=NULL;
		HuffmanNode* parent=NULL;
		while(minHeap.Size()>1)
		{
			frist=minHeap.Top();
			minHeap.Pop();
			second=minHeap.Top();
			minHeap.Pop();
			parent=new HuffmanNode(frist->_weight+second->_weight);
			parent->_left=frist;
			parent->_right=second;
			frist->_parent=parent;
			second->_parent=parent;
			minHeap.Push(parent);
		}
		root=minHeap.Top();
	}
	//层序遍历打印二叉树
	void Level()
	{
		queue*> q;
		HuffmanNode* cur=root;
		q.push(cur);
		while(!q.empty())
		{
			HuffmanNode* front=q.front();
			q.pop();
			cout<_weight<<" ";
			if(front->_left)
				q.push(front->_left);
			if(front->_right)
				q.push(front->_right);
		}
		cout<* GetRoot()
	{
		return root;
	}
	~HuffmanTree()
	{
		if(root!=NULL)
		{
			Destory(root);
		}
    }
protected:
	void Destory(HuffmanNode* root)
	{
		if(root==NULL)
			return;
		HuffmanNode* cur=root;
		Destory(cur->_left);
		Destory(cur->_right);
		delete cur;
		cur=NULL;
	}
private:
	HuffmanNode* root;
};
 void TestHuffmanTree()
{
	int a[10] = {2, 3, 6, 0 ,4, 5, 1, 9, 7, 8};
	HuffmanTree tree;
	tree.CreateHuffmanTree(a, 10, -1);
	tree.Level();
}
 int main()
{
	TestHuffmanTree();
	system("pause");
	return 0;
}

你可能感兴趣的:(数据结构)