018-huffman Tree-greed-algorithmic design skills and analysis M.H.A study notes

Source: Internet
Author: User

Huffman The Tree is a fully binary tree, with a larger weight node closer to the root.

Huffman encoding is a coding method, which is based entirely on character The probability to construct a code word with the shortest average length of a different word head .

Basic ideas:

The process of establishing a Huffman tree:

If there is n n a leaf knot. n w1 , w2 , ... , wn

(1) W1,W2,... , WN seen as having N the forest of a tree ( only one node per tree ) ;

(2) The tree merging of the minimum weights of the two root nodes in the forest is used as the left and right subtree of a new tree, and the root node weights of the new tree are the sum of their left and right subtree node weights;

(3) Remove the two selected trees from the forest and add the new tree to the forest;

(4) Repeat (2) , (3) step, until there is only one tree left in the forest, the tree is the Huffman tree obtained.

We use the smallest heap when choosing the least-weighted node.

We need to get the specific code of each letter, we only need to be on all the left son's side label 0, the right son's side label 1, from the root node to the target node of all the passing edge of the code is the letter encoding.

Algorithm Analysis:

Suppose there are n characters.

Inserting all the characters into the heap requires θ(n), removing two elements from the heap and adding a new element that requires O (log n). Repeat n-1 times, so the total time complexity is O (n log n).

Pseudo Code:


C++Code:

Calculates the number of bits of text under Huffman coding and compares it with the fixed length code. #include <iostream> #include <string> #include <cstring> #include <iomanip> #include <cstdio > #include <queue>//Huffman tree, implementing using namespace with Priority Queue Std;int main () {string S;while (cin >> s) {if         (s = = "END") Break;int len = S.size (); int date[30] = {0}; The date array records the frequency priority_queue<int>q;for of each character in text (int i = 0; i < len; i++) {if (s[i] = = ' _ ') Date[0]++;else date[s[i ]-' A ' + 1]++;}      for (int i = 0; i < i++) {if (date[i]!=0) Q.push (-date[i]); Only the frequency of the different characters is added to the priority queue, the characters themselves are not related to the topic}//processing makes small data priority high int ans = 0;int Tem;while (!q.empt     Y ()) {tem =-q.top (); Take out the smallest two numbers, add up to ans, and join the queue, processing to no number Q.pop () in the queue, if (!q.empty ()) {tem = Tem-q.top (); Q.pop ();} Ans = ans + tem;if (!q.empty ()) Q.push (-tem); If the queue has no data, then do not add (the last two, or one), without this step, the above Whlie judgment is not established. } int ans8 = Len << 3;double bi = (double) ans8/ans;printf ("%d%d%.1lf\n", ans8, ans, bi);}}


018-huffman Tree-greed-algorithmic design skills and analysis M.H.A study notes

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.