Introduction to Huffman Tree---java implementation

Source: Internet
Author: User

One, what is Huffman tree

is a two-fork tree with the shortest length of a weighted path, also known as the optimal binary tree

Weighted path length:wpl= (w1*l1+w2*l2+w3*l3+...+ wn*ln)

N a weighted value Wi (i=1,2,... N) form a tree with N a two-prong tree of leaf nodes. The path length of the corresponding leaf node is Li (i=1,2,... N).

ii. Establishment of Huffman tree

the weights of a known set of leaves W1,w2,w3......wn;
① first put n leaf nodes are considered n trees (two-fork trees with only one node). Think of them as a forest.


② combine the two trees with the smallest and lesser weights in the forest into a single tree. The weighted value of the root node is the sum of the weights of the two subtrees trees.

There are also n-1 trees in the forest .


③ repeated the first ② step until there is only one tree in the forest. This tree is the Huffman tree.

now give a group (n=4) detailed weights : 2 , 4 , 5 . 8, below is the construction of the detailed process:

The length of the right path of the Huffman tree composed of n leaves is unique. But the tree shape is not unique. Since the two trees in the forest are the smallest and the lesser of the sub-tree merging, which tree to do the left subtree, which tree does not strictly restrict the right sub-tree.

iii. Application of Huffman tree
a) Huffman code

the binary encoding used for communication using Huffman tree is called Huffman coding. The tree has a path from the root to each leaf node, and the branches on the path to the Zuozi are represented as "0" yards, and the branches pointing to the right subtree represent "1" yards. The sequence of "0" or "1" on each path is encoded as the corresponding character of each leaf node, that is Huffman code.

The corresponding Huffman codes of A,B,C,D were: 111. 10,110. 0


b) Two-way merge sort

If there are now n sorted files {d1,d2,.... dn}. The number of records included in each file corresponds to {w1,w2,w3.....wn}; the ability to merge all files into a large file with a 22 merge method makes all sorts of records in this file.

Q: What is the combination order to minimize the number of moves?

A: According to the structure of Huffman tree from the external node to the root node-by-layer merging, it must be a best combination of order.



Four, Huffman tree code implementation Huffman Tree implemented in Java
public class Huffman {public static void main (string[] args) {Huffman Huffman = new Huffman (); int[] A = {2,3,5,7,11,13,17,1 9,23,29,31,37,41}; System.out.println (a.length); Hftree tree = Huffman.createhftree (A.length, a); SYSTEM.OUT.PRINTLN ("HT ww parent lchild rchild"); for (int i=0;i<tree.node.length;i++) {System.out.println (i + "" + TR Ee.node[i].ww + "+ tree.node[i].parent +" "+ Tree.node[i].lchild +" "+ Tree.node[i].rchild);}} private static int maxint=10000;public hftree createhftree (int m, int a[]) {hftree hftree = new Hftree (m);/* Initializes Huffman tree */for (i NT i=0;i<2*m-1;i++) {hftree.node[i].lchild = -1;hftree.node[i].rchild = -1;hftree.node[i].parent = -1;if (i<m) { HFTREE.NODE[I].WW = A[i];}} /* Start generating Huffman tree */for (int i=0; i<m-1;i++) {int x1 = 0;int x2 = 0;int M1 = maxint;int m2 = maxint;for (int j=0; j<m+i;j++) {if (HFTREE.NODE[J].WW < m1 && Hftree.node[j].parent = =-1) {m2 = m1;x2 = X1;m1 = Hftree.node[j].ww;x1 = j;} else if (Hftree.node[j].ww < m2 && Hftree.node[j].parent = =-1) {m2 = hftree.node[j].ww;x2 = j;}} Hftree.node[x1].parent = M+i;hftree.node[x2].parent = M+i;hftree.node[m+i].ww = M1+m2;hftree.node[m+i].lchild = x1; Hftree.node[m+i].rchild = x2;} Hftree.root = 2*m-2;return Hftree;}}  class Hfnode{public int ww;public int parent;public int lchild;public int rchild;} class hftree{public hfnode[] node;  public int root;  public int m; Hftree (int m) {this.m = m; this.node = new hfnode[2*m-1];//Initializes an array of objects that must be created for each object for (int i=0;i<2*m-1;i++) {Node[i] = new H  FNode ();  } } }


Introduction to Huffman Tree---java implementation

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.