Implementation of Huffman encoding c

Source: Internet
Author: User

Huffman encoding: Based on the Huffman tree, the data structure used is a binary tree.

Typedef int elemtype; typedef struct {elemtype weight; int parent, l_child, r_child;} binarytree; // 2. Construct the optimal binary tree void createhuffman (INT leafnum, binarytree * huffmantree) {// leafnum leaves, decide nodenum nodes int nodenum = 2 * leafnum-1; huffmantree = (binarytree *) malloc (nodenum * sizeof (binarytree )); // 0 unit also use // leafnum leaves, determine nodenum knot points, also decided (nodenum-1) bit encoding char * huffmancode; huffmancode = (char *) malloc (nodenum-1) * sizeof (CH AR); // initialization of the huffmantree: manually input the data weight of the leaf node. The default values of non-leaf nodes are 0 cout. <"Enter the leaf weight:"; int I; for (I = 0; I <nodenum; I ++) {if (I <leafnum) {CIN> huffmantree [I]. weight;} else {huffmantree [I]. weight = 0;} huffmantree [I]. parent = huffmantree [I]. rochelle child = huffmantree [I]. r_child = 0;} Int J; // J starting from leafnum, it refers to the node number that contains the new subtree for (j = leafnum; j <nodenum; j ++) {int W1 = 32767, W2 = 0; // minimum stored weight; int p = 0, q = 0; // Number of the minimum stored weight; int K; for (k = 0; k <j; k ++) {If (huffmantree [K ]. Parent = 0) // determine whether the node uses {If (huffmantree [K]. weight <W1) {W2 = W1; q = P; W1 = huffmantree [K]. weight; P = K;} else if (huffmantree [K]. weight <W2) {W2 = huffmantree [K]. weight; q = K ;}}// P corresponds to the left node, encoded as '0'; q corresponds to the left node, encoded as '1 '; huffmancode [p] = '0'; huffmancode [Q] = '1'; huffmantree [J]. weight = W1 + W2; huffmantree [J]. rochelle child = P; huffmantree [J]. r_child = Q; huffmantree [p]. parent = J; huffmantree [Q]. parent = J;} // 3. Search for the code huffmancodin from the subnode to the root node. G (int n, binarytree * huffmantree) // For each leaf, search for huffmancode from the leaf to the root. // For each leaf, the corresponding encoding length is codelengthconst int maxsize = 10; char iscode [maxsize] [maxsize]; int m; For (m = 0; m <leafnum; m ++) // start from the first leaf and find {int n = 0; iscode [m] [N] = huffmancode [m]; int parent = huffmantree [M]. parent; while (parent! = 0) {iscode [m] [++ N] = huffmancode [Parent]; parent = huffmantree [Parent]. parent;}; int X; cout <"no." <m + 1 <"leaf huffmancode ----"; for (x = 0; x <N; X ++) cout <iscode [m] [X]; cout <Endl ;}} void main () {binarytree * T = 0; int leafnum; printf ("Number of input leaves n = \ n"); CIN> leafnum; createhuffman (leafnum, T); While (1 );}

The compilation result is as follows:








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.