Use the C language to explain the Huffman tree data structure _c language

Source: Internet
Author: User
Tags array length first row

1. Basic Concepts


A, path, and path length

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

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


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

In many applications, nodes in a tree are often given a real number with a meaning, which we call the real right of the node (as the blue digits in the tree below indicate the right of the node)

The length of the weighted path of a node is defined as the product of the length of the path from the root node to the node and the right at that point.


C, the length of the weighted path of the tree

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

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

The tree's weighted path length in the following figure 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 a tree with the right leaf node in all two fork trees, with the right path length WPL the smallest two fork trees.

The following figure is a Huffman tree schematic diagram.

2. Construction Huffman Tree


Assuming that there are n weights, the Huffman tree is constructed with n nodes. N weights are set to W1, W2 、...、 wn, the Huffman Tree construction rules are:


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


(2) The tree merging of the smallest weights of the two root nodes in the forest is merged as the left and right subtrees of a new tree, and the root node weights of the new tree are the sum of the weights of the left and right Zishugen nodes.


(3) Removing two selected trees from the forest and adding new trees to the forest;


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


For example: 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 get the Huffman tree structure as far as possible, it is usually stipulated that the Zogen node of each node in the Huffman tree is less than equal to the right Zishugen node right.


The specific algorithm is as follows:

 2, according to the array a n weights to establish a Huffman tree, return the 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++)//initialize 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++)///perform n-1 cycle establish Huffman tree {//K1 represents the subscript of a tree root node with a minimum weight in the forest, K2 as the second smallest subscript int k1 =-1, K2 
      ; 
          for (j = 0; J < N; j + +) Let K1 initially point to the first tree in the forest, K2 point to the second {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 the secondary minimum {if (B[j]!= NULL) from the current forest { 
            if (B[j]->data < b[k1]->data) {K2 = K1; 
          K1 = j;else if (B[j]->data < b[k2]->data) K2 = j; 
      A new tree is established by the minimum weight tree and the secondary 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 a new tree to the K1 position b[k2] = NULL;//K2 position is null} free (b); Deletes the dynamically established array b return q; 
 Back to the entire Huffman tree root pointer}


3, Huffman coding

In Telegraph communication, the message is transmitted in binary 0, 1 sequence, each character corresponds to a binary code, in order to shorten the total length of the message, using the unequal coding method, construction Huffman tree,

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

The 0 and 1 coded sequences of the branches are equal to the binary codes of the leaf nodes. 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

D is encoded as: 1010

E is encoded as: 1011

F is encoded as: 11


4, Huffman tree operation Operations


The above Huffman tree as a concrete example, with a detailed program to show the operation of Huffman tree operations

 #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 first sequence traversal. In a generalized table format, the element type is int void printbtree_int (struct btreenode* bt) {if (BT!= NULL) {printf ("%d", bt->data); The value of the output root node 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 subtree printf (")"); 
  }}//2, according to the n weights in array A to establish a Huffman tree, return the 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++)//initialize 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++)//n-1 Cycle Build Huffman tree {//K1 represents the subscript of a tree root node with a minimum weight in the forest, K2 as the second smallest subscript int k1 =-1, K2; 
     for (j = 0; J < N; j + +) Let K1 initially point to the first tree in the forest, K2 point to the second {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 the secondary minimum {if (B[j]!= NULL) {if (b[j]->data) from the current forest ; 
      B[k1]->data) {k2 = K1; 
     K1 = j; 
    else if (B[j]->data < b[k2]->data) K2 = j; 
   A new tree is established by the minimum weight tree and the secondary 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 a new tree to the K1 position b[k2] = NULL;//K2 position is null} free (b); Deletes the dynamically established array b return q; Back to the entire Huffman tree root pointer}//3, to find Huffman tree with the right path length elemtype weightpathlength (struct btreenode* FBT, int len)//len initially 0 {if ( 
  FBT = null)//Empty tree returns 0 return 0; else {if (FBt->left = = NULL && fbt->right = NULL)//access to the leaf node return fbt->data * len; else//access to non-leaf nodes, recursive calls, returns the sum of the weighted path lengths of the left and right subtrees, Len incremental return weightpathlength (fbt->left,len+1) +weightpathlength (fbt- 
  &GT;RIGHT,LEN+1); }//4, Huffman encoding (which can be modified based on the algorithm of Huffman Tree weighted path length) void huffmancoding (struct btreenode* FBT, int len)//len Initial value is 0 {Stati c int a[10];//defines the static array A, preserving the encoding of each leaf, the array length is at least the tree depth minus one if (FBT!= NULL)//access to the leaf node output its 0 and 1 sequence encoding stored in array a {if (Fbt->left = = 
    Null && fbt->right = null) {int i; 
    printf ("Code with a node weight of%d:", fbt->data); 
    for (i = 0; i < len; i++) printf ("%d", a[i]); 
   printf ("\ n"); 
    else//access to non-leaf nodes are recursively called to the left and right subtree, and the 0, 1 encoding on the branch is saved to the corresponding element in the array a {//, and the Len value increases by 1 A[len] = 0; 
    Huffmancoding (Fbt->left, Len + 1); 
    A[len] = 1; 
   Huffmancoding (fbt->right, Len + 1); 
  }}//main function void Main () {int n, I; 
  Elemtype* A; 
  struct btreenode* FBT; 
printf ("Input from the keyboard to be constructed in the Huffman tree with the right leaf knot number n:");  while (1) {scanf ("%d", &n); 
   if (n > 1) break; 
  else printf ("Re-transport N value:"); 
  } A = malloc (n*sizeof (elemtype)); 
  printf ("Enter%d integers from the keyboard as a weight value:", N); 
  for (i = 0; i < n; i++) scanf ("%d", &a[i]); 
  FBT = Createhuffman (A, n); 
  printf ("Huffman Tree of the generalized table Form:"); 
  Printbtree_int (FBT); 
  printf ("\ n"); 
  printf ("Huffman tree with the right path length:"); 
  printf ("%d\n", Weightpathlength (FBT, 0)); 
  printf ("Huffman code for each leaf node in the tree: \ n"); 
 Huffmancoding (FBT, 0); 

 }


Run Result:

Here's a look at an ACM topic

Topic Description:
Huffman Tree, the first line input a number of N, indicating the number of leaf nodes. Need to use these leaf nodes to generate Huffman tree, according to the concept of Huffman tree, these nodes have the right value, that is weight, the title needs to output all the nodes of the value and weight of the product of the sum.
Input:
Multiple sets of data are entered.
Enter a number n in the first row of each group, then enter n leaf nodes (the leaf node weights are no more than 100,2<=n<=1000).
Output:
The output weight value.
Sample input:
5
1 2 2 5 9
Sample output:
37


AC Code

Linked list Construction Huffman tree (insert sort)

 #include <stdio.h> #include <stdlib.h> #define MAX 1001 struct Htree {int weight; 
  struct Htree *lchild; 
  struct Htree *rchild; 
 struct Htree *next; 
  
 }; 
 void AddNode (struct htree *, struct htree *); 
 struct htree* createhfmtree (int *, int); 
  
 int GETWPL (struct htree *, int); 
  int main () {int W[max]; 
  int i, n, WPL; 
  
  struct Htree *ht; 
   while (scanf ("%d", &n)!= EOF) {for (i = 0; i < n; i + +) {scanf ("%d", &w[i]); 
   HT = Createhfmtree (w, N); 
   WPL = GETWPL (HT, 0); 
  printf ("%d\n", WPL); 
 return 0; 
  struct htree* createhfmtree (int *w, int n) {int i; 
  struct Htree *head, *PL, *PR, *proot; 
  Head = (struct Htree *) malloc (sizeof (struct htree)); 
  
  Head->next = NULL; 
   for (i = 0; i < n; i + +) {struct Htree *pnode = malloc (sizeof (struct htree)); 
   Pnode->weight = * (w + i); 
   Pnode->lchild = Pnode->rchild = Pnode->next = NULL; AddnOde (head, Pnode); 
   while (Head->next) {if (Head->next->next = NULL) break; 
   PL = head->next; 
   PR = pl->next; 
   Head->next = pr->next; 
   Proot = (struct Htree *) malloc (sizeof (struct htree)); 
   Proot->weight = Pl->weight + pr->weight; 
   proot->lchild = PL; 
   Proot->rchild = PR; 
  AddNode (head, proot); 
 Return head->next; 
  
  } void AddNode (struct htree *head, struct htree *pnode) {struct Htree *t = head; 
  while (T->next && t->next->weight < pnode->weight) T = t->next; 
  Pnode->next = t->next; 
 T->next = Pnode; 
  int getwpl (struct htree *ht, int level) {if (ht = = NULL) return 0; 
  if (!ht->lchild &&!ht->rchild) {return ht->weight * level; 
 Return to GETWPL (Ht->lchild, Level + 1) + GETWPL (Ht->rchild, level + 1); 

 }

Related Article

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.