如上,原题就是这样的。
然后,我这里按照青岛大学王卓的数据结构课的代码输入的,然后他说我没初始化变量l
把代码改一下这个,就不用初始化了
:加个取地址符
我的灵感来自这篇文章
c语言构造哈夫曼树-哈夫曼编码_快乐的孙悟空的博客-CSDN博客_c语言哈夫曼树
#include
#include
#include
typedef int ElemType;
#include
#define M 6
using namespace std;
typedef struct
{
int weight;
int left, right;
}HTNode,*HuffmanTree;
void CreatTree(HuffmanTree &root, int n)
{
int m;
m = 2 * n - 1;
root = new HTNode[m+ 1];
for (int i = 1; i <= m; i++)
{
root[i].left = 0;
root[i].right = 0;
}
for (int i = 1; i <= n; i++)
cin >> root[i].weight;
for (int i = 1; i <= n; i++)
printf("%d", root[i].weight);
}
int main()
{
HuffmanTree l;
//HTNode ht[M];
//CreatTree(ht[M], 5);
CreatTree(l, 5);
}
运行结果如图:
然后,这道题的真正答案是这篇文章:
哈夫曼(Huffman)编码_呓语程序缘-CSDN博客



