/*
Name: Heman encoding (priority queue implementation)
Copyright:
Author: self-defeating
Date: 28/09/14
Description:
A common Linear table is transformed into a Heman tree by using the priority queue, and then carried out the Heman encoding to obtain a cipher book that simultaneously records the plaintext and the corresponding encoding.
Using the priority queue (minimum heap) to construct a Heman tree is an efficient method, which is much faster than traversing the entire linear table every time.
When constructing a cipher book, I make sure that the array of the cipher book is sorted in ascending order, so that each time a new node is inserted, it can be searched and inserted in half, which is more efficient.
An ordered cipher book can greatly improve the search efficiency when it encodes plaintext into a ciphertext.
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
# Include <math. h>
# Include <time. h>
# Define Max size 300
# Define OK 1
# Define error 0
# Define true 1
# Define false 0
Typedef char elemtype;
Typedef int status; // function type, whose value is the function result status code, such as OK.
Typedef struct {
Elemtype data;
Int weight;
Int parent;
Int lchild;
Int rchild;
} Hnodetype; // node struct
Typedef struct {
Elemtype data; // plaintext Information
Char * Code; // ciphertext Information
} Hcodetype; // encoding struct
Void buildminheap (hnodetype HF [], int N); // converts a linear table HF into a minimum heap
Void minheapsiftdown (hnodetype HF [], int N, int POS); // adjust the POs element of the binary heap to satisfy the features of the minimum heap.
Void huffmantree (hnodetype HF [], int N); // converts a linear table into a Heman tree.
Void displaycodebook (hcodetype cb [], int N); // display the plaintext in the password book and the corresponding password information
Int insertcodelib (hcodetype cb [], int N, elemtype X, char * s, int Len); // Insert the new node into the encoding linear table of the cipher book
Int hfcoding (hcodetype cb [], hnodetype HF [], int N); // generates a password Based on the Heman tree
Int main (void)
{
Hnodetype HF [maxsize * 2]; // Heman tree
Hcodetype codebook [maxsize]; // password book
Int lenhf = 5; // Number of leaf nodes in the Heman tree
Int lencb; // The length of the cipher book (equal to lenhf)
Int I;
For (I = 0; I <lenhf; I ++) // randomly generated data and weights
{
HF [I]. Data = 'A' + I;
HF [I]. Weight = rand () % 10 + 1;
Printf ("% C: % d \ n", HF [I]. Data, HF [I]. weight );
}
Huffmantree (HF, lenhf); // converts a linear table into a Heman tree.
Lencb = hfcoding (codebook, Hf, lenhf * 2-1); // generates a cryptographic book based on the Heman tree
Puts ("cipher book :");
Displaycodebook (codebook, lencb); // display the plaintext in the password book and the corresponding password information
Return 0;
}
/*
Function: converts a linear table into a Heman tree.
Initial Condition: the non-decreasing linear table HF already exists.
Operation Result: first convert the original linear table to the smallest heap (priority queue), and then process the smallest heap in the following order:
Delete the two nodes with the minimum weight, add them to the end of the queue, add the weights of the two smallest nodes, generate a new node, and add the new nodes to the priority queue.
The minimum heap is constantly adjusted until only one node is left, that is, the Heman tree is obtained.
Note: each time a new node is generated, you only need to set the weight of the new node and the information of the child node. The information of the parent node is calculated after the Heman tree is constructed.
*/
Void huffmantree (hnodetype HF [], int N)
{
Int I;
Int Len = 2 * n-1;
Int front = n-1;
Int rear = len-1;
Buildminheap (HF, N );
For (I = 0; I <Len; I ++) // initialize the child node location
{
HF [I]. lchild = Hf [I]. rchild =-1;
}
While (front> 0)
{
// Delete the two nodes with the minimum weight and add them to the end of the queue
HF [rear] = Hf [0];
HF [0] = Hf [Front];
Minheapsiftdown (HF, front + 1, 1); // adjust the 1st elements of the binary heap downward to meet the minimum heap features.
HF [rear-1] = Hf [0];
// Set the weight of the new node and the child node Information
HF [0]. Weight = Hf [rear]. Weight + HF [rear-1]. weight;
HF [0]. lchild = rear;
HF [0]. rchild = rear-1;
Minheapsiftdown (HF, front, 1); // adjust the 1st elements of the binary heap downward to meet the minimum heap features.
Rear-= 2; // because there are two nodes added to the new queue, the team's tail indicates moving forward two places
Front --; // because there are (2-1 = 1) nodes out of the column, the first line indicates moving forward by 1
}
// Calculate the location of the parent node
HF [0]. Parent =-1;
For (I = 0; I <Len; I ++)
{
HF [HF [I]. lchild]. Parent = Hf [HF [I]. rchild]. Parent = I;
}
}
/*
Function: Insert a new node into the encoding linear table of the cipher book.
Initial Condition: the plaintext and ciphertext information of each encoding are recorded in X and S respectively, and Len is the length of string S.
Operation Result: First, find the insert position, insert the new node, and copy the information of x and S to the new node. Note that the node encoding path stored in S is from the leaf to the root, you need to copy the data to the cipher book in reverse order.
*/
Int insertcodelib (hcodetype cb [], int N, elemtype X, char * s, int Len) // sort by binary search and insert
{
Int I, mid;
Int left = 0, Right = n-1;
While (left <= right) // returns a half-width query for the insert position.
{
Mid = (left + right)/2;
If (x> cb [Mid]. Data)
{
Left = Mid + 1;
}
Else
{
Right = mid-1;
}
}
// Insert new data
For (I = N; I> left; I --)
{
CB [I] = CB [I-1];
}
CB [left]. Code = (char *) malloc (sizeof (char) * (LEN + 1 ));
If (! CB [left]. Code)
{
Printf ("out of space! ");
Exit (0 );
}
For (I = len-1; I> = 0; I --) // copy the string of S to code in reverse order
{
CB [left]. Code [len-1-i] = s [I];
}
CB [left]. Code [Len] = '\ 0 ';
CB [left]. Data = X;
Return n + 1;
}
/*
Function: generate a cryptographic book based on the Heman tree.
Initial Condition: HF already exists in the Homan tree, and N is the length of the Homan tree.
Operation Result: encode the leaf node of the Heman tree and store it in the cipher book. The length of the cipher book is returned.
Perform binary search and insert on each node in the cryptographic book to construct a non-descending sequence for search and encoding.
*/
Int hfcoding (hcodetype cb [], hnodetype HF [], int N)
{
Int I, J, K, F;
Int Top = 0; // construct a stack for the encoding structure. Top indicates the top of the stack.
Char STR [maxsize]; // stores the encoded temporary string
For (I = 0; I <n; I ++)
{
If (HF [I]. lchild =-1) // is the leaf node.
{
J = I;
F = Hf [J]. parent;
K = 0;
While (F! =-1) // the encoding of the node is stored in reverse order. Note that the path of the node encoding is from the leaf to the root. Copy the Node Code in reverse order when it is stored in the cipher book.
{
If (j = Hf [f]. lchild)
{
STR [k ++] = '0 ';
}
Else
{
STR [k ++] = '1 ';
}
J = F;
F = Hf [J]. parent;
}
STR [k] = '\ 0 ';
Top = insertcodelib (CB, top, HF [I]. Data, STR, k); // sort by binary search and insert
}
}
Return top;
}
/*
Function: display the plaintext and password information in the password book.
Initial Condition: the password already exists.
Operation Result: the plaintext and password information in the password book are displayed.
*/
Void displaycodebook (hcodetype cb [], int N)
{
Int I, J;
For (I = 0; I <n; I ++)
{
Printf ("% C:", CB [I]. data );
Puts (cb [I]. Code );
}
}
/*
Function: downward adjust the POs element of the binary heap to satisfy the features of the minimum heap.
Initial Condition: the least heap HF already exists, and only the POs element does not meet the features
Operation Result: The POS element of the binary heap is adjusted downward to satisfy the features of the minimum heap.
*/
Void minheapsiftdown (hnodetype HF [], int N, int POS)
{
Hnodetype temp = Hf [pos-1];
Int child = POS * 2; // point to left child
While (Child <= N)
{
If (Child <n & HF [Child-1]. weight> HF [child]. Weight) // if you have a right child, and the right child is smaller, locate the right child.
Child + = 1;
If (HF [Child-1]. weight <temp. Weight) // move the child node value up to ensure that the parent is greater than the child
{
HF [pos-1] = Hf [Child-1];
Pos = child;
Child = POS * 2;
}
Else
Break;
}
HF [pos-1] = temp; // adjust temp down to the appropriate position
}
/*
Function function: converts a linear table HF into a minimum heap.
Initial Condition: The linear table HF already exists.
Operation Result: the HF of the linear table is transformed into a minimum heap.
*/
Void buildminheap (hnodetype HF [], int N)
{
Int I;
For (I = n/2; I> 0; I --)
{
Minheapsiftdown (HF, N, I );
}
}
Heman coding (priority queue implementation)