Design and implementation of 5.6 Huffman coding

Source: Internet
Author: User

#include <stdio.h> #define MAXVALUE 10000#define maxleaf 30#define maxnode 59#define maxbit 10//Huffman tree structure typedef   struct{int data; node value int Weight;   weight int Flag; Identifies whether the node is to be reconstructed, or if it is represented by 0, otherwise the int Parent is represented by 1; parent node int lchild; Left node int rchild;    Right node}hnodetype;typedef struct{int bit[maxbit]; int Start;} hcodetype;/****--------------------------------------------*****///Function name: Inithaffman (Hnodetype huffnode[],        Hcodetypde huffnode[], int n)//parameter: (incoming) Hnodetype huffnode[] Huffman tree node//(incoming) Hcodetype huffcode[] Huffman Coding tree Node// (incoming) int n node number//function: Huffman node initialization/****--------------------------------------------*****/void Inithaffman (hnodetype    Huffnode[], Hcodetype huffcode[], int n) {int i; Initialize the resulting node, point to the father's pointer, left child, right child's hands are first empty for (i = 0; i < 2*n-1; i++) {huffnode[i].        Weight = 0; Huffnode[i].        Parent = 0; Huffnode[i].        Flag = 0; Huffnode[i].        Lchild =-1; Huffnode[i].     Rchild =-1; } for (i = 0; i < n; i++) {getcHar ();        printf ("Input%d leaf node value:", i+1);        scanf ("%c", &huffnode[i].data);        printf ("Input corresponding node weights:"); scanf ("%d", &huffnode[i].     Weight); }}/****--------------------------------------------*****///Function name: Outputhaffman (Hnodetype huffnode[], Hcodetype huffnode[], int n)//Parameter name: (incoming) Hnodetype huffnode[] Huffman tree node//(incoming) Hcodetype huffnode[] Huffman Tree Code tree Node//(incoming) int n node Quantity//function: Output Huffman code/****--------------------------------------------*****/void Outputhaffman (Hnodetype huffnode[],    Hcodetype huffcode[], int n) {int i, J;    printf ("%d leaf nodes corresponding to code: \ n", "N.");        for (i = 0; i < n; i++) {printf ("%c---", huffnode[i].data); for (j = huffcode[i]. start+1; J < N; J + +) {printf ("%d", huffcode[i].        BIT[J]);    } printf ("\ n"); }}/****-------------------------------------------*****///Function name: Haffman (Hnodetype huffnode[], hcodetypecode[], int n )//Parameters: (incoming) Hnodetype huffnode[] Huffman tree node//(incoming) Hcodetype huffcode[] Huffman Tree Coding tree Node//(incoming) int n node number//function: Construct Huffman tree, generate Huffman code according to tree/****-------------------------------------------*****/void haffman (hnodetype    Huffnode[], Hcodetype huffcode[], int n) {int I, J, M1, M2, X1, x2, C, p;    Hcodetype cd;       for (i = 0; i < n-1; i++) {//Construction Huffman Tree//According to Huffman Tree construction process, always select the minimum weight of two nodes to form a binary tree m1 = m2 = MAXVALUE;       X1 and x2 bit minimum weight of two node positions x1 = x2 = 0; The loop finds one from the flag 0 node for the minimum value for (j = 0; J < N+i; J + +) {if (Huffnode[j]. Weight < M1 && Huffnode[j].            Flag = = 0) {m2 = m1;            x2 = x1; M1 = Huffnode[j].            Weight;  x1 = j; Make a note of X2 's address} else if (Huffnode[j]. Weight < m2 && Huffnode[j]. Flag = = 0) {m2 = huffnode[j].            Weight;        x2 = j; }}//for//The found two nodes in accordance with the rules of Huffman tree to build a binary tree, X1 for the left child, X2 for right child huffnode[x1].        Parent = N+i; HUFFNODE[X2].        Parent = N+i; HUFFNODE[X1].  Flag = 1; Place the X1 Subscript 1 huffnode[x2].  Flag = 1; Place the X2 subscript 1 HuffnodE[n+i]. Weight = huffnode[x1]. Weight + huffnode[x2].        Weight; Huffnode[n+i].        Lchild = x1; Huffnode[n+i].    Rchild = x2; }//for for (i = 0; i < n; i++) {//Huffman encoded CD based on Huffman Tree.        Start = n-1;        c = i; p = huffnode[c].        Parent; while (P! = 0) {////When the parent node is not the root node, reverse-order up//If the child is currently left, encode 0 if (huffnode[p]. Lchild = = c) CD. BIT[CD.            Start] = 0; The current left child is encoded as 1 else CD. BIT[CD.            Start] = 1; Cd.            start--;            c = P; p = huffnode[c].        Parent; } for (j = CD. start+1; J < N; J + +) {Huffcode[i]. BIT[J] = CD.            BIT[J]; Huffcode[i]. Start = CD.        Start;    }}//for}//end of Haffmanint main () {Hnodetype huffnode[maxnode];    Hcodetype Huffcode[maxleaf];    int n;    printf ("Number of input leaf nodes n:\n");    scanf ("%d", &n);    Inithaffman (Huffnode, Huffcode, N);    Haffman (Huffnode, Huffcode, N);    Outputhaffman (Huffnode, Huffcode, N); return 0;}


Operation Result:

Number of input leaf nodes N:

6

Enter a 1th leaf node value: a

Input corresponding node weight: 3

Enter a 2nd leaf node value: b

Input corresponding node weight: 9

Enter a 3rd leaf node value: C

Input corresponding node weight: 12

Enter a 4th leaf node value: D

Input corresponding node weight: 3

Enter a 5th leaf node value: E

Input corresponding node weight: 2

Enter a 6th leaf junction value: F

Input corresponding node weight: 71

The corresponding codes for the 6 leaf nodes are:

A---------------01011

B---------------011

C---------------00

D---------------0100

E---------------01010

F----------------1

Design and implementation of 5.6 Huffman coding

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.