Huffman Tree of data structure (Java Implementation)-(V)

Source: Internet
Author: User

The so-called Huffman tree is required minimum weighted path length, what does this mean? In short, the path length (height-1) of all nodes is multiplied by the weight of the node, and the sum of these results is guaranteed to be minimal.

The most common application of Huffman tree is solving coding problems. In general, we use ASCII is a fixed-length encoding, for those commonly used characters, the use of a long length is a little wasted space.

The following is an example to build a Huffman coding tree.

Set character Set S={a,b,c,d,e,f}, character occurrence frequency w={2,3,5,7,9,12}, Huffman encoding character set

(1) Build 6 tree nodes with the node value of the frequency tree, save in a data set T

(2) Select the minimum two frequencies in the frequency set W, then add the result as the node value of the tree, build a new tree node, the two minimum values corresponding to the tree nodes, respectively, as the new node left and right children. Remove the nodes corresponding to the two minimum values from T, and finally put the new node in T.

(3) Repeat the 2nd step until only one node is left in T, then the node is the desired Huffman tree

In fact, easy to say, to achieve a little bit of trouble, the following Java implementation:

Tree node:

public class treenode<t>{public        treenode<t> Leftnode;        Public treenode<t> Rightnode;        Public T Data;public TreeNode (t data) {This.data=data;}}
Build Huffman Tree:

public class testtree{/* * Set s={a,b,c,d,e,f},w={2,3,5,7,9,12} */static hashmap<character, integer> map;public Testtree () {//TODO auto-generated constructor stub}public static void Main (string[] args) {character[] Character = {' A ', ' B ', ' C ', ' D ', ' E ', ' F '};int[] weight = {2, 3, 5, 7, 9, 12};//ordered or unordered are the same map=new Hashmap<character, integer> (); for ( int i=0;i<weight.length;i++) Map.put (Character[i], weight[i]); arraylist<treenode<integer>> nodes = new arraylist<treenode<integer>> (); for (int i = 0; i < we Ight.length; i++) {Nodes.Add (new treenode<integer> (Weight[i]));} while (true) {if (Nodes.size () <= 1) break;//find two smallest treenode<integer> Minnode = nodes.get (0);  treenode<integer> Sminnode = nodes.get (1); for (int i = 1; i < nodes.size (); i++) {treenode<integer> TempNode = Nodes.get (i); if (Minnode.data >=tempnode.data) {sminnode = Minnode;minnode = Tempnode;}} Nodes.remove (Minnode); Nodes.remove (Sminnode); Treenode<integer> NewNode = new Treenode<integer> (Minnode.data + sminnode.data); Newnode.leftnode = Minnode;newnode.rightnode = Sminnode;nodes.add (NewNode);} Treenode<integer>hafmantreenode=nodes.get (0); Gethalmancode (Hafmantreenode, "");} public static void Gethalmancode (treenode<integer>hafmantreenode,string blank) {if (hafmantreenode==null) Return if (hafmantreenode.leftnode==null&&hafmantreenode.rightnode==null) {System.out.println ("+" + Getcharacter (Hafmantreenode.data)); } else {System.out.print ("0"); Gethalmancode (hafmantreenode.leftnode,blank+ ""); System.out.print (blank+ "1"); Gethalmancode (hafmantreenode.rightnode,blank+ "");}} Gets the encoding of a character public static void Gethalmancode (Treenode<integer>hafmantreenode,character Character) {if ( Hafmantreenode==null) return; if (hafmantreenode.leftnode==null&&hafmantreenode.rightnode==null) {if (Getcharacter (HafmanTreeNode.data)                ==character) {System.out.print ("");}}} Get the character of the weight corresponding to public static CharacterGetcharacter (int weight) {set<map.entry<character, Integer>>set=map.entryset (); for (iterator< Map.entry<character, integer>> iterator=set.iterator (); Iterator.hasnext ();) {map.entry<character, Integer>entry=iterator.next (); if (Entry.getvalue () ==weight) {Map.Remove (Entry.getKey ( )); return Entry.getkey (); }} return null; }}
Results:

d:00

E:01

a:1000

b:1001

C:101

F:11

Huffman Tree of data structure (Java Implementation)-(V)

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.