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:
abbcccdddde
This is a string with a total of 5 characters. The weight of each character is the frequency at which it occurs.a
It is1,b
The weight value is2,c
The weighted value is3,d
The weighted value is4,e
The 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
。
c
Is that00
,d
Is that01
,a
is 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)