[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
As we mentioned above, the next important part of the creation of the Harman tree is the sorting of the Harman tree. However, because the sorting content is a data structure, we need to adopt a general data sorting algorithm, which has been involved in my previous blog (general algorithm design ). Therefore, all we need to do is compile the compare and swap functions. The general bubble code is as follows,
Void bubble_sort (void * array [], int length, int (* compare) (void *, void *), void (* swap) (void **, void **))
{
Int outer;
Int inner;
For (outer = length-1; outer> 0; outer --){
For (inner = 0; inner <outer; inner ++ ){
If (compare (array [inner], array [inner + 1])
Swap (& array [inner], & array [inner + 1]);
}
}
Return;
}
Void bubble_sort (void * array [], int length, int (* compare) (void *, void *), void (* swap) (void **, void **))
{
Int outer;
Int inner;
For (outer = length-1; outer> 0; outer --){
For (inner = 0; inner <outer; inner ++ ){
If (compare (array [inner], array [inner + 1])
Swap (& array [inner], & array [inner + 1]);
}
}
Return;
}
The compare and swap codes are as follows,
Int compare (void * a, void * B)
{
HUFFMAN_NODE * node1 = (HUFFMAN_NODE *);
HUFFMAN_NODE * node2 = (HUFFMAN_NODE *) B;
Return node1-> frequence> node2-> frequence? 1: 0;
}
Void swap (void ** a, void ** B)
{
HUFFMAN_NODE * median;
HUFFMAN_NODE ** node1 = (HUFFMAN_NODE **);
HUFFMAN_NODE ** node2 = (HUFFMAN_NODE **) B;
Median = * node1;
* Node1 = * node2;
* Node2 = median;
}
Int compare (void * a, void * B)
{
HUFFMAN_NODE * node1 = (HUFFMAN_NODE *);
HUFFMAN_NODE * node2 = (HUFFMAN_NODE *) B;
Return node1-> frequence> node2-> frequence? 1: 0;
}
Void swap (void ** a, void ** B)
{
HUFFMAN_NODE * median;
HUFFMAN_NODE ** node1 = (HUFFMAN_NODE **);
HUFFMAN_NODE ** node2 = (HUFFMAN_NODE **) B;
Median = * node1;
* Node1 = * node2;
* Node2 = median;
}
With the create function and sort function, you can create the user tree,
HUFFMAN_NODE * create_huffman_tree (HUFFMAN_NODE * huffmanNode [], int length)
{
HUFFMAN_NODE * head = NULL;
If (NULL = huffmanNode | length <= 1)
Return NULL;
While (length> 1 ){
Bubble_sort (void **) huffmanNode, length, compare, swap );
Head = create_new_node ('\ 0', huffmanNode [0]-> frequence + huffmanNode [1]-> frequence );
Assert (NULL! = Head );
Head-> left = huffmanNode [0];
Head-> right = huffmanNode [1];
HuffmanNode [0]-> parent = head;
HuffmanNode [0]-> symbol = 1;
HuffmanNode [1]-> parent = head;
HuffmanNode [1]-> symbol = 0;
Memmove (& huffmanNode [0], & huffmanNode [2], sizeof (HUFFMAN_NODE *) * (length-2 ));
HuffmanNode [length-2] = head;
Length --;
}
Return head;
}
HUFFMAN_NODE * create_huffman_tree (HUFFMAN_NODE * huffmanNode [], int length)
{
HUFFMAN_NODE * head = NULL;
If (NULL = huffmanNode | length <= 1)
Return NULL;
While (length> 1 ){
Bubble_sort (void **) huffmanNode, length, compare, swap );
Head = create_new_node ('\ 0', huffmanNode [0]-> frequence + huffmanNode [1]-> frequence );
Assert (NULL! = Head );
Head-> left = huffmanNode [0];
Head-> right = huffmanNode [1];
HuffmanNode [0]-> parent = head;
HuffmanNode [0]-> symbol = 1;
HuffmanNode [1]-> parent = head;
HuffmanNode [1]-> symbol = 0;
Memmove (& huffmanNode [0], & huffmanNode [2], sizeof (HUFFMAN_NODE *) * (length-2 ));
HuffmanNode [length-2] = head;
Length --;
}
Return head;
} The above Code completely writes the creation process of the huffman tree. How do we know the symbol encoding? This is actually not difficult, because the root node knows that we only need to traverse the node from the bottom up to print the encoding, but the encoding is in reverse order,
Void print_code_for_str (HUFFMAN_NODE * pNode, HUFFMAN_NODE * head)
{
If (NULL = pNode | NULL = head)
Return;
While (head! = PNode ){
Printf ("% d", pNode-> symbol );
PNode = pNode-> parent;
}
Return;
}
Void print_code_for_str (HUFFMAN_NODE * pNode, HUFFMAN_NODE * head)
{
If (NULL = pNode | NULL = head)
Return;
While (head! = PNode ){
Printf ("% d", pNode-> symbol );
PNode = pNode-> parent;
}
Return;
}
If you have doubts about the code, compile a test case to verify the code,
Void test ()
{
HUFFMAN_NODE * node1 = NULL;
HUFFMAN_NODE * node2 = NULL;
HUFFMAN_NODE * node3 = NULL;
HUFFMAN_NODE * node4 = NULL;
HUFFMAN_NODE * test [] = {node1 = create_new_node ('A', 0.1 ),
Node2 = create_new_node ('B', 0.2 ),
Node3 = create_new_node ('C', 0.3 ),
Node4 = create_new_node ('D', 0.4 ),
};
HUFFMAN_NODE * head = create_huffman_tree (test, sizeof (test)/sizeof (HUFFMAN_NODE *));
Print_code_for_str (node1, head );
Print_code_for_str (node2, head );
Print_code_for_str (node3, head );
Print_code_for_str (node4, head );
}
Void test ()
{
HUFFMAN_NODE * node1 = NULL;
HUFFMAN_NODE * node2 = NULL;
HUFFMAN_NODE * node3 = NULL;
HUFFMAN_NODE * node4 = NULL;
HUFFMAN_NODE * test [] = {node1 = create_new_node ('A', 0.1 ),
Node2 = create_new_node ('B', 0.2 ),
Node3 = create_new_node ('C', 0.3 ),
Node4 = create_new_node ('D', 0.4 ),
};
HUFFMAN_NODE * head = create_huffman_tree (test, sizeof (test)/sizeof (HUFFMAN_NODE *));
Print_code_for_str (node1, head );
Print_code_for_str (node2, head );
Print_code_for_str (node3, head );
Print_code_for_str (node4, head );
}
Summary:
(1) The Harman tree is not complicated. If the hand calculation can be successful, there should be no problems in programming.
(2) complex algorithms are built by small algorithms. Friends should lay a solid foundation on the basic algorithms.
(3) Pay attention to the reuse of algorithms. Here we use the general algorithm content.