树和二叉树.docx





《树和二叉树.docx》由会员分享,可在线阅读,更多相关《树和二叉树.docx(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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 measures the fence an
3、d 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 lost to sawdust wh
4、en 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 instead offers to charge
5、 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 minimum amount of money h
6、e 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 2.N+1: Each line contains a single integer
7、 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 cut will cost 21, a
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二叉

限制150内