Huffman Coding C + + implementation __HTML5

Source: Internet
Author: User
#include <stdio.h> #include <limits.h> #include <string.h> #include <stdlib.h> #define N 6 Typede   f struct Huffnode {unsigned int weight;  weight unsigned int lchild,rchild,parent;
}htnode,*hufftree the left and right child nodes and the parent nodes;

typedef char **huffcode;
Find out the two node subscript with no parent node in the array, and save the Void Select (const hufftree &ht,int n,int &s1,int) with S1 and S2 respectively;


HT: Huffman Tree, HC: Huffman coding, W: The construction of Huffman tree node weights, N: The number of construction Huffman tree node void Huffmancode (Hufftree &ht,huffcode &hc,int *w,int N);
    int main () {int i;
    Char Key[n] = {' 0 ', ' A ', ' B ', ' C ', ' D ', ' E '};//No. 0 element is reserved without int w[n] = {0,1,2,4,5,6};//No. 0 element is reserved without hufftree HT;
    Huffcode HC;
    Huffmancode (ht,hc,w,n-1);

    for (i = 1; i < N; i++) printf ("%c:%s\n", Key[i],hc[i]);
    printf ("\ n");
return 0;
    //Find the two-node subscript with the smallest weights in the array, and save the Void Select (const hufftree &ht,int n,int &s1,int) {int i) with S1 and S2 respectively;
    S1 = s2 = 0;

  int min1 = int_max;//min, int_max int min2 = int_max;//minor value defined in <limits.h>  for (i = 1; I <= n; ++i) {if (ht[i].parent = 0) {//filter the minimum and minor weights without the parent node subscript if (Ht[i].weight <
        Min1) {//If the minimum value is smaller than min2 = min1;
        s2 = S1;
        Min1 = Ht[i].weight;
        S1 = i; else if ((ht[i].weight >= min1) && (Ht[i].weight < min2) {//if is greater than or equal to the minimum value, and is less than the minor value Mi
        N2 = Ht[i].weight;
        s2 = i;
        else {//if greater than the minor value, do nothing; }}//ht: Huffman tree, HC: Huffman coding, W: Constructs Huffman tree node weight, N: Constructs the number of Huffman tree nodes void Huffmancode (Hufftree &ht,huffcode &hc,int *w,
    int n) {int S1;
    int S2;       int m = 2 * n-1;
    It is easy to know that n-node-constructed Huffman tree is a 2n-1 node int i,c,f,j;  Char *code;  The staging code of the HT = (hufftree) malloc ((m+1) *sizeof (Htnode)); Unit 0 is not used for (i = 1; I <= n; i++) Ht[i] = {w[i],0,0,0};//Initializes the first n nodes (the original node of the construction Huffman Tree) for (i = n + 1; i <= m;  i++) Ht[i] = {0,0,0,0};
   Initialize n-1 node//Build Huffman for (i = n + 1; I <= m; i++) { Select (ht,i-1,s1,s2); To find the node subscript ht[s1].parent with the lowest weights in the first I-1 nodes = i;
    Ht[s2].parent = i;
    Ht[i].lchild = S1;
    Ht[i].rchild = s2;
    Ht[i].weight = Ht[s1].weight + ht[s2].weight;
    }//Huffman code HC = (char * *) malloc ((n) *sizeof (char *));
    The Staging Code code = (char *) malloc (n*sizeof (char));//uses unit No. 0 code[n-1] = ';
    for (i = 1; I <= n; i++) {int start = n-1; for (c = i, F = ht[c].parent; f!= 0; c = ht[c].parent, F = ht[c].parent) {//scan from leaf to root if (Ht[f].lchild = C
        ) {Code[--start] = ' 0 ';
        else if (ht[f].rchild = = c) {Code[--start] = ' 1 ';
        else {//otherwise do nothing;
    } Hc[i] = (char *) malloc (strlen (code) *sizeof (char));
    strcpy (Hc[i],&code[start]);
 }

}

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.