The node structure of Huffman tree
typedef struct _HUFF_NODE ... {
float weight; Node weights
int lchild; node of the left child
int rchild; node of the right child
int parent; Node's parent node
} huff_node, * phuff_node;
Huffman Tree
typedef struct _HUFF_TREE ... {
int leaf_num; The Tree middle node is the number of nodes that need to be encoded
int node_num; Corresponding to the number of leaf nodes, Huffman tree in total need to 2*leaf_num-1 nodes
Phuff_node nodes; All nodes in the tree, with an array to store these nodes
char** Huff_code; Huffman coding for leaf nodes
} Huff_tree;
Initialize a Huffman tree, including the initialization of allocating space and weights for this tree.
void Init_huff_tree (FLOAT * weights, int leaf_num, Huff_tree & Tree);
Create a Huffman tree, if it must have been initialized
void Create_huff_tree (Huff_tree & Tree);
Print a tree has been created a good Huffman tree of the corresponding Huffman code
void Print_huff_code (Huff_tree & Tree);
Destroy a Huffman tree, release the initialization of the requested node and coding space
void Destroy_huff_tree (Huff_tree & Tree);
Initialize a tree Huffman tree
void Init_huff_tree (FLOAT * weights, int leaf_num, Huff_tree & Tree)
... {
Save the number of leaf nodes
Tree.leaf_num = Leaf_num;
Calculate the number of nodes required by the whole Huffman tree according to the leaf node points
In a binary tree, the nodes with degrees 0, 1 and 2 have the following relationships:
n = n0 + n1 + n2 and n = n1 + 2*n2 + 1
Therefore, there are n2 = N0-1
In the Huffman tree, only the degree of 0 and 2 nodes
Therefore, n = n0 + N2 = 2*n0-1
Tree.node_num = 2 * leaf_num-1;
Allocate space for a node
Tree.nodes = new Huff_node[tree.node_num];
if (tree.nodes = NULL) ... {
printf ("Memry out.");
Exit (1);
}
Allocate stored Huffman-encoded character arrays for each leaf node
Tree.huff_code = new Char*[leaf_num];
if (Tree.huff_code = NULL) ... {
printf ("Memory out.");
Exit (1);
}
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.