Introduction to algorithms ------------ Huffman Encoding

Source: Internet
Author: User

Introduction to algorithms ------------ Huffman Encoding

The biggest achievement of learning Huffman encoding is to learn how to use priority queues in STL and how to use them: when using custom data types, priority Queues must reload their own comparison operators.


For how to explain the Huffman tree, please refer to the introduction to algorithms. The principle is really simple, but the difficulty to write the complete code lies in the use of priority queues. I am not talking nonsense. I want to clarify the principle again. Please refer to the introduction to algorithms. The explanation on the tree is much clearer than that on the Internet. ----------------- The code is finally available.

// Store the pointer type nodes in the priority queue # include
 
  
# Include
  
   
# Include
   
    
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) const // {// return key> node-> key; //}; class Comapre {public: // the pointer passed in the priority queue, therefore, you cannot directly reload the comparison operator in the Node class. // you can check the relationship between priority queues in STL and heap. bool operator () (Node * node1, Node * node2) {// bool flag = node1-> lessthan (node2); // return flag; return node1-> key <node1-> key ;}}; // use the priority queue to store elements. The priority queue can ensure that the top of the queue is the smallest Node * Huffman (priority_queue
    
     
, Comapre> que) {// repeat the top two elements and merge them into 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 () ;}// output the Hoffmann code using the method of sequential traversal // the idea is to add 0 to each node s to the left and 1 to each node to the right, exit the last element of s at the end of each iteration // use the string pop_back function void Inorder (Node * node, string s) {if (node = NULL) {return ;} else {if (node-> l Eft! = 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) ;}// clear the opened space, otherwise, it will cause memory leakage void Delete (Node * node) {if (node = NULL) {return;} Delete (node-> left); Delete (node-> right ); delete node;} int main () {string s; Node * n [6]; n [0] = new Node (5, 'F '); n [1] = new Node (9, 'E'); n [2] = new Node (16, 'D'); n [3] = new Node (12, 'C'); n [4] = new Node (13, 'B'); n [5] = new Node (45, 'A'); priority_queue
     
      
, Comapre> qu; int I; for (I = 0; I <6; I ++) {qu. push (n [I]);} Node * R = Huffman (qu); Inorder (R, s); Delete (R); return 0 ;}
     
    
   
  
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.