C language implementation code

Source: Internet
Author: User

Construct a user-defined tree using the HuffmanTree () function, and start from the bottom up (that is, starting from the node where the array serial number is zero) in the main function main, if it is on the left side of the parent node, it is set to 0. If it is on the right side, it is set to 1. Final output Encoding

We set a structure array HuffNode to save the information of each node in the Harman tree. According to the nature of the binary tree, we can see that there are 2-1 nodes in the Harman tree with n leaf nodes, so the size of the array HuffNode is set to 2n-1. The HuffNode structure includes the weight, lchild, rchild, and parent fields. The weight field stores the node weights. lchild and rchild respectively store the numbers of the left and right nodes of the node in the array HuffNode, thus establishing the relationship between nodes. To determine whether a node has been added to the user tree to be created, you can use the value of the parent field. The initial parent value is-1. When a node is added to the tree, the value of the node parent is the serial number of its parent node in the array HuffNode, instead of-1.

Encoding of leaf nodes:
In essence, this process is to roll back from the leaf node to the root node in the established Harman tree along the parent chain field of the node. Every time we step back, we go through a branch of the Harman tree, in this way, a user can obtain the value of the Harman code. Because a single-character Harman encoding is a series of 0 and 1 branches in the path from the root node to the corresponding leaf node, the branch code obtained first is the low encoding level, the obtained branch code is the height code. We can set a structure array, HuffCode, to store the information of each character's Harman encoding. The structure of the array element contains two fields: bit and start. Where, the bit field is a one-dimensional array, used to save the character's Harman encoding, start indicates the start position of the encoding in the array bit. Therefore, for the I-th character, the User-Defined code is stored in the bits of HuffCode [I]. bit From HuffCode [I]. start to n.

Copy codeThe Code is as follows:


/*-------------------------------------------------------------------------
* Name: source code encoded by Harman.
* Pass the test under Win-TC
* Implementation process: the user first constructs the User-Defined tree using the HuffmanTree () function, and then
* Starts from the bottom to the top (that is, starting from the node where the array serial number is zero ).
* Set the code to 0 on the left side of the parent node. If it is on the right side, set the code to 1. Finally, the generated encoding is output.
*------------------------------------------------------------------------*/
# Include <stdio. h>

# Define maxbbit 100
# Deprecision MAXVALUE 10000
# Define MAXLEAF 30
# Define maxnode maxleaf * 2-1

Typedef struct
{
Int bit [MAXBIT];
Int start;
} HCodeType;/* encoding struct */
Typedef struct
{
Int weight;
Int parent;
Int lchild;
Int rchild;
} HNodeType;/* node struct */

/* Construct a user-defined tree */
Void HuffmanTree (HNodeType HuffNode [MAXNODE], int n)
{
/* I, j: cyclic variable, m1, m2: Construct the weights of the two least weight nodes in the different PROCESS OF THE Harman tree,
X1 and x2: Construct the sequence numbers of the two least weight nodes in the array during different processes of the Harman tree. */
Int I, j, m1, m2, x1, x2;
/* Initialize and store the nodes in the HuffNode [] array of the Harman tree */
For (I = 0; I <2 * n-1; I ++)
{
HuffNode [I]. weight = 0;
HuffNode [I]. parent =-1;
HuffNode [I]. lchild =-1;
HuffNode [I]. lchild =-1;
}/* End */

/* Enter the weight of n leaf nodes */
For (I = 0; I <n; I ++)
{
Printf ("Please input weight of leaf node % d: n", I );
Scanf ("% d", & HuffNode [I]. weight );
}/* End */

/* Construct the Huffman tree cyclically */
For (I = 0; I <n-1; I ++)
{
M1 = m2 = MAXVALUE;/* m1 and m2 store two nodes with no parent node and the minimum node weight */
X1 = x2 = 0;
/* Find the two nodes with the smallest weight and no parent node in all nodes and combine 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 */
/* Set the parent node information of the two child nodes x1 and x2 found */
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 */
}/* End HuffmanTree */

Int main (void)
{
HNodeType HuffNode [MAXNODE];/* defines a node struct array */
HCodeType HuffCode [MAXLEAF], cd;/* defines an array of encoding structures and a temporary variable to store information for encoding */
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 --;/* calculate the lower bit of encoding */
C = p;
P = HuffNode [c]. parent;/* set the next cycle condition */
}/* End while */

/* Save the start bits of the Harman encoding and encoding for each leaf node */
For (j = cd. start + 1; j <n; j ++)
{HuffCode [I]. bit [j] = cd. bit [j];}
HuffCode [I]. start = cd. start;
}/* End */

/* Output all the Haffman codes that have been saved */
For (I = 0; I <n; I ++)
{
Printf ("% d's Huffman code is:", I );
For (j = HuffCode [I]. start + 1; j <n; j ++)
{
Printf ("% d", HuffCode [I]. bit [j]);
}
Printf ("n ");
}
Getch ();
Return 0;
}

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.