Definition:
The length of the weighted path of a node is the product of the path length from the node to the root of the node and the permission on the node. The length of a tree's weighted path is the sum of the length of the weighted path of all leaf nodes in the tree. Assume that there are n weights, and we try to construct a binary tree with n leaf nodes. Each leaf node has a weight of wi, the binary tree with the minimum length of the weighted path is called the optimal binary tree or the Heman tree.
The method for constructing the Heman tree is as follows:
(1) Based on the given n weights {w1, w2, w3 ......} set of n Binary Trees F = {T1, T2, T3, t4 ......}, there is only one root node with the wi permission in each binary tree Ti, and its left and right Subtrees are empty.
(2) In F, select the tree with the minimum weight of the two root nodes as the left and right subtree to construct a new binary tree, the weights of the root node of the new binary tree are the sum of the weights of the root node of the left and right Subtrees.
(3) Delete the two trees in F and add the new binary tree to F.
(4) Repeat (2) and (3) until F contains only one tree. This tree is the Homan tree.
Code implementation:
Copy codeThe Code is as follows: # include <iostream>
# Include <assert. h>
Using namespace std;
Struct HuffmanNode
{
Unsigned int weight;
Unsigned int parent, leftChild, rightChild;
HuffmanNode ()
{
Weight = 0; parent = 0; leftChild = 0; rightChild = 0;
}
};
Void Select (const HuffmanNode * & nodelist, const int length, int & a, int & B)
{
Int min = 1000000, min2 = 1000000;
For (int I = 0; I <length; I ++)
{
If (min> nodelist [I]. weight & nodelist [I]. parent = 0)
{
Min = nodelist [I]. weight;
A = I;
}
}
For (int j = 0; j <length; j ++)
{
If (j! = A & min2> nodelist [j]. weight & nodelist [j]. parent = 0)
{
Min2 = nodelist [j]. weight;
B = j;
}
}
}
Char ** HuffmanCoding (const int * w, const int n)
{
Assert (w! = NULL );
Int I, min1, min2;
Int m = 2 * n-1;
HuffmanNode * nodelist = new HuffmanNode [m] ();
For (I = 0; I <n; I ++)
{
Nodelist [I]. weight = w [I];
Nodelist [I]. parent = 0;
}
For (I = n; I <m; I ++)
{
Select (nodelist, I, min1, min2 );
Nodelist [min1]. parent = I;
Nodelist [min2]. parent = I;
Nodelist [I]. weight = nodelist [min1]. weight + nodelist [min2]. weight;
Nodelist [I]. rightChild = min2;
Nodelist [I]. leftChild = min1;
Nodelist [I]. parent = 0;
}
Char temp [20];
Char ** code = new char * [n];
For (I = 0; I <n; I ++)
{
Int j = I;
Int index = 0;
While (j! M-1)
{
If (j = nodelist [nodelist [j]. parent]. leftChild) temp [index ++] = '0 ';
Else temp [index ++] = '1 ';
J = nodelist [j]. parent;
}
Temp [index] = '\ 0 ';
Code [I] = new char [index + 1];
Strcpy (code [I], temp );
}
Delete nodelist;
Return code;
}
Int main ()
{
Const int size = 6;
Char word [size] = {'A', 'B', 'C', 'D', 'E', 'F'}; // encoded character
Int w [size] = {4, 3, 2, 1, 7, 8}; // weight
Char ** code;
Code = HuffmanCoding (w, size );
Assert (code! = NULL );
For (int I = 0; I <size; I ++)
{
Cout <word [I] <"is coded as" <code [I] <endl;
}
// Pay attention to the release of the second-level pointer.
For (int j = 0; j <size; j ++)
{
Delete [] code [j];
}
Delete [] code;
Return 0;
}