Algorithm learning-Java coding (c ++) and Java Coding
User ID
Although the coding method is simple, it is a very important encoding method. It solves a very important problem of data storage: Compression! Its encoding method is optimal and lossless, especially in many images. The following describes how it works.
Encoding Method
The construction of the Harman encoding is based on the size of the weight. First, construct a user-defined tree based on the weights, and then perform reverse traversal on the user-defined tree to find the encoding method of each node.
For example:
abbcccddddeThis is a string with a total of five characters. The weight of each character is the occurrence frequency.aYes1,bThe weight is2,cThe weight is3,dThe weight is4,eThe weight is1. In normal encoding, it indicates that five letters must contain at least three digits, that is, 3 bits. this string requires 1*3 + 2*3 + 3*3 + 4*3 + 1*3 = 33 bits. What about the coding of the user's account?
The process for constructing a user-defined tree is to find the two with the smallest weights and put them together to form a tree. The root node is the sum of weights.
Step 1:
2 / \ 1 1
In this way, the weight is 1aAndeThe structure is complete. And put the current 2 back into the weight list. The smallest two below are2AndbValue2.
4 / \ 2 2 / \ 1 1
In this way, the second step is constructed, and 4 is put back into the weight list, continue searching, and so on.
... Omitting ing
The result of the Harman tree:
11 / \ 7 4 / \ / \ 3 4 2 2 / \ 1 1
Well, this is the final Al User-Defined tree. No ~ Each leaf node represents a single character,1YesaAnde.2Yesb, In turn... omitting ing
Then traverse, and the left is0, Right is1.
cIs00,dIs01,aYes100,b=11,e=101.
The following verification result: the computing bucket. SlaveaToe: 3*1 + 2*2 + 2*3 + 2*4 + 3*1 = 24 bits. I didn't find it. I lost a full 9 bits. What is the concept? That is to say, the compression size is close to 1/3. If the size is 3 GB, the compression size is 2 GB.
In this case, the code implementation is attached below.
Code Implementation
</Pre> <pre name = "code" class = "cpp"> /// 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); void HuffmanCoding (HuffmanTree & HT, HuffmanCode & HC, int * w, int n) {// int m, I; int child1, child2; if (n <= 1) {return ;} m = n * 2-1; // number of nodes in the entire tree HT = (HuffmanTree) malloc (m + 1) * sizeof (HTNode )); // apply for sufficient space for (I = 1; I <= n; I ++, w ++) {HT [I] = {* w, 0, 0, 0 };}for (; I <= m; I ++) {HT [I] = {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, '\ 0 ', 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 (HT [tempParent]. lchild = c) {cd [count ++] = '0';} else {cd [count ++] = '1 ';}} cd [count] = '\ 0'; printf ("% s ~ % D \ n ", cd, I); HC [I] = (char *) malloc (count) * sizeof (char); for (int j = count; j> = 0; j --) {HC [I] [count-j] = cd [J-1];} // memset (cd, '\ 0 ', n * sizeof (char); // error} void Select (HuffmanTree HT, int num, int & child1, 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 ;}