To construct Huffman tree by Huffmantree () function First, then in the main function main () starting from the bottom up (that is, from the array sequence number zero) up layer to judge, if the left side of the parent node, the code is 0, if on the right, the code is 1. The last output generated encoding
We set up an array of Huffnode to save the information of each node in the Huffman tree. According to the nature of the binary tree, the Huffman tree with n leaf nodes has a total of 2n-1 nodes, so the size of the array huffnode is set to 2n-1. The HUFFNODE structure has weight, lchild, rchild and parent domains. The weight domain preserves the weights of the nodes, and the Lchild and Rchild hold the sequence number of the nodes of the left and right children of the node in the array Huffnode respectively, thus establishing the relationship between the nodes. In order to determine whether a node has been added to the Huffman tree to be established, it can be determined by the value of the parent domain. The value of parent at initial time is-1. When the node is added to the tree, the node parent's value is the ordinal number of its parent node in the array huffnode, not-1.
To find the code of the leaf node:
the process is essentially in the established Huffman tree, starting from the leaf node, the parent chain along the node back to the root node, every step back, went through a branch of Huffman tree, so that a Huffman code value. Because a character Huffman code is from the root node to the corresponding leaf nodes through the path of the branches of the 0, 1 sequence, so the first branch code for the low coding, and then the branch code for the desired code high. We can set up a structure array huffcode to hold the Huffman encoding information of each character, and the structure of the array element has two domains: bit and start. The field bit is a one-dimensional array that holds the Huffman encoding of the character, and start represents the starting position of the encoding in the array bit. So, for the first I character, its Huffman encoding is stored in the bit bits from Huffcode[i].start to n in the huffcode[i].bit.
Copy Code code as follows:
/*-------------------------------------------------------------------------
* Name: Huffman coding source Code.
* Under WIN-TC test pass
* Implementation process: The first through the Huffmantree () function constructs Huffman tree, and then in the main function main ()
* Starting from the bottom up (that is, starting from the node with the array sequence number zero), the upward layer is judged
* The left side of the parent node, the code is 0, if on the right side, the code is 1. The last output generated encoding.
*------------------------------------------------------------------------*/
#include <stdio.h>
#define MAXBIT 100
#define MAXVALUE 10000
#define MAXLEAF 30
#define Maxnode maxleaf*2-1
typedef struct
{
int bit[maxbit];
int start;
} hcodetype; /* Encoded structure body */
typedef struct
{
int Weight
int parent;
int lchild;
int rchild;
} hnodetype; /* node structure */
/* Constructs a huffman tree */
void Huffmantree (hnodetype huffnode[maxnode], int n)
{
* I, J: Loop Variables, M1, M2: The weights of the two minimum weights nodes in different processes of the Huffman tree,
x1, X2: The ordinal number of two minimum weights nodes in the different process of a Huffman tree is constructed. */
int I, J, M1, M2, X1, x2;
/* Initialize the node in the Huffman Tree array huffnode[] */
for (i=0; i<2*n-1; i++)
&nbs p; {
huffnode[i].weight = 0;
huffnode[i].parent =-1;
Huffnode[i].lchild =-1;
Huffnode[i].lchild =-1;
}/* End for */
/* Enter the weight of n leaf node * *
for (i=0; i<n; i++)
{
printf ("Please input weight of leaf node%d:n", i);
scanf ("%d", &huffnode[i].weight);
}/* End for/*
/* Cyclic construction Huffman tree/
for (i=0 i<n-1; i++)
{
M1=m2=maxvalue; /* M1, M2 store two no parent node and node weight of the smallest two nodes * *
x1=x2=0;
/* Finds two nodes in all nodes with minimum weights and no parent nodes, and merges them into a binary tree./
for (j=0 j<n+i; j + +)
{
if (Huffnode[j].weight < M1 && huffnode[j].parent==-1)
{
m2=m1;
x2=x1;
M1=huffnode[j].weight;
x1=j;
}
else if (Huffnode[j].weight < m2 && huffnode[j].parent==-1)
{
M2=huffnode[j].weight;
x2=j;
}
}/* End for/*
/* Settings found two child nodes x1, x2 parent Node Information * *
huffnode[x1].parent = n+i;
huffnode[x2].parent = n+i;
huffnode[n+i].weight = huffnode[x1].weight + huffnode[x2].weight;
huffnode[n+i].lchild = x1;
Huffnode[n+i].rchild = x2;
printf ("X1.weight and X2.weight in round%d:%d,%dn", I+1, Huffnode[x1].weight, huffnode[x2].weight); /* For testing * *
printf ("n");
}/* End for/*
}/* End Huffmantree * *
int main (void)
{
Hnodetype Huffnode[maxnode]; /* Define a node structure body array/*
Hcodetype Huffcode[maxleaf], cd; /* Defines an array of encoded structures, while defining a temporary variable to hold the information when solving the code. *
int I, J, C, p, N;
printf ("Please input n:n");
scanf ("%d", &n);
Huffmantree (Huffnode, N);
for (i=0 i < n; i++)
{
Cd.start = n-1;
C = i;
p = huffnode[c].parent;
while (P!=-1)/* Parent node exists * *
{
if (huffnode[p].lchild = = c)
Cd.bit[cd.start] = 0;
Else
Cd.bit[cd.start] = 1;
cd.start--; * * To find the low one of the code/
c=p;
p=huffnode[c].parent; /* Set Next cycle condition * * *
}/* End while/*
/* Save the Huffman encoding and encoding of each leaf node/code starting bit * *
for (j=cd.start+1 j<n; j + +)
{Huffcode[i].bit[j] = cd.bit[j];}
Huffcode[i].start = Cd.start;
}/* End for/*
/* Output has been saved all the existence of encoded Huffman code */
for (i=0 i<n; i++)
{
printf ("%d" Huffman code is: ", I);
for (j=huffcode[i].start+1 J < N; j + +)
{
printf ("%d", huffcode[i].bit[j]);
}
printf ("n");
}
Getch ();
return 0;
}