Basic construction and operation of Huffman tree

Source: Internet
Author: User
Tags array length
See the explanation of the Huffman tree of a more understood blog

Source: http://blog.csdn.net/wtfmonking/article/details/17150499#
1. Basic Concepts


A, path, and path length

If there is a node sequence k1,k2,......,kj in a tree that makes Ki the parent of ki+1 (1<=I<J), the node sequence is called the path from K1 to KJ.

The number of branches that pass from K1 to KJ is called the path length between these two points, which equals the node count minus 1 on the path.


b, the right of the node and the length of the weighted path

In many applications, the nodes in the tree are often given a real number with a certain meaning, which we call the right of the node, (as the blue numbers in the following tree represent the node's right).

The length of a node's weighted path is defined as the product of the length of the path from the root node to the node and the right of the node.


C, the length of the tree with weighted path

The weighted path length of the tree is defined as the sum of the weighted path lengths of all leaf nodes in the tree, with the formula:


where n represents the number of leaf nodes, WI and Li respectively represent the weight of the leaf node Ki and the path length between the root node and the ki.

The tree in the following figure has a weighted path length of WPL = 9 x 2 + x 2 + x 2 + 6 x 3 + 3 x 4 + 5 x 4 = 122



D, Huffman Tree

Huffman Tree is also called the optimal binary tree. It is n all two-fork trees with weighted leaf nodes, with a weighted path length of WPL with a minimum of two forks.

The figure below is a Huffman tree diagram.


2. Constructing Huffman Tree


Assuming there are n weights, the Huffman tree is constructed with n leaf nodes. N weights are set to W1, W2 、...、 WN respectively, then the structure rules of Huffman tree are:


(1) W1, W2 、..., wn as a forest with n trees (each tree has only one node);


(2) The tree merging of the minimum weights of the two root nodes in the forest is used as the left and right subtree of a new tree, and the root node weights of the new tree are the sum of their left and right subtree node weights;


(3) Remove the selected two trees from the forest and add the new tree to the forest;


(4) Repeat (2), (3) step, until there is only one tree left in the forest, the tree is the Huffman tree obtained.


Such as: The following figure in the six with the right leaf node to construct a Huffman tree, the steps are as follows:


Note: In order to make the structure of Huffman tree as unique as possible, it is usually stipulated that the right of the Zogen node of each node in the Huffman tree is less than or equal to the right subtree node.


The specific algorithm is as follows:[CPP] View plain Copy//2, set up a Huffman tree based on the array  a   n  weights, return the tree root pointer    struct btreenode*  Createhuffman (elemtype a[], int n)    {       int i,  j;       struct BTreeNode **b, *q;        b = malloc (n*sizeof (struct btreenode));       for   (i = 0; i < n; i++)  //initializes the B-pointer array so that each pointer element points to the corresponding element node in the a array        {           b[i] = malloc ( sizeof (Struct btreenode));           b[i]->data  = a[i];           b[i]->left = b[i]-> right = null;       }       for  (i  = 1; i < n; i++)//For  n-1  Cycles Establishment Huffman tree        {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//K1 represents the subscript of the tree root node with a minimum weight in the forest, K2 the sub-minimum subscript             int k1 = -1, k2;            for  (j = 0; j < n; j++)//Let K1 initially point to the first tree in the forest, K2 points to the second tree            {                if  (b[j] != null && k1 ==  -1)                {                   k1 =  j;                    continue;               }                if  (b[j] != null)                 {                    k2 = j;                    break;                }            }           for  (j = k2; j  < n; j++)//from the current forest to find the minimum weight tree and sub-minimum            {                if  (B[j] != null)    &Nbsp;           {                    if  (b[j]->data <  B[k1]->data)                     {                        k2 = k1;                        k1 = j;                    }                    else  if  (b[j]->data < b[k2]->data)                         k2 = j;                }            }           //A new tree from the minimum and secondary minimum weights tree, Q points to the root node            q = malloc (sizeof (Struct btreenode));            q->data = b[k1]->data + b[k2]- >data;           q->left = b[k1];           q->right = b[k2];               b[k1] = q;//assigns a pointer to the new tree to the B-pointer array k1 position       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;B[K2]&NBSP;=&NBSP;NULL;//K2 location is empty        }     &Nbsp;  free (b);  //delete the dynamically created array b       return q; // Returns the root of the entire Huffman tree pointer   }  
3. Huffman Code

In Telegraph communication, the message is transmitted in a binary sequence of 0, 1, each character corresponding to a binary code, in order to shorten the total length of the message, the use of unequal length encoding method, to construct Huffman tree,

The occurrence frequency of each character is assigned to the leaf node as the weight of the character node, and the left and right branches of each branch node are encoded in 0 and 1 respectively, from the root node to the path of each leaf junction.

The 0, 1 encoding sequence of the branch is equal to the binary encoding of the leaf node. The Huffman code as shown above is as follows:

The code for A is: 00

The code for B is: 01

The code for C is: 100

The code for D is: 1010

The code for E is: 1011

The code for F is: 11


4, the operation of Huffman tree operations


Huffman tree above as a concrete example, with detailed procedures to show the operation of Huffman tree operations[CPP] View plain copy #include <stdio.h>   #include <stdlib.h>   typedef int  elemtype;   struct btreenode   {       ElemType data;        struct BTreeNode* left;        struct btreenode* right;  };     //1, Output binary tree, can be modified on the basis of the pre-order traversal. Generalized tabular format with element types int   void printbtree_int (STRUCT&NBSP;BTREENODE*&NBSP;BT)    {       if  (bt != null)        {            printf ("%d",  bt->data)  //output root node value             if  (bt->left != null | |  bt->right != null)            {               printf ("(");                printbtree_int (bt->left);  //output left subtree                 if  (bt->right != null)                     printf (",");                printbtree_int (BT->right);  //output right sub-tree                printf (")");           }       }  }      //2, according to the array  a   n  weights to build a Huffman tree, return to the root of the pointer    struct btreenode*  createhuffman (elemtype a[], int n)    {       int  i, j; &nbsp     struct BTreeNode **b, *q;       b =  malloc (n*sizeof (struct btreenode));       for  (i = 0;  i < n; i++)  //Initializes a B-pointer array so that each pointer element points to the corresponding element node in the A array        {            b[i] = malloc (sizeof (struct  Btreenode));           b[i]->data = a[i];            b[i]->left = b[i]->right =  null;       }       for  (i = 1;  i < n; i++)//For  n-1  Cycles Establishment Huffman tree        {   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//K1 represents the subscript of the tree root node with the smallest weights in the forest, K2 the sub-minimum subscript             int k1 = -1, k2;            for  (j = 0; j < n; j++)//Let K1 initially point to the first tree in the forest, K2 points to the second tree             {                if  (b[j] != null && k1 == -1)                 {                    k1 = j;                    continue;               }                if  (b[j] != null)                 {                    k2 = j;                    break;                }           }           for  (j = k2; j < n; j++) Find the minimum weight tree and sub-minimum            {       in the current forest          if  (b[j] != null)                 {                    if  (b[j]->data < b[k1]-> Data)                    {                         k2 = k1;                        k1 = j;                    }                    else if  (b[j]- >data < b[k2]->data)                         k2 = j;                }            }          //a new tree from the minimum weight tree and the sub-minimum weight tree, Q points to the root node             q = malloc (sizeof (Struct btreenode));            q->data = b[k1]->data + b[k2]->data;            q->left = b[k1];            q->right = b[k2];               b[k1] = q;//assigns a pointer to the new tree to the B-pointer array k1 position         &NBSP;&NBSP;&NBSP;&NBSP;B[K2]&NBSP;=&NBSP;NULL;//K2 location is empty        }        free (b);  //delete the dynamically created array b       return q; // Returns the root of the entire Huffman tree pointer   }     //3, to find the Huffman tree with the right path length    elemtype weightpathlength ( struct btreenode* fbt, int len)//len initial 0   {       if  (fbt ==  null)  //empty tree back to 0           return 0;       else       {            if  (fbt->left == null && fbt->right == null )//access to leaf nodes                return fbt- >data * len;           else //access to non-leaf nodes, Make a recursive call, return the sum of the weighted path lengths of the left and right subtrees, Len increment                 return weightpathlength (fbt->left,len+1) +weightpathlength (fbt->right,len+1);       }  }     //4, Huffman coding (can be modified based on the algorithm based on the Huffman tree-weighted path length)    Void huffmaNcoding (Struct btreenode* fbt, int len)//len initial value is 0   {        static int a[10];//defines the static array A, saves each leaf's encoding, the array length is at least the tree depth minus one        if   (Fbt != null)//access to the leaf node outputs its 0 and 1 sequence encodings saved in array a        {           if  (fbt->left == null &&  Fbt->right == null)            {               int i;                printf ("Code with node weights of%d:",  fbt->data);                for  (i = 0;  i < len; i++)                     printf ("%d",  a[i]);                printf ("\ n");           }            else//access to the non-leaf nodes, respectively, to the left and right subtree recursive call, and the branch of the 0, 1 encoding to the array a            {   //in the corresponding element, the Len value increases 1   when you drill down one layer              a[len] = 0;                huffmancoding (fbt->left, len + 1);               a[len] = 1;               huffmancoding (FBT->right, len  + 1);           }        }  }     //main functions    Void main ()    {       int  n, i;       ElemType* a;  

  • 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.