struct RNode

typedef struct RNode {
unsigned long flags;
char *nd_file;
union {
struct RNode *node;
ID id;
VALUE value;
VALUE (*cfunc)(ANYARGS);
ID *tbl;
} u1;
union {
struct RNode *node;
ID id;
int argc;
VALUE value;
} u2;
union {
struct RNode *node;
ID id;
long state;
struct global_entry *entry;
long cnt;
VALUE value;
} u3;
} NODE;
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 100 typedef struct { char data; unsigned int weight; unsigned int parent,lchild, rchild; }HTNode; typedef struct { char cd[N]; int start;} HCode; // 创建Huffman树 void createHT(HTNode ht[], int n) { int i, k, lnode, rnode; double min1, min2; for (i = 0; i < 2 * n - 1; i++) ht[i].parent = ht[i].lchild = ht[i].rchild = -1; for (i = n; i <= 2 * n - 2; i++) { min1 = min2 = 32767; lnode = rnode = -1; for (k = 0; k <= i - 1; k++) { if (ht[k].parent == -1) { if (ht[k].weight < min1) { min2 = min1; rnode = lnode; min1 = ht[k].weight; lnode = k; } else if (ht[k].weight < min2) { min2 = ht[k].weight; rnode = k; } } } ht[i].weight = ht[lnode].weight + ht[rnode].weight; ht[i].lchild = lnode; ht[i].rchild = rnode; ht[lnode].parent = i; ht[rnode].parent = i; }} // 生成编码 void CreateHCode(HTNode ht[], HCode hcd[], int n) { int i, f, c; HCode hc; for (i = 0; i < n; i++) { hc.start = n; c = i; f = ht[i].parent; while (f != -1) { if (ht[f].lchild == c) hc.cd[hc.start--] = '0'; else hc.cd[hc.start--] = '1'; c = f; f = ht[f].parent; } hc.start++; hcd[i] = hc; }} int main() { char str[N] = "\0"; int x; printf("请输入字符串:"); gets(str); int n = strlen(str); HTNode ht[N]; for (int i = 0; i < n; i++) { ht[i].data = str[i]; printf("请输入第%d个结点权重:", i + 1); scanf("%d", &x); ht[i].weight = x; } HCode hcd[N]; createHT(ht, n); CreateHCode(ht, hcd, n); for (int j = 0; j < n; j++) { printf("%s", hcd[j].cd); } return 0; }改正错误
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值