The greatest gain in learning Huffman coding is to learn the use of priority queues in STL and the issues to be aware of when using them: when using custom data types, priority queues overload their comparison operators.
About Huffman tree How to explain, see the introduction of the algorithm, the principle is really simple, but to write the complete code is the difficulty is the use of priority queue. No nonsense, ah, again stressed, want to make clear the principle, see the introduction of the algorithm, the tree on the explanation of what is more than the online garbage explain how much clearer, a look to understand. -----------------can finally get on the code.
To deposit pointer-type nodes in the priority queue #include<iostream> #include <queue> #include <string>using namespace Std;class Comapre;class node{public:friend comapre;int Key;char ch; Node* left; node* right;public:node (int num, char c): Key (num), CH (c), left (null), right (null) {}////bool LessThan (const node* Node) CO Nst//{//return Key>node->key;//}};class comapre{public://cannot overload the comparison operator directly in the node class because of a pointer passed in the priority queue Everybody go to see the relationship between the priority queue usage and the heap in STL bool Operator () (Node*node1, node*node2) {//bool flag = Node1->lessthan (node2);//return Flag;return Node1->key < node1->key;}};/ /Use priority queue to store elements, the priority queue ensures that the top of the queue is the smallest element node* Huffman (priority_queue<node*, Vector<node*> comapre > Que) {//Repeat the top two elements out of the merge and put them in the queue while (Que.size () >1) {node *node = new Node (0, 0); node->left = Que.top (); Que.pop (); node->right = Que.top (); Que.pop (); node->key = Node->left->key + node->right->key; Que.push (node);} return Que.top ();} Using the method of middle order traversal to Output Huffman code//The idea is that each left refers to a node s add 0, each right refers to a node to add 1, each iteration to exit the last element of S//used in STring pop_back function void Inorder (node* node, string s) {if (Node = = NULL) {return;} Else{if (Node->left! = NULL) {s + = ' 0 ';} Inorder (Node->left, s); if (Node->left = = Null&&node->right = NULL) {cout << node->ch << "' s code is:", for (auto i:s) cout << i;cout << Endl;} S.pop_back (); if (node->right! = NULL) s + = ' 1 '; Inorder (node->right, s);}} Emptying the open space, otherwise causing a memory leak void Delete (Node*node) {if (Node = = NULL) {return;} Delete (Node->left);D elete (node->right);d elete node;} int main () {string S; NODE*N[6];N[0] = new node (5, ' f '); n[1] = new node (9, ' e '); n[2] = new node (+, ' d '); n[3] = new node (n, ' C '); n[4] = new Nod E (' B '); n[5] = new Node (' a ');p riority_queue<node*, Vector<node*>, Comapre>qu;int i;for (i = 0; i<6; i++) {Qu.push (n[i]);} node* r = Huffman (qu), inorder (R, s);D elete (r); return 0;}
--------------Huffman coding for the introduction of algorithms