返回信息流已知26个英文字母及空格字符的使用频率从高到低为:
“空格”etaoinsrhldcumfpgwybvkxjqz
下面是英文字母使用频率表:(%)
A 8.19 B 1.47 C 3.83 D 3.91 E 12.25 F 2.26 G 1.71
H 4.57 I 7.10 J 0.14 K 0.41 L 3.77 M 3.34 N 7.06
O 7.26 P 2.89 Q 0.09 R 6.85 S 6.36 T 9.41
U 2.58 V 1.09 W 1.59 X 0.21 Y 1.58 Z 0.08
求经过哈弗曼编码后各自的码字。
以前写过这个程序,现在找不到了,哪位达人给个结果吧。只要结果就可以了
这是一条镜像帖。来源:北邮人论坛 / cpp / #13687同步于 2008/10/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[求助]已知26个英文字母的使用频率,求哈弗曼编码表
luckyboy120
2008/10/10镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
数改改,自己跑吧。
# Copyright (c) 2008 the authors listed at the following URL, and/or
# the authors of referenced articles or incorporated external code:
# http://en.literateprograms.org/Huffman_coding_(Python)?action=history&offset=20070126061347
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Retrieved from: http://en.literateprograms.org/Huffman_coding_(Python)?oldid=8684
from bisect import insort
def makeHuffTree(symbolTupleList):
sortedList = list(symbolTupleList)
sortedList.sort()
while len(sortedList) > 1:
childL, childR = sortedList.pop(1), sortedList.pop(0)
parent = (childL[0] + childR[0], childL, childR)
insort(sortedList, parent)
return sortedList[0]
def printHuffTree(huffTree, prefix = ''):
if len(huffTree) == 2:
print huffTree[1], prefix
else:
printHuffTree(huffTree[1], prefix + '0')
printHuffTree(huffTree[2], prefix + '1')
exampleData = [
(0.124167 , 'e'),
(0.0969225 , 't'),
(0.0820011 , 'a'),
(0.0768052 , 'i'),
(0.0764055 , 'n'),
(0.0714095 , 'o'),
(0.0706768 , 's'),
(0.0668132 , 'r'),
(0.0448308 , 'l'),
(0.0363709 , 'd'),
(0.0350386 , 'h'),
(0.0344391 , 'c'),
(0.028777 , 'u'),
(0.0281775 , 'm'),
(0.0235145 , 'f'),
(0.0203171 , 'p'),
(0.0189182 , 'y'),
(0.0181188 , 'g'),
(0.0135225 , 'w'),
(0.0124567 , 'v'),
(0.0106581 , 'b'),
(0.00393019, 'k'),
(0.00219824, 'x'),
(0.0019984 , 'j'),
(0.0009325 , 'q'),
(0.000599 , 'z')
]
if __name__ == '__main__':
huffTree = makeHuffTree(exampleData)
printHuffTree(huffTree)