Huffman编码的问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 10:34:42
将字符串bababecbdcdedebeebba进行Huffman编码,将之编码成二进制串,并计算平均码长

先分析个字符的权值:
a=3,b=7,c=2,d=3,e=5
生成一棵霍夫曼树,得到各字符的编码:
a=110,b=0,c=1111,d=1110,e=10
平均码长为46/15

wd@Klutz-wd ~/c
$ gcc huffman.c

wd@Klutz-wd ~/c
$ ./a.exe
bababecbdcdedebeebba
* * * * * * ** * ** ** ** * ** * * * * * * *
* * * * * * * * * * * * * * *

*代表1,平均码长自己做一下除法。

huffman算法的VC++实现
最小堆头文件minheap.h:
template <class Type> class MinHeap{
public:
MinHeap(int maxSize);//常成员函数不能修改this指向的内容
MinHeap(Type arr[],int n);
~MinHeap(){delete[]heap;}
int Insert(const Type &x);
int RemoveMin(Type &x);//用参数作返回值
Type DeleteMin();
int IsEmpty()const{return CurrentSize==0;}
int IsFull()const{return CurrentSize==MaxHeapSize;}
void MakeEmpty(){CurrentSize=0;}
private:
enum{DefaultSize=10};
Type *heap;
int CurrentSize;
int MaxHeapSize;
void FilterDown(const in