1、实验五 赫夫曼树编码基本实验:赫夫曼编码 【实验内容与要求】某通讯系统只使用 8 种字符 a、b、c 、d、e、f、g、h,在某篇电文中其使用频率分别为 0.05,0.29,0.07,0.08,0.14,0.23, 0.03,0.11,设想用何种编码方式对其进行编码,能使其在进行远程通信时电文长度最短且误码率最低。【实现提示】 利用 Huffman 编码树求得最佳的编码方案。根据哈夫曼算法,建立哈夫曼树时,可以将哈夫曼树定义为一个结构型的一维数组 HuffTree,保存哈夫曼树中各结点的信息,每个结点包括:权值、左孩子、右孩子、双亲。由于哈夫曼树中共有 2n-1 个结点,并且进行 n-1 次合
2、并操作,所以该数组的长度为 2n-1。在哈夫曼树中,设左分支为 0,右分支为 1,从根结点出发,遍历整棵哈夫曼树,求得各个叶子结点所表示字符的哈夫曼编码。扩展实验:实际信息传输中的 Huffman 编码与译码【实验内容与要求】针对一段文本(推荐为英语),就这段文本进行相应的哈夫曼编码和译码。基本要求: 完成文本的频率统计。 构造哈夫曼树。 编写编码程序和译码程序。补充实验:修理栅栏(Fence Repair)DescriptionFarmer John wants to repair a small length of the fence around the pasture. He meas
3、ures the fence and finds that he needs N (1 N 20,000) planks of wood, each having some integer length Li (1 Li 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf“, the extra length
4、 lost to sawdust when a sawcut is made; you should ignore it, too.FJ sadly realizes that he doesnt own a saw with which to cut the wood, so he mosies over to Farmer Dons Farm with this long board and politely asks if he may borrow a saw.Farmer Don, a closet capitalist, doesnt lend FJ a saw but inste
5、ad offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimu
6、m amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.InputLine 1: One integer N, the number of planks Lines 2N+1: Each line contain
7、s a single integer describing the length of a needed plankOutputLine 1: One integer: the minimum amount of money he must spend to make N-1 cutsSample Input3858Sample Output34HintHe wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. The original board measures 8+5+8=21. The first c
8、ut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).S
9、ourceUSACO 2006 November Gold【实验内容与要求】农夫 John 要修理牧场的一段栅栏,他测量了栅栏,发现需要 N (1 N 20,000)块木头,每块木头长度为整数 Li (1 Li 50,000)个单位。于是他购买了一条很长的,能锯成 N 块的木头(即该木头长度是 Li 的总和) 。农夫John 忽略“损耗” ,即忽略的时候产生锯末而造成的长度损耗。农夫 John 没有锯子,于是他去找农夫 Don,向他借锯子。Don 是一个守财奴,他不把锯子借给 John,而是要 John 为在木头上锯 N-1次支付每一次锯的费用。锯一段木头要支付的费用等于这段木头的长度,即锯长
10、度为 21 的木头就要支付 21 美分。 (例如,要将长度为 21 的木头锯成长度为8,5,8 的三段。第一次锯木头,将木头锯成 13 和 8,花费 21;第二次将长度为 13 的那块木头锯成 8 和 5,花费 13,这样总的花费为 21+13=34。 )但是,如果将长度为 21 的木头第一次锯成 16 和 5,第二次锯长度为 16 的木头,总的花费为 21+16=37(大于 34) 。Don 让 John 决定在木头上切的次序和位置。请你帮助 John 确定锯 N 块木头所要花费的最少的钱。【实现提示】 由于木块锯一次产生两块木块,因此锯木过程可以用一棵二叉树表示:根表示初始木板,初始木板的
11、总长度为根结点的权,n 段目标木板为 n 个叶结点,其中第 i 个叶结点的权为第 i 段目标木板的长度 wi。从初始木板中锯下的第 i段目标木板,锯木的次数为根至第 i 个叶结点的路径长度 pi。根据题意,锯下第 i 段目标木板的花费为 pi*wi(1 i n),计算总花费最小的锯木方案,实际上是计算带权路径长度和最小的哈夫曼树。【基本实验的参考程序】#include#include#include#includetypedef structint weight;int parent;int lchild;int rchild;HTNode,*HuffmanTree; /动态分配数组存储哈夫曼树typedef char *HuffmanCode; /动态分配数组存储哈夫曼编码表void Select(HuffmanTree t,int i,int char *cd;int m,i,s1,s2,start,c,f;if (n1) :“);scanf(“%d“,w=(int*)malloc(n*sizeof(int);printf(“请依次输入%d 个权值(整数):n“,n);for(i=0;i=n-1;i+)scanf(“%d“,w+i);HuffmanCoding(HT,HC,w,n);for(i=1;i=n;i+)puts(HCi);