python Huffman树,pythonhuffman树,[Python]代码#!
python Huffman树,pythonhuffman树,[Python]代码#!
[Python]代码
#!/usr/bin/pythona = (45, 13, 12, 16, 9, 15)class Node(object): def __init__(self, weight = None, parent = None, left = None, right = None): self.weight = weight self.parent = parent self.left = left self.right = rightdef HuffmanTree(): HuffNode = [] #input weight for n in range(len(a)): HuffNode.append(Node(a[n], -1, -1, -1)) n = len(HuffNode) for i in range(n): m1 = m2 = 1000 x2 = x1 = 0 for j in range(n + i): if HuffNode[j].weight < m1 and HuffNode[j].parent == -1: m2 = m1 x2 = x1 m1 = HuffNode[j].weight x1 = j elif HuffNode[j].weight < m2 and HuffNode[j].parent == -1: m2 = HuffNode[j].weight x2 = j HuffNode[x1].parent = HuffNode[x2].parent = n + i HuffNode.append(Node(HuffNode[x1].weight + HuffNode[x2].weight, -1, x1, x2)) print 'x1.weight and x2.weight in round %d: %d, %d' % (i+1, HuffNode[x1].weight, HuffNode[x2].weight)if __name__ == "__main__": HuffmanTree()
相关内容
- python 3.3下结合tkinter做的比大小程序,pythontkinter,step1
- Householder similarity transformation of matrix in Python,,''' d,c
- 实现追磨铁上小说功能,追磨铁小说,最近一直在磨铁上
- Python的map、reduce和filter以及list comprehension(列表推导)
- 生成随机密码python 窗口程序,python窗口,随机码主窗口
- 计算阶乘,,[Python]代码pr
- 通过动态代理生成基于gfwlist的动态代理脚本,,[Python
- 删除注释并编译,删除注释编译,[Python]代码#!
- 由域名获取该域名的ip地址,获取域名ip地址,[Python]代码
- 百度指定行业seo市场竞争度查询工具,seo市场竞争,[P
评论关闭