用c语言构造Huffm树,其中包含如下函数。 ●CreateHT(HTNode ht[],intn); 由含有n个叶子结点的ht构造完整的哈夫曼树。 ●CreateHCode( HTNode ht[], HCode hed[],int n): 由哈夫曼树ht构造哈夫曼编码 hed。 ●DispHCode( HTNode ht[], HCode hed[],int n): 输出哈夫曼树ht和哈夫曼编码 hed中n个叶子结点的哈夫曼编码。 其中,主函数如下:int main() { int n = 15, i; string str[] = { "The","of","a","to","and","in","that","he","is","at","on","for","His","are","be" }; int fnum[] = { 1192,677,541,518,462,450,242,195,190,181,174,157,138,124,123 }; HTNode ht[M]; HCode hcd[N]; for (i = 0; i < n; i++) { ht[i].data = str[i]; ht[i].weight = fnum[i]; } CreateHT(ht, n);//创建哈夫曼树 CreateHCode(ht, hcd, n);//求得哈夫曼编码 DispHCode(ht, hcd, n);//输出哈夫曼编码和平均查找长度 return 0; }
时间: 2024-01-25 09:03:28 浏览: 101
下面是一个用C语言实现哈夫曼树的示例代码,包含了CreateHT、CreateHCode和DispHCode三个函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 2*N-1 // 哈夫曼树中结点的个数
#define N 100 // 哈夫曼编码的最大长度
typedef struct {
char* data; // 结点的内容
int weight; // 结点的权值
int parent; // 父结点的下标
int lchild; // 左子结点的下标
int rchild; // 右子结点的下标
} HTNode;
typedef char* HCode; // 定义哈夫曼编码类型为char*类型
// 选择权值最小的两个结点s1和s2
void Select(HTNode ht[], int n, int* s1, int* s2)
{
int i, min1, min2;
min1 = min2 = -1;
for (i = 0; i < n; i++) {
if (ht[i].parent == -1) {
if (min1 == -1 || ht[i].weight < ht[min1].weight) {
min2 = min1;
min1 = i;
}
else if (min2 == -1 || ht[i].weight < ht[min2].weight) {
min2 = i;
}
}
}
*s1 = min1;
*s2 = min2;
}
// 创建哈夫曼树
void CreateHT(HTNode ht[], int n)
{
int i, s1, s2;
for (i = 0; i < 2 * n - 1; i++) {
ht[i].parent = -1;
ht[i].lchild = -1;
ht[i].rchild = -1;
}
for (i = n; i < 2 * n - 1; i++) {
Select(ht, i, &s1, &s2);
ht[s1].parent = i;
ht[s2].parent = i;
ht[i].lchild = s1;
ht[i].rchild = s2;
ht[i].weight = ht[s1].weight + ht[s2].weight;
}
}
// 求哈夫曼编码
void CreateHCode(HTNode ht[], HCode hcd[], int n)
{
int i, j, p;
char* cd = (char*)malloc(N * sizeof(char));
cd[N - 1] = '\0';
for (i = 0; i < n; i++) {
p = i;
j = N - 1;
while (ht[p].parent != -1) {
if (ht[ht[p].parent].lchild == p) {
cd[--j] = '0';
}
else {
cd[--j] = '1';
}
p = ht[p].parent;
}
hcd[i] = (char*)malloc((N - j) * sizeof(char));
strcpy(hcd[i], &cd[j]);
}
free(cd);
}
// 输出哈夫曼编码和平均查找长度
void DispHCode(HTNode ht[], HCode hcd[], int n)
{
int i, j, len, sum = 0;
printf("哈夫曼编码如下:\n");
for (i = 0; i < n; i++) {
printf("%s: %s\n", ht[i].data, hcd[i]);
len = strlen(hcd[i]);
sum += ht[i].weight * len;
}
printf("平均查找长度为:%.2f\n", (float)sum / 10000);
}
int main()
{
int n = 15, i;
char* str[] = { "The","of","a","to","and","in","that","he","is","at","on","for","His","are","be" };
int fnum[] = { 1192,677,541,518,462,450,242,195,190,181,174,157,138,124,123 };
HTNode ht[M];
HCode hcd[N];
for (i = 0; i < n; i++) {
ht[i].data = str[i];
ht[i].weight = fnum[i];
}
CreateHT(ht, n); // 创建哈夫曼树
CreateHCode(ht, hcd, n); // 求得哈夫曼编码
DispHCode(ht, hcd, n); // 输出哈夫曼编码和平均查找长度
return 0;
}
```
在上面的代码中,我们首先定义了一个HTNode结构体,用于表示哈夫曼树中的结点。然后定义了HCode类型为char*类型,用于表示哈夫曼编码。在CreateHT函数中,我们使用了Select函数来选择权值最小的两个结点,然后将它们合并为一个新的结点,并更新哈夫曼树。在CreateHCode函数中,我们使用了一个cd数组来保存当前结点的编码,然后反向遍历哈夫曼树,找到每个叶子结点的编码,并将其保存在hcd数组中。在DispHCode函数中,我们输出每个叶子结点的编码,并计算平均查找长度。
注意,上面的代码中使用了动态内存分配来保存哈夫曼编码,需要在程序结束时释放掉分配的内存。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)