Huffman coding tree

All data structures, algorithms and applications curricular templates please click: https://blog.csdn.net/weixin_44077863/article/details/101691360

Huffman tree is a minimum requirement of n external nodes and a binary tree expansion WPL

This extension is not a binary tree leaves empty expansion, but expansion of air roots, to the original n-node is to say empty leaves

WPL = Σ wi * li, WPL refers to a leaf node weighted sum of the path lengths, the sum is multiplied by the right point to the root of the distance

The idea is very simple algorithm

① right from the root to be greater the closer, the smaller the right to be farther away from the root

② leaf node up to find the roots did not go up once again on the increase at this point right, then right to point two nodes together to synthesize the root, continue to add up directly count like this root, that is, you can use such a root node to continue to do the synthesis

So very simple when implementing

A bunch of points

Take two synthetic minimal subtree, the root node is the sum of his son, and then back to the subtree pile points

This can be seen as the root of the subtree this pile of points and one point to the other point to make a judgment

Or elect two together look, repeated this operation until you close out a complete tree

For example as follows

Huffman tree is then talk application, is used for message encryption aspect, a binary number to represent a letter

Making represented neither ambiguity, it is the shortest

Then the frequency is simple to construct Huffman tree can be, and then we used the left and right 0 1 correspond to the letters to write out a binary string, a very simple, look at an example of it

Here we give Huffman tree achievements template code, the rest of the basic operations do not write

One thing forgot to say, is to look at n nodes requires n-1 merge the second son tree, well understood, do not say

template <class T>
class Node{
public:
	T x;
	Node<T> *l,*r,*pa;
	Node():l(0),r(0),pa(0){}
	Node(T x,Node<T> *l=0,Node<T> *r=0,Node<T> *pa=0):x(x),l(l),r(r),pa(pa){}
};
template <class T>
class cmp{//注意如果堆里是对指针排序的话,Node里重载小于号就不好用了,你就必须得写cmp了 
public:
	bool operator()(Node<T> *a,Node<T> *b){
		return a->x>b->x;
	}
};
template <class T>
class HuffmanTree
{
private:
	Node<T>* root;                               //Huffman树的树根
public:	
	HuffmanTree(T w[], int n);
	void merge(Node<T> *a,Node<T> *b,Node<T>* pa);
};
template<class T>
void HuffmanTree<T>::merge(Node<T> *a,Node<T> *b,Node<T>* pa){
	pa->x=a->x+b->x;
	pa->l=a;
	pa->r=b;
	a->pa=pa;
	b->pa=pa;
}
template<class T>
HuffmanTree<T>::HuffmanTree(T w[], int n){
	priority_queue<Node<T>*,vector<Node<T>*>,cmp<T>> q;
	Node<T> *pa,*a,*b,*tmp;
	for(int i=0;i<n;i++){
		tmp=new Node<T>(w[i]);
		q.push(tmp);
	}
	for(int i=0;i<n-1;i++){
		a=new Node<T>;
		b=new Node<T>;
		pa=new Node<T>;
		a=q.top();q.pop();
		b=q.top();q.pop();
		merge(a,b,pa);
		q.push(pa);
		root=pa;
	}
}

 

Published 49 original articles · won praise 0 · Views 1706

Guess you like

Origin blog.csdn.net/weixin_44077863/article/details/103181700