huffman树的建立
Huffman树:所有结点带权路径长度之和最小的树
将权重结点(left right设为null)输入堆的指针数组中,再排成最小堆,每次取出两个,建立新结点t,把两个最小结点挂在t上,再将t插入最小堆,每个结点合并一次,共需h->size次
typedef struct treenode {
int weight;
huffmantree left;
huffmantree right;
}*huffmantree;
typedef struct hfnode {
huffmantree *data;
int size;
int maxsize;
}*hfheap;
hfheap buildminheap(hfheap h);
huffmantree huffman(hfheap h) {
int i;
buildminheap(h);
for (i = 0; i < h->size; i++) {
huffmantree t =(huffmantree) malloc(sizeof(struct treenode));
t->left = deletemin(h);
t->right = deletemin(h);
t->weight = t->left->weight + t->right->weight;
insert(h, t);
}
t = deletemin(h);
return t;
}