When developing mobile apps, you always want to compress the information transmitted over the network to reduce traffic. This article is only guided by the Harman encoding to implement compression functions.
You may think that the program and a third party provide many compression methods. Why write the compression code by yourself? Good, there are many compression tools such as gzip, but in some cases (such as text content is small and the characters are not repeated), Gzip compression will be larger than the original text. Therefore, you can use your own compression method in some special cases.
You may have forgotten the knowledge of Homan at school. You can first learn about Homan At Baidu encyclopedia.
Http://baike.baidu.com/view/127820.htm
Husky's idea: Calculate the repetition rate of text characters, obtain the weight of each character, and then construct an optimal binary tree (also known as the husky tree), and then generate a bit for each leaf node) the unit code value. Each code value cannot be used as the prefix of other code values. Then, the code value is combined to generate one byte for every eight.
Package COM. huffman;/*** node * @ author davee */public class node implements comparable <node> {int weight; // weight node leftchild; // left child node rightchild; // right child node string huffcode; private Boolean isleaf; // whether it is leaf character value; Public node (character value, int weight) {This. value = value; this. weight = weight; this. isleaf = true;} public node (INT weight, node leftchild, node rightchild) {This. weight = weight; this. leftchild = leftchild; this. rightchild = rightchild;} public void increaseweight (int I) {weight + = I;} public Boolean isleaf () {return isleaf;} @ override public int compareto (node O) {return this. weight-o. weight ;}}
Package COM. huffman; import Java. math. biginteger; import Java. util. arraylist; import Java. util. collections; import Java. util. hashmap; import Java. util. map; import Java. util. treemap; public class huffmantree {private Boolean DEBUG = false; private hashmap <character, node> nodemap; private arraylist <node> nodelist; Public huffmantree () {nodemap = new hashmap <character, node> (); nodelist = new arraylist <No De> ();} public void setdebug (Boolean Debug) {This. DEBUG = debug;} Public String decode (Map <string, character> codetable, string binary) {int begin = 0, end = 1, Count = binary. length (); stringbuffer sb = new stringbuffer (); While (end <= count) {string key = binary. substring (begin, end); If (codetable. containskey (key) {sb. append (codetable. get (key); begin = end;} else {} end ++;} return s B. tostring ();} Public String encode (string origintext) {If (origintext = NULL) return NULL; calculateweight (origintext); // If (Debug) printnodes (nodelist ); node root = generatehuffmantree (nodelist); generatehuffmancode (root, ""); If (Debug) printnodes (Root); stringbuffer sb = new stringbuffer (); For (character key: origintext. tochararray () {sb. append (nodemap. get (key ). huffcode);} If (Debug) System. out. println ("binary:" + sb. tostring (); return sb. tostring ();}/*** calculate the leaf weight * @ Param text */private void calculateweight (string text) {for (character C: text. tochararray () {If (nodemap. containskey (c) {nodemap. get (c ). increaseweight (1); // weight plus 1} else {node leafnode = new node (C, 1); nodelist. add (leafnode); nodemap. put (C, leafnode) ;}}/ *** generate a Harman tree * @ Param nodes */private node generat Ehuffmantree (arraylist <node> nodes) {collections. sort (nodes); While (nodes. size ()> 1) {node Ln = nodes. remove (0); node Rn = nodes. remove (0); insertsort (nodes, new node (LN. weight + rn. weight, LN, RN);} node root = nodes. remove (0); nodes = NULL; return root;}/*** insert sort * @ Param sortednodes * @ Param node */private void insertsort (arraylist <node> sortednodes, node node) {If (sortednodes = NUL L) return; int Weight = node. weight; int min = 0, max = sortednodes. size (); int index; If (sortednodes. size () = 0) {Index = 0;} else if (weight <sortednodes. get (min ). weight) {Index = min; // insert to first} else if (weight> = sortednodes. get (max-1 ). weight) {Index = max; // insert to the end} else {Index = max/2; for (INT I = 0, Count = max/2; I <= count; I ++) {If (weight> = sortednodes. get (index-1 ). weight & weight <Sortednodes. get (index ). weight) {break;} else if (weight <sortednodes. get (index ). weight) {max = index;} else {min = index;} Index = (MAX + min)/2;} sortednodes. add (index, node);} private void generatehuffmancode (node, string code) {If (node. isleaf () node. huffcode = Code; else {generatehuffmancode (node. leftchild, code + "0"); generatehuffmancode (node. rightchild, code + "1 ");}} /*** Generate code table ** @ return */Public Map <string, character> getcodetable () {Map <string, character> map = new hashmap <string, character> (); for (node: nodemap. values () {map. put (node. huffcode, node. value);} return map;}/*** print node information * @ Param root */private void printnodes (node root) {system. out. println ("letter weight"); printtree (Root);} private void printtree (node root) {If (root. isleaf () sys TEM. Out. println (root. value = NULL? "": Root. Value) + "" + root. Weight + "" + (root. huffcode = NULL? "": Root. huffcode); If (root. leftchild! = NULL) printtree (root. leftchild); If (root. rightchild! = NULL) printtree (root. rightchild);}/*** print node information * @ Param nodes */private void printnodes (arraylist <node> nodes) {system. out. println ("letter weight"); For (node: nodes) {system. out. println (node. value + "" + node. weight + "" + node. huffcode );}}}
Package COM. test; import Java. util. map; import COM. huffman. huffutils; import COM. huffman. huffmantree; public class test {public static void main (string [] ARGs) {string origintext = "abcdacaha"; huffmantree = new huffmantree (); huffmantree. setdebug (true); // test string binary = huffmantree. encode (origintext); byte [] bytes = huffutils. binary2bytes (Binary); Map <string, character> codetable = Hu Ffmantree. getcodetable (); int lastbytenum = binary. length () % 8; system. out. println (bytes. length); // pass bytes, codetable, and lastbytenum to the server side // omit ...... /* The server parses the received parameters and converts them to bytes, relationmap, and lastbytenum */string fullbinary = huffutils. bytes2binary (bytes, lastbytenum); system. out. println ("server binary:" + fullbinary); string retrievetext = huffmantree. decode (codetable, fullbinary); system. out. println ("Restore text:" + retrievetext );}}