Algorithmic Learning-Huffman Coding (C + + implementation)

Source: Internet
Author: User

Huffman code

Huffman coding is simple, but it is a very important coding method, it solves a very important problem of data storage: Compression! Its coding is optimal, lossless, especially in the image used very much. Here's how it works.

Encoding method

Huffman coding is based on the weight of the size of the implementation. The Huffman tree is constructed according to the weights, and then the Huffman tree is reversed to find the encoding method of each node.

For example:
abbcccddddeThis is a string with a total of 5 characters. The weight of each character is the frequency at which it occurs.aIt is1bThe weight value is2cThe weighted value is3dThe weighted value is4eThe weighted value is1。 In normal coding, it means that 5 letters are at least 3 bits, which is 3bit. Then this string of characters requires 1*3+2*3+3*3+4*3+1*3=33 bits. And the use of Huffman coding it?

The process of constructing Huffman tree is to find the minimum weight of two each time, put together, make up a tree, the root node is the value of the and.
The first step:

 2/1 1  

Such a weight of 1 a and e has been constructed. And put the current 2 back to the weight list, below the smallest two is this 2 and b 2 .

           4         /           2     2       /        1    1

So the second step is finished, then put 4 back to the weight list, continue to find, and so on

      ...省略ing

Huffman Tree of the results:

 11/7 4/\ /\ 3 4 2 2/1 1  

Well, this is the ultimate Huffman tree. See no ~ Each leaf node, all represents a character,1 is a and e . 2 Yes b , in turn ... Omit ing

Then Traverse, left is0, right is1
cIs that00,dIs that01,ais the100,b=11,e=101. This will be a success.

Verify the results below: Calculate storage space. From a to e : 3*1+2*2+2*3+2*4+3*1 = bits. Found no, a whole 9 bits, what's the concept? That is, the compression of nearly 1/3 Ah, if 3G size, compression is 2G ah.
Since this is so good, the following code implementation is attached.

Code implementation

main.cpp//huffmancode////Created by Alps on 14/11/22.//Copyright (c) 2014 Chen.    All rights reserved.//#include <iostream>using namespace std;typedef struct htnode{int weight;    int parent;    int lchild; int rchild;} Htnode, *huffmantree;typedef char** huffmancode;void Select (huffmantree HT, int num, int &child1, int &child2); VO    ID huffmancoding (huffmantree &ht, Huffmancode &hc, int *w, int n) {//int m,i;    int child1,child2;    if (n <= 1) {return;         } m = number of nodes n*2-1;//whole tree HT = (huffmantree) malloc ((m+1) * sizeof (Htnode));//apply enough space for (i = 1; I <= n; i++,w++) {    Ht[i] = {*w, 0, 0, 0};    } for (; I <= m; i++) {Ht[i] = {0, 0, 0, 0};        } for (i = n+1; I <= m; i++) {Select (ht,i-1,child1,child2);        Ht[child1].parent = i;        Ht[child2].parent = i;        Ht[i].lchild = Child1;        Ht[i].rchild = child2;        Ht[i].weight = Ht[child1].weight + ht[child2].weight; PriNTF ("%d==%d\n", child1,child2);        } HC = (char**) malloc ((n+1) *sizeof (char *));    Char *CD = (char*) malloc (n*sizeof (char));//memset (CD, ' n ', n*sizeof (char));        int c = 0;    int tempparent,count;        for (i = 1; I <= n; i++) {count = 0; for (c = i,tempparent = Ht[i].parent; Tempparent! = 0;c=tempparent, tempparent = ht[tempparent].parent) {if (H            T[tempparent].lchild = = c) {cd[count++] = ' 0 ';            }else{cd[count++] = ' 1 ';        }} cd[count]= ' + ';        printf ("%s~%d\n", cd,i);                Hc[i] = (char *) malloc ((count) *sizeof (char)); strcpy (Hc[i], CD);//memset (CD, ' + ', n*sizeof (char));//error}}void Select (huffmantree HT, int num, int &chi    ld1, int &child2) {child1 = 0;    child2 = 0;    int w1 = 0;    int w2 = 0; for (int i = 1; I <= num; i++) {if (ht[i].parent = = 0) {if (child1 = = 0) {child1 =            I    W1 = Ht[i].weight;            Continue                } if (child2 = = 0) {child2 = i;                W2 = Ht[i].weight;            Continue                } if (W1 > W2 && W1 > Ht[i].weight) {w1 = ht[i].weight;                Child1 = i;            Continue                } if (W2 > W1 && W2 > Ht[i].weight) {w2 = Ht[i].weight;                Child2 = i;            Continue    }}}}int Main (int argc, const char * argv[]) {char a[] = "Abcaab";    int i = (int) strlen (a);        printf ("%d\n", I);    int b[]={1,2,3,4};    Huffmantree HT;    Huffmancode HC;    Huffmancoding (HT, HC, B, 4);    for (i = 1; I <= 7; i++) {printf ("%d-%d\n", ht[i].weight,ht[i].parent);    } for (i = 1; I <=4; i++) {printf ("%s\n", Hc[i]); } return 0;}


Algorithmic Learning-Huffman Coding (C + + implementation)

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.