Huffman tree to create and encode __ code

Source: Internet
Author: User

the creation and coding of Huffman tree

Project busy dying, blog stopped for two days, do outsourcing really uncomfortable, or do products strong. Software last most valuable is not the code, but the relevant documents, documents clear, according to gourd painting ladle according to do it should not be difficult. The project is over. At least the requirements specifications, system design documents, user instructions, development schedules, tenders, job descriptions and other documents should be sorted out.

This article according to "Data Structure and algorithm" (C language Edition) (third edition) collation.

This blog as a learning material, the source code is VC + + 6.0 on the executable program, I moved to the VS2010 in the implementation.

1. Huffman tree, also known as the optimal binary tree , is a kind of tree with the shortest length of weighted path. for the optimal binary tree, the greater the weight of the node is closer to the root node of the tree, the smaller the weight of the node farther away from the root node of the tree.
The construction algorithm steps of the optimal binary tree:
(1) According to the given n weights w1,w2,..., wn constitute n tree f={t1,t2,..., Tn}, in which each of the two fork-tree ti has only one right for the WI root node, and its left and right subtree is empty.
(2) in Forest F, the tree of two root node weights is selected as the left and right subtree of a new binary tree, and the weight of the root node of the new binary tree is the sum of the weights of its left and right Zishugen nodes.
(3) Remove the two binary trees from F and add the new binary tree to the F.
(4) Repeat steps (2), (3) until the F contains only one tree, the tree is the optimal binary tree.

Huffman tree construction process diagram is as follows:

Huffman tree node type declaration:

    struct TreeNode
    {
       int weight;
       int parent;
       int lchild;
       int rchild;
    };
    typedef struct TREENODE Hftreenode;
    typedef hftreenode Huffmantree;

Huffman Tree Construction algorithm:

     #define MAXSIZE 1000//leaf number void Select (Huffmantree *ht, int g, int &s1, int &s2);
         void Createhuffmantree (Huffmantree t[maxsize], int n) {int i,p1,p2;
         if (n<1) return 1;        M=2*n;
            Compute Huffman tree node size for (i=1; i<m; i++) {t[i].parent=0;
            T[i].lchild=0;
            T[i].rchild=0;
         t[i].weight=0;
           For (I=1 i<n; i++)//Read the right value of the leaf node {scanf ("%d", &weight);
          T[i].weight=weight;
            For (i=n i<m-1; i++) {selectmin (T, I-1, p1, p2);
            In t[0...i-1], two root nodes with the smallest weights are selected, and their serial numbers are P1 and P2 t[p1].parent=t[p2].parent=i respectively.      T[I].LCHILD=P1;      The root node of the smallest right is the left child t[i].rchild=p2 of the new node;
        The root node of Chicao is the right child t[i].weight=t[p1].weight+t[p2].weight of the new node; } void Selectmin (Huffmantree *ht, int g, int &s1, int &s2) {inT J, K, M, N;
            For (k=1 k<=g; k++)//Find a subtree of parent-1 {if (ht[k].parent==0) {s1=k;
           Break For (J=1 j<=g; j + +) {if (ht[j].weight<=ht[k].weight) && (ht[j].parent==0)
       )//Find a s1=j with parent-1 as the smallest subtree;
            For (m=1 m<=g; m++) {if (ht[m].parent==0) && (m!=s1)) {s2=m;
           Break For (n=1 n<=g; n++) {if (ht[n].weight 
2. Huffman CodingHuffman coding is a variable-length code. The definition is as follows:

For the given character set D={D1,D2,..., DN} and its frequency distribution f={w1,w2,..., WN}, using the D1,D2,..., DN as the node, W1,W2,..., WN as the nodes, and using Huffman algorithm to construct an optimal binary tree, The left branch of each branch node in the tree is labeled "0", the right branch is labeled "1", and the path symbol ("0" or "1") from the root to each leaf is connected as the leaf's encoding.

Huffman coding is based on Huffman tree on the basis of the basic idea is: from the leaf node di (0<=i<n) Start, back up to the root node, in order to find out the encoding of each character.

Example: for character set d={a,b,c,d}, its frequency (in thousands) is distributed as f={12,6,2,18}, and the following figure shows the Huffman code graph for D.

Huffman code of the backtracking steps are as follows:
(1) Select a one-leaf node of Huffman tree.
(2) The parent node is found by using its parent pointer.
(3) Use the Lchild and rchild in the pointer field of the found parent node to determine whether the node is the left or right child of the parent. If the node is the left child of its parent node, code 0 is generated, and if the node is the right child of its parent node, code 1 is generated.
(4) The generated encoding is reversed because of the generated encoding and the required coding sequence.
(5) Repeat steps (1) ~ (4) until all nodes have been traced back.

Inverse sequence method: First, the generated encoding is stored in a temporary one-dimensional array, and a pointer start indicates the starting position of the encoding in the one-dimensional array.        When the encoding of a leaf node is complete, copy the encoding from the start of the temporary one-dimensional array to bits in the corresponding character. Huffman encoded storage structure:

    struct Codenode
    {
       char ch;           Store character
       Char bits[n+1];    Storing the coded bit string
    };
    typedef struct CODENODE Codenoe;
    

Huffman Coding algorithm:

     void Charsethuffmanencoding (Huffmantree t, Huffmancode h)
     {   //based Huffman T Huffman coding table H
        int C, p, I;
        Char cd[n+1];
        int start;
        Cd[n]= ' ";
        For (i=0 i<n; i++)
        {
          //In order to find the leaf t[i] encoding
          H[i].ch=getchar ();      Read into the leaves T[i] corresponding character
          start=n;                Initial value c=i of the encoding starting position
          ;                    From Leaf t[i] start back while
          (p=t[c].parent>0)
          {
             if (t[p].lchild==c)
             {
                cd[--start]= ' 0 ';
             }
             else
             {
                cd[--start]= ' 1 ';
              }
             c=p;      Go back
          to
          strcpy (H[i].bits, &cd[start]);      Copy encoded bit string
       }
    }  
3. Huffman decodingHuffman decoding process: From Huffman tree root node, in order to identify the message in the binary code, if 0, then to the left child, or to the right child, go to the leaf node, you can get the corresponding decoding characters.

The algorithm is as follows:

    void Charsethuffmandecoding (Huffmantree T, char* CD, int n) {int p=2*n-2;
           Starting with the root node int i=0; When the string to decode does not end while (cd[i]!= '/0 ') {//When the leaf of the Huffman tree is not reached and the string to decode does not end WH  Ile ((t[p].lchild!=0 && t[p].rchild!= 0) && cd[i]!= ' i ') {if (cd[i)
                     = = ' 0 ') {//If it is 0, the leaves are p=t[p].lchild in the left subtree;
                        else {//if 1, leaves in Zuozi
                     P=t[p].rchild;
          } i++; //If the leaves of Huffman tree are reached if (t[p].lchild = 0 && T[p].rchild = = 0) {printf (
               "%c", t[p].ch);
            p = 2*n-1; else//If the node number p is not a leaf, then the encoding is wrong {printf ("\ n decoding error!")
                \ n "); ReturN
    printf ("\ n"); }
4. Huffman Tree creation and Huffman coding program:Create a new WIN32 console Application project in VS2010: Huffmantree, creating the results as follows:
HuffmanTree.cpp: Defines the entry point for a console application. #include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct HUFFM
   antree {int weight;
 int parent, lchild, Rchild;

 } Huffmantree;
   typedef struct CODENODE {int ch;
 Char bits[4+1];

 }codenode;
   void Selectmin (Huffmantree tree[], int len, int * pos1, int* pos2) {int min=255;
   int I, J;
   *pos1=0;
   *pos2=0; For (i=0 i<len; i++) {if (tree[i].parent==-1) if (min>tree[i].weight) {min=tree[i].we
         ight;
       *pos1=i;

   }} min=255;
     For (j=0 j<len; j + +) {if (J==*POS1) continue;
         if (tree[j].parent==-1) if (min>tree[j].weight) {min=tree[j].weight;
       *pos2=j;
   }} void Createhuffmantree (Huffmantree tree[], int n) {int m=2*n;
   int i;
     For (i=n i<m-1; i++) {int pos1, pos2;
     Huffmantree node; Selectmin (tree, I, &AMP;POS1, &POS2);
     printf ("pos1=%d,pos2=%d\n", POS1, Pos2);
     Node.weight=tree[pos1].weight+tree[pos2].weight;
     tree[pos1].parent=i;
     tree[pos2].parent=i;
     NODE.LCHILD=POS1;
     Node.rchild=pos2;
     Node.parent=-1;
   Tree[i]=node;
   } void Huffmanencoding (Huffmantree tree[]) {int C, p, I;
   int start;
   Char cd[4+1];

   Cd[4]= ' ";
     For (i=0 i<4; i++) {printf ("\ n");
     printf ("%d", tree[i].weight);
     printf (":");
     start=4;
     C=i;
       while ((p=tree[c].parent)!=-1) {if (tree[p].lchild==c) {cd[--start]= ' 0 ';
       else {cd[--start]= ' 1 ';
     } c=p;
   printf (&cd[start]);
   int main (int argc, char* argv[]) {huffmantree tree[4*2];
   int I, J;
     for (i=0; i<4; i++) {tree[i].lchild=-1;
	 Tree[i].rchild=-1;
   Tree[i].parent=-1;
   printf ("Please enter the Haffman node's weight: \ n");
     for (i=0; i<4; i++)/Read the weight of the leaf node {int weight; ScaNF ("%d", &weight);
   Tree[i].weight=weight;
   Createhuffmantree (tree, 4);
   For (j=0 j<2*4-1; j + +) {printf ("tree[%d]:weight=%d \ n", J, Tree[j].weight);
   
   } huffmanencoding (tree);
 return 0; }

Ctrl+f5 execution HuffmanTree.cpp results are as follows:


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.