Graphics and text detailed Java Implementation Huffman tree _java

Source: Internet
Author: User

Objective

I want to learn the data structure of the small partners must know Huffman, the great God invented the famous "Best binary Tree", in order to commemorate him, we call it "Huffman tree." Huffman tree can be used for Huffman coding, coding words can be a great knowledge, such as for compression, for cryptography and so on. Let's take a look at what Huffman tree is today.

Concept

Of course, one of the routines, first we need to understand some basic concepts.

1. path Length: the branch from one node to another in the tree forms the path of these two nodes, and the number of branches on the path is called the path length.

2, the length of the tree path: from the root to each node of the path length of the sum, we call the complete binary tree is the shortest path length of the two-fork tree.

3, the length of the tree with the right path: If you assign a weight to each leaf node of a tree, then the length of the tree's weighted path equals the sum of the path length of the root node to all leaf nodes and the product of the leaf node weights.

So how do we judge whether a tree is the best binary tree, and first look at the following tree:

Their weighted lengths were:

wpl1:7*2+5*2+2*2+4*2=36

Wpl2:7*3+5*3+2*1+4*2=46

Wpl3:7*1+5*2+2*3+4*3=35

It is obvious that the third tree has the shortest path of the right (unbelief of the small partners can try, if we can find a shorter one, I think we can get the Turing prize, which is what we call the "Optimal Binary Tree" (Huffman Tree), which is simple to build, and then select the node with the smallest weights in order to put it at the bottom of the tree. To make the smallest two connections a new node, it should be noted that the weights of the new nodes should be equal to the sum of the weights of the two nodes, then we'll put this new node back in the node where we need to make the tree, so that the Huffman tree, all the nodes where the information is stored, is on the leaf node.

When the concept is finished, it may be a little partner or an "unknown sensation".

Here's an example to build on.

There is a string: aaaaaaaaaabbbbbaaaaaccccccccddddddfff

The first step, we first count the number of occurrences of each character, called the character of the weight value. A, b 5, C 8, D 6, f 3.

The second step is to find the two characters with the smallest face weights, B5 and F3, to build the node.

Then the F3 and B5 are removed and now the A15,C8,D6,FB8.

In the third step, repeat the second step until you have only one node left to build.

Now it's DFB14,A15,C8.

At last

OK, so our Huffman tree on the construction completed.

Steps to build

According to the above logic, summed up, is a few steps:

1. Count the occurrences of characters and characters in the string;

2. Create nodes according to the structure of the first step;

3. The node weight value ascending sort;

4. Take out two nodes with the smallest weights and generate a new parent node;

5. Delete the two nodes with the smallest weights, and store the parent nodes in the list;

6. Repeat the 45th step until a node is left;

7. Assign the last node to the root node.

Java code

The principle is finished, followed by the code implementation.

First you need a node class to store the data.

Package Huffman;
/**
 * Node class
 * @author Yuxiu
 * */public
class Node {public
 String code;//node's Huffman encoding public
 int codesize;//Node Huffman encoded length public
 String data;//node's data public
 int count;//node's weights public nodes
 lchild;
 Public Node rchild;

 Public node () {
 } public

 node (String data, int count) {
  this.data = data;
  This.count = count;
 }

 public Node (int count, node lchild, node Rchild) {
  this.count = count;
  This.lchild = Lchild;
  This.rchild = rchild;
 }

 Public Node (String data, int count, node lchild, node Rchild) {
  this.data = data;
  This.count = count;
  This.lchild = Lchild;
  This.rchild = rchild;
 }

Then there is the process of implementation.

Package Huffman;
Import java.io.*;

Import java.util.*; public class Huffman {private String str;//the string that was originally used for compression private string newstr = "";//Huffman encoded string private Node root;/ /Huffman binary tree root node private Boolean flag;//The latest character whether the existing label private arraylist<string> charlist;//store different characters of the queue same word Fu Cun in the same position PR Ivate arraylist<node> nodelist;//Storage node Queue 15 16/** * Build Huffman Tree * * @param str/public void CREATHFMT
  REE (String str) {this.str = str;
  CharList = new arraylist<string> ();
  nodelist = new arraylist<node> (); 1. Number of occurrences of characters and characters in a statistical string//The basic idea is to place a disordered string such as ababccdebed into the charlist, Aa,bbb,cc,dd,ee//And the length of the string in the list is the corresponding weight for (int i = 0; I < str.length ();
   i++) {Char ch = str.charat (i);//Remove the character from the given string flag = true; for (int j = 0; J < Charlist.size (); j + +) {if (Charlist.get (j). charAt (0) = ch) {//If the same character String s = char is found
     List.get (j) + ch;
     Charlist.set (J, s);
     Flag = false;
    Break } if (flag) {CharList. Add (Charlist.size (), ch + ""); }//2. According to the structure of the first step, create the node for (int i = 0; i < charlist.size (); i++) {String data = Charlist.get (i). CharAt (0) + " "; Gets the first character int count = Charlist.get (i) of each string in the charList. Length (); The length of the string in the list is the corresponding weight node node = new node (data, count); Create node Object Nodelist.add (I, node);
  Add to Node Queue}//3. Sort the node weights in ascending order (nodelist); while (Nodelist.size () > 1) {//when the number of nodes is greater than a moment//4. Remove the two nodes with the smallest weights, generating a new parent node//5. Delete the two nodes with the smallest weights, and store the parent node in the list node left = N
   Odelist.remove (0);
   Node right = nodelist.remove (0);
   int parentweight = left.count + right.count;//parent node weight equals node parent = new Node (parentweight, left, right); Nodelist.add (0, parent);
 Place the parent node first}//6. Repeat step 45th, which is the while loop//7. Assigns the last node to the root node root = nodelist.get (0);  /** * Ascending Sort * @param nodelist */public void Sort (Arraylist<node> nodelist) {for (int i = 0; i < Nodelist.size ()-1; i++) {for (int j = i + 1; j < Nodelist.size (); j + +) {Node temp;
     if (Nodelist.get (i). Count > Nodelist.get (j). count) {temp = Nodelist.get (i);
     Nodelist.set (I, Nodelist.get (j));
    Nodelist.set (J, temp); /** * Traverse * @param node * node * */public void output (node node) {if node.lchild!= null
  ) {output (node.lchild); } System.out.print (Node.count + "");
  The sequence traversal if (node.rchild!= null) {output (node.rchild);
 }} public void output () {output (root); 
  /** * Main Method * * @param args/public static void main (string[] args) {Huffman huff = new Huffman ();//Create Havermann Object Huff.creathfmtree ("Sdfassvvdfgsfdfsdfs");//Construction Tree}

Summarize

The above is based on the Java implementation of the whole Huffman tree content, I hope this article for everyone to learn to use Java can help. If you have questions, you can leave a message for discussion.

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.