As a communicator, he has been in the information theory at the undergraduate level, and the graduate student has continued to work on coding. During the interview, the interviewer said a Hoffman tree, and as a communicator he forgot. Somewhat justified.
Theoretical knowledge
The most fundamental principle of the Huffman algorithm is that the cumulative (the encoded length of the character's statistical character) is minimal, that is, the weight ( the encoding length of the character's statistical character) and the minimum. This encoding can achieve the effect of compression. Also known as the best binary tree.
Specific can refer to the Left ear mouse blog: http://coolshell.cn/articles/7459.html is very image.
Realize
Mainly include: construction tree, encoding, decoding. Show Me Your code:
Package Com.zy.huffman;import Java.nio.charset.charset;import Java.util.*;class Tree {Node root; PublicNodeGetroot() {returnRoot } Public void Setroot(Node Root) { This. root = root; }}class Node implements comparable<node> {String chars ="";intFrequence =0; Node parent; Node Leftnode; Node Rightnode; Public int CompareTo(Node N) {returnFrequence-n.frequence; } PublicBooleanIsLeaf() {returnChars.length () = =1; } PublicBooleanIsRoot() {returnParent = =NULL; } PublicBooleanIsleftchild() {returnParent! =NULL&& This= = Parent.leftnode; } Public int getfrequence() {returnFrequence; } Public void setfrequence(intFrequence) { This. frequence = frequence; } PublicStringGetChars() {returnChars } Public void SetChars(String chars) { This. chars = chars; } PublicNodegetParent() {returnParent } Public void setParent(Node Parent) { This. parent = parent; } PublicNodeGetleftnode() {returnLeftnode; } Public void Setleftnode(Node Leftnode) { This. Leftnode = Leftnode; } PublicNodeGetrightnode() {returnRightnode; } Public void Setrightnode(Node Rightnode) { This. Rightnode = Rightnode; }} Public classHuffmantree {//Statistics number of occurrences of the data character Public StaticMap<character, integer>Statistics(Char[] chararray) {map<character, integer> Map =NewHashmap<character, integer> (); for(CharC:chararray) {Character Character =NewCharacter (c);if(Map.containskey (character)) {map.put (character, map.Get(character) +1); }Else{Map.put (character,1); } }returnMap }//Build tree Private StaticTreeBuildtree(Map<character, integer> statistics, list<node> Leafs) {character[] keys = Statistics.keyset (). ToArray (Newcharacter[0]); Priorityqueue<node> Priorityqueue =NewPriorityqueue<node> (); for(Character Character:keys) {Node node =NewNode (); Node.chars = Character.tostring (); Node.frequence = statistics.Get(character); Priorityqueue.add (node); Leafs.add (node); }intSize = Priorityqueue.size (); for(inti =1; I <= Size-1; i++) {Node Node1 = Priorityqueue.poll (); Node Node2 = Priorityqueue.poll (); Node Sumnode =NewNode (); Sumnode.chars = Node1.chars + node2.chars; Sumnode.frequence = node1.frequence + node2.frequence; Sumnode.leftnode = Node1; Sumnode.rightnode = Node2; Node1.parent = Sumnode; Node2.parent = Sumnode; Priorityqueue.add (Sumnode); } Tree tree =NewTree (); Tree.root = Priorityqueue.poll ();returnTree }//Coding Public StaticStringencode(String originalstr, Map<character, integer> statistics) {if(Originalstr = =NULL|| Originalstr.equals ("")) {return ""; }Char[] Chararray = Originalstr.tochararray (); List<node> leafnodes =NewArraylist<node> (); Buildtree (statistics, leafnodes); Map<character, string> encodinfo = Buildencodinginfo (leafnodes); StringBuffer buffer =NewStringBuffer (); for(CharC:chararray) {Character Character =NewCharacter (c); Buffer.append (Encodinfo.Get(character)); }returnBuffer.tostring (); }Private StaticMap<character, string>Buildencodinginfo(list<node> leafnodes) {map<character, string> codewords =NewHashmap<character, string> (); for(Node leafnode:leafnodes) {Character Character =NewCharacter (Leafnode.getchars (). CharAt (0)); String codeword =""; Node CurrentNode = Leafnode; Do{if(Currentnode.isleftchild ()) {codeword ="0"+ codeword; }Else{codeword ="1"+ codeword; } CurrentNode = Currentnode.parent; } while(Currentnode.parent! =NULL); Codewords.put (character, codeword); }returncodewords; }//decoding Public StaticStringDecode(String binarystr, Map<character, integer> statistics) {if(Binarystr = =NULL|| Binarystr.equals ("")) {return ""; }Char[] Binarychararray = Binarystr.tochararray (); Linkedlist<character> binarylist =NewLinkedlist<character> ();intsize = Binarychararray.length; for(inti =0; i < size; i++) {Binarylist.addlast (NewCharacter (Binarychararray[i])); } list<node> leafnodes =NewArraylist<node> (); Tree tree = Buildtree (statistics, leafnodes); StringBuffer buffer =NewStringBuffer (); while(Binarylist.size () >0) {node node = tree.root; Do{Character c = Binarylist.removefirst ();if(C.charvalue () = =' 0 ') {node = Node.leftnode; }Else{node = Node.rightnode; } } while(!node.isleaf ()); Buffer.append (Node.chars); }returnBuffer.tostring (); }//Test Public Static void Main(string[] args) {String Oristr ="I love Somebody's name is Xiaofang"; Map<character, integer> statistics = statistics (Oristr.tochararray ()); String encodedbinaristr = encode (oristr, statistics); String decodedstr = decode (encodedbinaristr, statistics); System. out. println ("Original sstring:"+ oristr); System. out. println ("Huffman encoed binary string:"+ encodedbinaristr); System. out. println ("decoded string from Binariy string:"+ decodedstr); System. out. println ("binary string of Us-ascii:"+ Getstringofbyte (oristr, Charset.forname ("Us-ascii"))); } Public StaticStringGetstringofbyte(String str, Charset Charset) {if(str = =NULL|| Str.equals ("")) {return ""; }byte[] ByteArray = Str.getbytes (charset);intsize = Bytearray.length; StringBuffer buffer =NewStringBuffer (); for(inti =0; i < size; i++) {bytetemp = Bytearray[i]; Buffer.append (Getstringofbyte (temp)); }returnBuffer.tostring (); } Public StaticStringGetstringofbyte(byteb) {StringBuffer buffer =NewStringBuffer (); for(inti =7; I >=0; i--) {bytetemp = (byte) ((b >> i) &0x1); Buffer.append (string.valueof (temp)); }returnBuffer.tostring (); }}
Result is:
Original sstring:i Love Somebody' s name is XiaofangHuffman encoedbinary string:001101110011000111011111011110100011011110111110011011111000110101110101010100001101111101001111010101001001000100010 000000101110110Decodedstring fromBinariystring: I Love Somebody' s name is Xiaofangbinary string ofUS-ASCII:011010010010000001101100011011110111011001100101001000000111001101101111011011010110010101100010011011110110010001111 0010010011101110011001000000110111001100001011011010110010100100000011010010111001100100000011110000110100101100001011011 1101100110011000010110111001100111
Program Reference: http://blog.csdn.net/kimylrong/article/details/17022319
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Huffman Tree Java Implementation