Harman Coding Algorithm

Source: Internet
Author: User
Huffman. h
# Ifndef _ huffman_h_fe
# DEFINE _ huffman_h_fe // The node Structure of the Harman tree
Typedef struct _ huff_node ...{
Float weight; // node weight
Int lchild; // left child of the node
Int rchild; // The right child of the node
Int parent; // The parent node of the node.
} Huff_node, * phuff_node; // the User-Defined tree
Typedef struct _ huff_tree ...{
Int leaf_num; // The number of nodes to be encoded in the middle of the tree.
Int node_num; // corresponds to the number of leaf nodes. Two leaf_num-1 nodes are required in the Harman tree.
Phuff_node nodes; // All nodes in the tree, which are stored in an array
Char ** huff_code; // The Harman code corresponding to the leaf node
} Huff_tree; // initialize a user-defined tree, including allocating space for the tree and initializing the weights.
Void init_huff_tree (float * weights, int leaf_num, huff_tree & tree); // create a user-defined tree, provided that it has been initialized
Void create_huff_tree (huff_tree & tree); // print the Harman code corresponding to the leaf node of the created Harman tree
Void print_huff_code (huff_tree & tree); // destroy a user-defined tree, and release the node and encoding space applied for during initialization.
Void destroy_huff_tree (huff_tree & tree); # endifhuffman. cpp
# Include <stdio. h>
# Include <stdlib. h>
# Include <float. h>
# Include <memory. h>
# Include "Huffman. H" Void huff_encode (huff_tree & tree); // initialize a Harman tree
Void init_huff_tree (float * weights, int leaf_num, huff_tree & tree)
...{
// Number of leaf nodes saved
Tree. leaf_num = leaf_num; // calculate the number of nodes required by the entire table store based on the number of leaf nodes.
// In a binary tree, nodes with an outbound degree of 0, 1, and 2 have the following relationships:
// N = N0 + N1 + N2 and n = N1 + 2 * N2 + 1
// Therefore, n2 = N0-1
// Only the nodes with an outbound degree of 0 and 2 are in the Harman tree.
// Therefore, n = N0 + n2 = 2 * N0-1
Tree. node_num = 2 * leaf_num-1; // allocate space for the node
Tree. nodes = new huff_node [tree. node_num];
If (tree. nodes = NULL )...{
Printf ("memry out .");
Exit (1 );
} // Assign an array of characters that store the Harman encoding to each leaf node
Tree. huff_code = new char * [leaf_num];
If (tree. huff_code = NULL )...{
Printf ("memory out .");
Exit (1 );
}
Else ...{
For (INT I = 0; I <leaf_num; I ++ )...{
If (tree. huff_code [I] = new char [leaf_num + 1]) = NULL )...{
Printf ("memory out .");
Exit (1 );
}
Else ...{
Memset (tree. huff_code [I], 0, leaf_num + 1 );
}
}
} // Initialize the node Value
For (INT I = 0; I <tree. node_num; I ++ )...{
// Leaf nodes are stored in the front of the node array and their initial weights are assigned.
If (I <leaf_num )...{
Tree. nodes [I]. Weight = weights [I];
}
Else ...{
Tree. nodes [I]. Weight = 0;
} // Left and right children and Father's Day pointer (array index) initialization is null (-1)
Tree. nodes [I]. Parent =-1;
Tree. nodes [I]. lchild =-1;
Tree. nodes [I]. rchild =-1;
}
} // Create a complete table based on the initialized User-Defined tree, including the User-Defined code for each leaf node.
Void create_huff_tree (huff_tree & tree)
...{
Float min1 = flt_max, min2 = flt_max; // min1 stores the current minimum value and min2 stores the current small value.
Int lchild_temp =-1, rchild_temp =-1; // structure of the Binary Tree
// Select the two nodes with the minimum weight from the nodes with no parent nodes in each round.
// The left and right child pointers of the next node in the node array point to the smallest and secondary nodes respectively.
// And set the weight to the sum of the preceding two nodes.
// Set the parent node pointer of the minimum node and secondary node to the index of the node.
For (INT I = tree. leaf_num; I <tree. node_num; I ++ )...{
// Select the smallest two nodes from the arrays with no parent nodes
For (Int J = 0; j <I; j ++ )...{
If (tree. nodes [J]. Parent =-1 )...{
If (tree. nodes [J]. weight <min1 )...{
Min2 = min1;
Rchild_temp = lchild_temp;
Min1 = tree. nodes [J]. weight;
Lchild_temp = J;
}
Else if (tree. nodes [J]. weight <min2 )...{
Min2 = tree. nodes [J]. weight;
Rchild_temp = J;
}
}
} Tree. nodes [lchild_temp]. Parent = I;
Tree. nodes [rchild_temp]. Parent = I;
Tree. nodes [I]. lchild = lchild_temp;
Tree. nodes [I]. rchild = rchild_temp;
Tree. nodes [I]. Weight = min1 + min2; min1 = flt_max;
Min2 = flt_max;
Lchild_temp =-1;
Rchild_temp =-1;
} // Encode the leaf node of the Harman tree
Huff_encode (tree );
} // Encode the leaf node of the Harman tree
Void huff_encode (huff_tree & tree)
...{
// A temporary array that stores the Harman encoding from the back to the back
Char * code_temp = new char [tree. leaf_num + 1];
Memset (code_temp, 0, tree. leaf_num + 1 );
// The write pointer is initially at the end of the temporary Array
Int start_pos = tree. leaf_num; // starts from the leaf node and continues up until the root node position.
// Encode each leaf node
For (INT I = 0; I <tree. leaf_num; I ++ )...{
Int cur_node = I;
Int parent = tree. nodes [I]. parent;
// Until the root node
While (parent! =-1 )...{
// If the current node is the left child of the parent node, It is encoded as 0
If (tree. nodes [Parent]. lchild = cur_node )...{
Code_temp [-- start_pos] = '0 ';
}
// If the current node is the right child of the parent node, It is encoded as 1
Else if (tree. nodes [Parent]. rchild = cur_node )...{
Code_temp [-- start_pos] = '1 ';
}
// Continue up
Cur_node = parent;
Parent = tree. nodes [Parent]. parent;
} // Copy the encoding in the temporary encoding array to the encoding bucket corresponding to the node in the Harman tree.
Memcpy (tree. huff_code [I], (code_temp + start_pos), (tree. leaf_num-start_pos + 1); memset (code_temp, 0, tree. leaf_num + 1 );
Start_pos = tree. leaf_num;
} Delete [] code_temp;
} // Print the Harman encoding of each leaf node
Void print_huff_code (huff_tree & tree)
...{
For (INT I = 0; I <tree. leaf_num; I ++ )...{
Printf ("node weight: %. 2f", tree. nodes [I]. weight );
Printf ("Huffman code: % s", tree. huff_code [I]);
}
} // Destroy the Harman tree
Void destroy_huff_tree (huff_tree & tree)
...{
// Release Node space
If (tree. nodes! = NULL )...{
Delete [] tree. nodes;
} // Release the coding Space
If (tree. huff_code! = NULL )...{
For (INT I = 0; I <tree. leaf_num; I ++ )...{
If (tree. huff_code [I]! = NULL )...{
Delete [] tree. huff_code [I];
}
}
Delete [] tree. huff_code;
}
} Example
# Include <stdio. h>
# Include <stdlib. h>
# Include "Huffman. H" int main (void)
...{
Float weights [10] =... {14, 3, 7, 9, 34, 8, 12, 1, 5, 20 };
Huff_tree; printf ("weights of the nodes are :");
For (INT I = 0; I <10; I ++ )...{
Printf ("%. 2f", weights [I]);
}
Printf (""); init_huff_tree (weights, 10, huff_tree );
Create_huff_tree (huff_tree );
Print_huff_code (huff_tree );
Destroy_huff_tree (huff_tree); System ("pause"); 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.