# Contains? " hfm.h "
# include & ltstring.h & gt
# include & ltmalloc.h & gt//malloc (), etc.
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & ltctype.h & gt
# include & lt restrictions.>
# include & ltiostream & gt
# Definition? Really? 1
# Definition? Fake? 1
# Definition? Okay? 1
# Definition? Mistakes? 1
# Definition? Not feasible? - 1
typedef? int? Status;
typedef? int? Boolean type;
/************************************************************************/
/*? Optimal Binary Tree Abbreviation: Huffman Tree? */
/************************************************************************/
//Huffman tree structure
; ? typedef? Structure {
Not signed? int? Weight; //weight
Not signed? int? Parents? lchild? Parent node and left and right child nodes of rchild// tree
}HTNode,? * Huffington tree;
typedef? char * * HuffmanCode
//Returns the serial number of the root node of the tree with the smallest weight among I nodes, which can be called by select ().
int? Min(HuffmanTree? t,? int? i){
int? j,? Flag;
Not signed? int? k? =? UINT _ MAX//% d-& gt; UINT_MAX? =? - 1,% u-& gt; A very large number.
For what? (j? =? 1; ? j? & lt=? Me; ? j++)
What if? (T[j]。 Weight? & lt? k? & amp& amp? T[j]。 Parents? ==? 0)
k? =? T[j]。 What about the weight? Flag? =? j; //
T[ flag] Parents? =? 1; //Mark the parent as 1 to avoid secondary search.
Return? Flag; ? //Return
}
Invalid? Select (HuffmanTree? t,? int? Me, int & s 1,int & amp? s2){
//Select the root node serial numbers of the two trees with the smallest weights from I nodes, and s 1 is the one with the smaller serial number.
int? j;
s 1? =? Min(T,I);
s2? =? Min(T,I);
What if? (s 1? & gt? s2){
j? =? s 1;
s 1? =? S2;
s2? =? j;
}
}
//The tree represented by //HuffmanCode decodes binary values.
Invalid? HuffmanCoding(HuffmanTree? & ampHT,? Hoeffmann code & ampHC,? int*? w,? int? n){
//w stores the weight of n characters (all >;; 0), construct Huffman tree HT, and get huffman encoding HC with n characters.
int? m,? Me? s 1,? s2,? Start;
Not signed? c,? f;
char*? CD; ?
//Allocate storage space
Hefmantry? p;
What if? (n? & lt= 1)
Return;
//n characters (leaf nodes) have 2n- 1 tree nodes, so tree node m.
m? =? 2? *? n? -? 1;
HT? =? (HuffmanTree)malloc((m? +? 1)* sizeof(ht node)); //Element 0 is not used.
//This step is to initialize the leaf nodes of the Huffman tree.
For what? (p? =? HT? +? 1,? Me? =? 1; ? Me? & lt=? n; ? ++me? ++p,++w)
{
(*p)。 Weight? =? * w;
(*p)。 lchild? =? 0;
(*p)。 Childre? =? 0;
(*p)。 Parents? =? 0;
}
//This step is to initialize the non-leaf nodes of the Huffman tree.
For what? (; ? Me? & lt=? m; ? ++me? ++p){
(*p)。 Parents? =? 0;
}
/************************************************************************/
/*? After the preparations are over? , start building Hoffman tree?
/************************************************************************/
For what? (me? =? n? +? 1; ? Me? & lt=? m; ? i++)
{
//Select the node with parent=0 and minimum weight in HT[ 1~i- 1], and its serial numbers are S 1 and S2 respectively.
Select (HT, me? -? 1,? s 1,? S2); //Pass reference
HT[s 1]。 Parents? =? HT[s2]。 parent=? Me;
HT[i]。 lchild? =? s 1;
HT[i]。 Childre? =? S2;
HT[i]。 Weight? =? HT[s 1]。 Weight? +? HT[s2]。 Weight;
}
/************************************************************************/
/*? Inverse huffman encoding */
/************************************************************************/
//Assign N character-coded head pointer vectors, ([0] not used)
HC? =? (HuffmanCode)malloc((n? +? 1)* sizeof(char *);
cd? =? (char *)malloc(n * sizeof(char)); ? //Assign a coded workspace.
cd[n? -? 1]? =? '\0'; ? //terminator
For what? (me? =? 1; ? Me? & lt=? n; ? I++)// Traverse each node.
{
Start? =? n? -? 1;
For what? (c? =? Me? f? =? HT[i]。 Parents; ? f? ! =? 0; ? c? =? f,f? =? HT[f]。 Parents) {? //Traverse from each node to the root node
//Reverse coding from leaf node to root node
What if? (HT[f]。 lchild? ==? c)
cd[ - start]? =? '0';
other
cd[ - start]? =? ' 1';
}
HC[i]? =? (char*)malloc((n? -? start)* sizeof(char)); //Generate a memory to store characters.
//Allocate space for the i-th character encoding.
strcpy(HC[i],? & ampCD[start]); //Assign the strings in the cd to the cd.
}
Free (CD); //Release resources
}
//Function declaration
int? Min(HuffmanTree? t,? int? I); ? //Find the serial number with the smallest weight among I nodes and return it.
Invalid? Select (HuffmanTree? t,? int? Me? int & amp? s 1,? int & amp? S2); ? //Choose the smallest (left) from the two smallest weights to give s 1 and the right to s2.
Invalid? HuffmanCoding(HuffmanTree? & ampHT,? Hoeffmann code & ampHC,? int*? w,? int? n); //huffman encoding sum decoding
int? main(){
Hefmantry? HT;
HuffmanCode? HC;
int? *w,? n,? Me;
Printf ("Please enter the weight number (>1):");
scanf_s("%d ",& ampn);
W? =? (int *)malloc(n * sizeof(int));
Printf ("Please enter %d weights (shaping) in turn: \n", n);
For what? (me? =? 0; ? Me? & lt=? n? -? 1; i++)
{
scanf_s("%d ",w+I);
}
HuffmanCoding(HT,HC,? w,? n);
For what? (me? =? 1; ? Me? & lt=? n; i++)
{
puts(HC[I]);
}
Return? 0;
}