Build and codec of The Harman tree, and build codec of The Harman tree

Source: Internet
Author: User

Build and codec of The Harman tree, and build codec of The Harman tree

 /*
* Implementation process: the user first constructs the User-Defined tree using the HuffmanTree () function, and then * starts from the bottom up (that is, starting from the node where the array serial number is zero) in the main () function) it is determined from the above layer. If it is on the left side of * parent node, it is set to 0. If it is on the right side, it is set to 1. Finally, the generated encoding is output. * ------------------------------------------------------------------------ */# Include <stdio. h> # include <stdlib. h ># include <cstring> const int MAXBIT = 100; const int MAXVALUE = 10000; const int MAXLEAF = 30; const int MAXNODE = MAXLEAF * 2-1; typedef struct {int bit [MAXBIT]; int start;} HCodeType; // The encoding structure typedef struct {int weight; int parent; int lchild; int rchild; int value;} HNodeType; // node struct // input and initialize Void node_input (HNodeType HuffNode [], const int & n) {int I; for (I = 0; I <n; I ++) {printf ("Please input weight of leaf node % d: \ n", I); scanf ("% d", & HuffNode [I]. weight); // The weight Value HuffNode [I]. parent =-1; HuffNode [I]. lchild =-1; HuffNode [I]. rchild =-1; // The initial value is-1 HuffNode [I]. value = I; // The current value is the lower mark. The actual value can be replaced with a letter} void out_put (HCodeType HuffCode [], const int & n) {int I, j; for (I = 0; I <n; I ++) {printf ("% d' S Huffman code is: ", I); for (j = HuffCode [I]. start + 1; j <n; j ++) {printf ("% d", HuffCode [I]. bit [j]);} printf ("start: % d", HuffCode [I]. start); printf ("\ n") ;}} void min_two (HNodeType HuffNode [], const int & n, const int & I, int & x1, int & x2) {int j, m1, m2; m1 = m2 = MAXVALUE; // m1 and m2 store two nodes with no parent node and the minimum node weight. // find the two nodes with the minimum and no parent node in all nodes, and merged into a binary tree for (j = 0; j <n + I; j ++) {if (HuffNode [j]. weight <m1 & HuffNode [J]. parent =-1) {m2 = m1; x2 = x1; m1 = HuffNode [j]. weight; x1 = j;} else if (HuffNode [j]. weight <m2 & HuffNode [j]. parent =-1) {m2 = HuffNode [j]. weight; x2 = j ;}}// construct a HuffmanTree (HNodeType HuffNode [], const int & n) {// I: cyclic variable, m1 and m2: Construct the weights of the two minimum weight nodes in different processes of the Harman tree. // x1 and x2: construct the sequence numbers of the two least weighted nodes in the array during the different process of the user tree. Int I, x1, x2; // cyclically construct the Huffman tree for (I = 0; I <n-1; I ++) {x1 = x2 = 0; min_two (HuffNode, n, i, x1, x2);/* set the parent node information of the two child nodes x1 and x2 */HuffNode [x1]. parent = n + I; HuffNode [x2]. parent = n + I; HuffNode [n + I]. weight = HuffNode [x1]. weight + HuffNode [x2]. weight; HuffNode [n + I]. lchild = x1; HuffNode [n + I]. rchild = x2; HuffNode [n + I]. parent =-1; // The Father of the new node is-1 printf ("x1.weight and x2.weight in round % d: % d, % d \ n", I + 1. HuffNode [x1]. weight, HuffNode [x2]. weight);/* for testing */printf ("\ n");}/* for (I = 0; I <n + 2; I ++) {printf ("Parents: % d, lchild: % d, rchild: % d, value: % d, weight: % d \ n", HuffNode [I]. parent, HuffNode [I]. lchild, HuffNode [I]. rchild, HuffNode [I]. value, HuffNode [I]. weight);} * // test} // end HuffmanTree // encode void encodeing (HNodeType HuffNode [], HCodeType HuffCode [], const int & n) {int I, j, c, p; HCodeType cd; for (I = 0; I <n; I ++) {cd. start = n-1; c = I; p = HuffNode [c]. parent; while (p! =-1) // The parent node has {if (HuffNode [p]. lchild = c) cd. bit [cd. start] = 0; else cd. bit [cd. start] = 1; cd. start --; // evaluate the encoding of a lower c = p; p = HuffNode [c]. parent; // set the next loop condition} // end while // Save the start position of the Harman encoding and encoding for (j = cd. start + 1; j <n; j ++) {HuffCode [I]. bit [j] = cd. bit [j];} HuffCode [I]. start = cd. start ;}// decode void decodeing (char string [], HNodeType Buf [], int Num) {int I, tmp = 0, code [1024]; int m = 2 * Num-1; char * num P; char num [1024]; for (I = 0; I <strlen (string); I ++) {if (string [I] = '0 ') num [I] = 0; else if (string [I] = '1') num [I] = 1; else return;} // convert it to a number I = 0; nump = & num [0]; while (nump <(& num [strlen (string)]) {tmp = m-1; while (Buf [tmp]. lchild! =-1) & (Buf [tmp]. rchild! =-1) {if (* nump = 0) {tmp = Buf [tmp]. lchild;} else tmp = Buf [tmp]. rchild; nump ++;} printf ("% d", Buf [tmp]. value) ;}} int main (void) {HNodeType HuffNode [MAXNODE]; // defines a node struct array HCodeType HuffCode [MAXLEAF], cd; // defines an encoding struct array, define a temporary variable to store the int I, j, n; char pp [100]; printf ("Please input n: \ n") for encoding "); scanf ("% d", & n); // input n nodes node_input (HuffNode, n); // construct the HuffmanTree (HuffNode, n); // encode the encode Ing (HuffNode, HuffCode, n); // output all the stored Harman encoding out_put (HuffCode, n); // The Decoding process printf ("Decoding? Please Enter code: \ n "); scanf (" % s ", & pp); decodeing (pp, HuffNode, n); return 0 ;}

Note: voidmin_two (HNodeTypeHuffNode [], constint & n, constint & I, int & x1, int & x2) can reduce the algorithm complexity to O (nlogn) through the minimum heap)


Building and coding/decoding of the Harman tree

Typedef struct {
Int weight;
Int flag;
Int parent;
Int leftchild;
Int rightchild;
} Haffnode;
Typedef struct
{
Int bit [MAXN];
Int start;
Int weight;
} Code;
Void haffman (int weight [], int n, haffnode hafftree [])
{
Int I, j, m1, m2, x1, x2;
For (I = 0; I <2 * n-1; I ++)
{
If (I <n) hafftree [I]. weight = weight [I];
Else hafftree [I]. weight = 0;
Hafftree [I]. parent =-1;
Hafftree [I]. flag = 0;
Hafftree [I]. leftchild =-1;
Hafftree [I]. rightchild =-1;
}
For (I = 0; I <n-1; I ++)
{
M1 = m2 = maxvalue;
X1 = x2 = 0;
For (j = 0; j <n + I; j ++)
{
If (hafftree [j]. weight <m1 & hafftree [j]. flag = 0)
{
M2 = m1;
X2 = x1;
M1 = hafftree [j]. weight;
X1 = j;
}
Else if (hafftree [j]. weight <m2 & hafftree [j]. flag = 0)
{
M2 = hafftree [j]. weight;
X2 = j;
}
}
Hafftree [x1]. parent = n + I;
Hafftree [x2]. parent = n + I;
Hafftree [x1]. flag = 1;
Hafftree [x2]. flag = 1;
Hafftree [n + I]. weight = hafftree [x1]. weight + hafftree [x2]. weight;
Hafftree [n + I]. leftchild = x1;
Hafftree [n + I]. rightchild = x2;
}
}
 
Hafman encoding and decoding problem: how can a sender construct a hafman tree based on the input content and encode it at the receiving end?

After learning about the coding tree. Start from the root node and read the binary code in the received message one by one. If the code is "0", go to the root node of the Left subtree; otherwise, go to the root node of the right subtree; once the leaf node is reached, the characters corresponding to the Code are translated. Then start decoding again from the root node until the binary message ends.

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.