#include
#include
Typedef struct
{
int weight;
Int the parent, lchild rchild;
} HTNode, * HuffmanTree;
Void InitTree (HuffmanTree & amp; HT)//initializes the
{
If (n<=1) return 0;
M=2 * n - 1;
HT=(int *) malloc (sizeof (m + 1));
for(i=1; i<=m; I++)
{
HT [I]. Parent=0;
HT [I] lchild=0;
HT [I] rchild=0;
HT [I]. Weight=0;
}
for(i=1; i<=n; I++)
The scanf (" % d ", & amp; HT [I]. Weight);
}
Void CreatHuffmanTree (HuffmanTree & amp; HT, int n) creates a Huffman tree//
{int m, I;
Int s1, s2;
for(i=1; i<=n; I++)
The scanf (" % d ", HT [I] weight);
For (I=n + 1; i<=m; I++)
{
Select (HT, I - 1, s1, s2);//select two minimum value of the HT
HT (s1). The parent=HT (s2). The parent=I;
HT [I] lchild=s1; HT [I] rchild=s2;
HT [I]. Weight=HT [s1]. Weight + HT (s2). The weight;
}
}
Void the Select (HuffmanTree & amp; HT, int (I - 1), int int s1, s2)//selected array
the two smallest Numbers{
Int min1=HT [1]. Weight;
Int min2=HT [1]. Weight;
for(i=1; i<=n; I++)
{if (HT [I] weight
S1=I; }
for(i=1; i<=n; I++)
{
If (HT [I] weight
S2=I; }
}
Void PrintHuffmanTree (HuffmanTree & amp; HT, int I)
{int m;
for(i=1; i<=m; I++)
{
Printf (" node number is: \ n ", I);
Printf (" weight: \ n ", HT [I] weight);
Parent: printf (" \ n ", HT [I] the parent).
Printf (" lchild: \ n ", HT [I] lchild);
Printf (" rchild: \ n ", HT [I] rchild);
}
}
Int main ()
{int n;
Int I=0, m=0;
The scanf (" % d ", & amp; N);
Printf (" please enter the number of leaf nodes in the Huffman tree: \ n ", n);
M=2 * n - 1;
HuffmanTree HT.
HT=(int *) malloc (siezof ());
InitTree (HT);
Printf (" start creating Huffman tree \ n ");
Printf (" please enter the weight of each leaf node ", HT [I] weight);
CreatHuffmanTree (HT, n);
PrintHuffmanTree (HT, I);
return ;
}
CodePudding user response:
Best wishes! I copy your code into the compiler to find red, you are just learning or just learning c language data structure?CodePudding user response:
Just learning data structureMay c also is not very good
CodePudding user response:
All of my error, the defined function that line, but I don't know how to changeCodePudding user response:
#include
#include
Typedef struct
{
int weight;
Int the parent, lchild rchild;
} HTNode, * HuffmanTree;
Void InitTree (HuffmanTree & amp; HT, int n)//initializes the
{
Int j, I;
If (n & lt;=1) return;
M=2 * n - 1;
Memset (HT, 0, sizeof (HuffmanTree));
For (I=1; I & lt;=m; I++)
{
HT [I]. Parent=0;
HT [I] lchild=0;
HT [I] rchild=0;
HT [I]. Weight=0;
}
For (I=1; I & lt;=n; I++)
The scanf (" % d ", & amp; HT [I]. Weight);
}
Void the Select (HuffmanTree & amp; HT, int n, int & amp; S1, int & amp; S2)//selected array
the two smallest Numbers{
int i=0;
Int min1=HT [1]. Weight;
Int min2=HT [1]. Weight;
For (I=1; I & lt;=n; I++)
{
If (HT [I] weight & lt; Min1)
Min1=HT [I]. Weight;
S1=I;
}
For (I=1; I & lt;=n; I++)
{
If (HT [I] weight & lt; Min2 & amp; & Min2!=min1)
Min2=HT [I]. Weight;
S2=I;
}
}
Void CreatHuffmanTree (HuffmanTree & amp; HT, int int n, m) creates a Huffman tree//
{
Int s1=0, s2=0;
for (int i=1; I & lt;=n; I++)
The scanf (" % d ", HT [I] weight);
For (int I=n + 1; I & lt;=m; I++)
{
Select (HT, I - 1, s1, s2);//select two minimum value of the HT
HT (s1). The parent=HT (s2). The parent=I;
HT [I] lchild=s1; HT [I] rchild=s2;
HT [I]. Weight=HT [s1]. Weight + HT (s2). The weight;
}
}
Void PrintHuffmanTree (HuffmanTree & amp; HT, int m)
{
int i=0;
For (I=1; I & lt;=m; I++)
{
Printf (" node number is: \ n ", I);
Printf (" weight: \ n ", HT [I] weight);
Parent: printf (" \ n ", HT [I] the parent).
Printf (" lchild: \ n ", HT [I] lchild);
Printf (" rchild: \ n ", HT [I] rchild);
}
}
Int main ()
{
int n;
Int I=0, m=0;
The scanf (" % d ", & amp; N);
Printf (" please enter the number of leaf nodes in the Huffman tree: \ n ", n);
M=2 * n - 1;
HuffmanTree HT.
//HT=(int *) malloc (siezof ());
InitTree (HT, n);
Printf (" start creating Huffman tree \ n ");
For (I=0; I & lt; n; I++)
{
Printf (" please enter the weight of each leaf node ", HT [I] weight);
}
CreatHuffmanTree (HT, n, m);
PrintHuffmanTree (HT, n);
system("pause");
return 0;
}
Changed your fault, the function to help you see
Honestly code is so messed up, m, n, I don't know which is which these parameters, all sorts of uninitialized are used, the function is not defined in the following code in use (not stated in the main function, other functions can be directly used, unless it is ShenMingCheng global variable, or a class member function), and to output into the parameter declaration & amp; nullnullnullnullnullnullnullnullnullnullnullnullnull