Implementation of the Harman Tree Code

Source: Internet
Author: User

Definition
A type of tree with the shortest length of a weight path, also known as the optimal tree, is a type of tree with the shortest length.

The length of the weighted path of all leaf nodes in the tree. It is usually recorded as WPL = W1 * L1 + W2 * L2 +... + Wn * Ln.

For example:

 

Nodes ABCDE have the following weights: 1, 2, 4, 5, and 6. For Figure 1, WPL = 4*3 + 2*3 + 1*3 + 5*3 + 6*1 = 42. For Figure 2, WPL = 1*3 + 2*3 + 4*2 + 5*2 + 6*2 = 39. The above nodes can also list other trees and calculate WPL. We can see that the WPL value in Figure 2 is the smallest, and this tree is called the optimal binary tree or the Harman tree.

How to Create a binary tree?
1. Regard All nodes as independent trees, and left and right subtree are empty without parent nodes;

2. Select two trees with the smallest root node weight without a parent node and generate a node as their parent node. The parent node's weight is equal to the sum of their weights;

3. Repeat Step 1 until it turns into a tree.

For example, for the above ABCDE node, first select the parent node (and marked as A') formed by A and B as 3, then select from the weight value of 3, 4, 5, 6, of course, select 3, 4, that is, for the' and C nodes, the parent node (and marked as C ') has A value of 7. Next, select the smallest two from the values 5, 6, and 7, of course, 5, 6. that is, D and E form the weight of the parent node (and recorded as D ') is 11, and then D and C are formed into the parent node, that is, the final root node. Built the Harman tree.

How to build a user-defined coding system?
Scan the leaf node to the root node. If it is left subtree, it is marked as 0. If it is right subtree, it is marked as 1. 2. the encoding of A is: 000, and the encoding of B is 100.

The Code is as follows:


[Cpp]
// HuffmanCoding. c
# Include <stdio. h>
# Include <limits. h>
# Include <string. h>
# Include <stdlib. h>
# Define N 6
 
Typedef struct huffNode
{
Unsigned int weight; // weight
Unsigned int lchild, rchild, parent; // left and right child nodes and parent nodes
} HTNode, * HuffTree;
Typedef char ** HuffCode;
 
// Find the two subscripts with no parent node and minimum weight in the array, and save them with s1 and s2 respectively.
Void select (const HuffTree & HT, int n, int & s1, int & s2 );
// HT: Harman tree, HC: Harman code, w: Construct the weight of the Harman Tree node, n: Construct the number of Harman Tree nodes
Void HuffmanCode (HuffTree & HT, HuffCode & HC, int * w, int n );
 
 
Int main ()
{
Int I;
Char key [N] = {'0', 'A', 'B', 'C', 'D', 'E'}; // 0th elements Reserved
Int w [N] = {0th,}; // elements Reserved
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 subscripts with the smallest weight in the array, save them with s1 and s2 respectively
Void select (const HuffTree & HT, int n, int & s1, int & s2)
{
Int I;
S1 = s2 = 0;
Int min1 = INT_MAX; // minimum value, which is defined by INT_MAX in <limits. h>
Int min2 = INT_MAX; // small value
 
For (I = 1; I <= n; ++ I)
{
If (HT [I]. parent = 0)
{// Filter the minimum and minimum sub-weight subscript with no parent node
If (HT [I]. weight <min1)
{// If it is smaller than the minimum value
Min2 = min1;
S2 = s1;
Min1 = HT [I]. weight;
S1 = I;
}
Else if (HT [I]. weight> = min1) & (HT [I]. weight <min2 ))
{// If the value is greater than or equal to the minimum value and smaller than the next small value
Min2 = HT [I]. weight;
S2 = I;
}
Else
{// If the value is greater than the next small value, nothing will be done
;
}
}
}
}
 
// HT: Harman tree, HC: Harman code, w: Construct the weight of the Harman Tree node, n: Construct the number of Harman 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 the user tree constructed by n nodes is 2n-1 nodes.
Int I, c, f, j;
Char * code; // temporarily encoded
HT = (HuffTree) malloc (m + 1) * sizeof (HTNode); // unit 0 is not used

 
For (I = 1; I <= n; I ++)
HT [I] = {w [I], 0}; // initialize the first n nodes (create the original node of the Harman tree)

For (I = n + 1; I <= m; I ++)
HT [I] = {0, 0, 0}; // n-1 nodes after Initialization
 
// Construct a user-defined tree
For (I = n + 1; I <= m; I ++)
{
Select (HT, I-1, s1, s2); // locate the subscripts with the smallest weight in the previous I-1 Node
HT [s1]. parent = I;
HT [s2]. parent = I;
HT [I]. lchild = s1;
HT [I]. rchild = s2;
HT [I]. weight = HT [s1]. weight + HT [s2]. weight;
}
// Harman Encoding
HC = (char **) malloc (n) * sizeof (char *));
// Temporary Encoding
Code = (char *) malloc (n * sizeof (char); // unit 0th is used
For (I = 1; I <= n; I ++)
{
For (c = I, f = HT [c]. parent, j = 0; f! = 0; c = HT [c]. parent, f = HT [c]. parent, j ++)
{// Scan the leaf to the root
If (HT [f]. lchild = c)
{
Code [j] = '0 ';
}
Else if (HT [f]. rchild = c)
{
Code [j] = '1 ';
}
Else
{// Otherwise, nothing will be done
;
}
}
Code [j] = '\ 0 ';
HC [I] = (char *) malloc (strlen (code) * sizeof (char ));
Strcpy (HC [I], code );
}

}

// HuffmanCoding. c
# Include <stdio. h>
# Include <limits. h>
# Include <string. h>
# Include <stdlib. h>
# Define N 6

Typedef struct huffNode
{
Unsigned int weight; // weight
Unsigned int lchild, rchild, parent; // left and right child nodes and parent nodes
} HTNode, * HuffTree;
Typedef char ** HuffCode;

// Find the two subscripts with no parent node and minimum weight in the array, and save them with s1 and s2 respectively.
Void select (const HuffTree & HT, int n, int & s1, int & s2 );
// HT: Harman tree, HC: Harman code, w: Construct the weight of the Harman Tree node, n: Construct the number of Harman Tree nodes
Void HuffmanCode (HuffTree & HT, HuffCode & HC, int * w, int n );


Int main ()
{
Int I;
Char key [N] = {'0', 'A', 'B', 'C', 'D', 'E'}; // 0th elements Reserved
Int w [N] = {0th,}; // elements Reserved
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 subscripts with the smallest weight in the array, save them with s1 and s2 respectively
Void select (const HuffTree & HT, int n, int & s1, int & s2)
{
Int I;
S1 = s2 = 0;
Int min1 = INT_MAX; // minimum value, which is defined by INT_MAX in <limits. h>
Int min2 = INT_MAX; // small value

For (I = 1; I <= n; ++ I)
{
If (HT [I]. parent = 0)
{// Filter the minimum and minimum sub-weight subscript with no parent node
If (HT [I]. weight <min1)
{// If it is smaller than the minimum value
Min2 = min1;
S2 = s1;
Min1 = HT [I]. weight;
S1 = I;
}
Else if (HT [I]. weight> = min1) & (HT [I]. weight <min2 ))
{// If the value is greater than or equal to the minimum value and smaller than the next small value
Min2 = HT [I]. weight;
S2 = I;
}
Else
{// If the value is greater than the next small value, nothing will be done
;
}
}
}
}

// HT: Harman tree, HC: Harman code, w: Construct the weight of the Harman Tree node, n: Construct the number of Harman 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 the user tree constructed by n nodes is 2n-1 nodes.
Int I, c, f, j;
Char * code; // temporarily encoded
HT = (HuffTree) malloc (m + 1) * sizeof (HTNode); // unit 0 is not used

For (I = 1; I <= n; I ++)
HT [I] = {w [I], 0}; // initialize the first n nodes (create the original node of the Harman tree)

For (I = n + 1; I <= m; I ++)
HT [I] = {0, 0, 0}; // n-1 nodes after Initialization

// Construct a user-defined tree
For (I = n + 1; I <= m; I ++)
{
Select (HT, I-1, s1, s2); // locate the subscripts with the smallest weight in the previous I-1 Node
HT [s1]. parent = I;
HT [s2]. parent = I;
HT [I]. lchild = s1;
HT [I]. rchild = s2;
HT [I]. weight = HT [s1]. weight + HT [s2]. weight;
}
// Harman Encoding
HC = (char **) malloc (n) * sizeof (char *));
// Temporary Encoding
Code = (char *) malloc (n * sizeof (char); // unit 0th is used
For (I = 1; I <= n; I ++)
{
For (c = I, f = HT [c]. parent, j = 0; f! = 0; c = HT [c]. parent, f = HT [c]. parent, j ++)
{// Scan the leaf to the root
If (HT [f]. lchild = c)
{
Code [j] = '0 ';
}
Else if (HT [f]. rchild = c)
{
Code [j] = '1 ';
}
Else
{// Otherwise, nothing will be done
;
}
}
Code [j] = '\ 0 ';
HC [I] = (char *) malloc (strlen (code) * sizeof (char ));
Strcpy (HC [I], code );
}

}

 

 

Running result:

 


 


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.