python实现二叉树
来源:互联网 发布:黑客刷枪软件 编辑:程序博客网 时间:2024/05/22 20:28
you can try
from collections import defaultdictdef hash(): return defaultdict(hash)users = hash()users['harold']['username'] = 'hrldcpr'users['handler']['username'] = 'matthandlersux'
另一种方式
class _Tree(object): def __init__(self, value, nodeClass): self.value = nodeClass(value) self.leftNode = None self.rightNode = None def setLeftNode(self, node): self.leftNode = node def setRightNode(self, node): self.rightNode = node def __str__(self): returnList = [] returnList.append(self) index = 0 while index<len(returnList) and returnList[index]: curNode = returnList[index] returnList.append(curNode.leftNode) returnList.append(curNode.rightNode) index += 1 return str(returnList) def __repr__(self): return '<N:%s>'%self.valuedef build(sequence, nodeClass=lambda *a: a): length = len(sequence) if length: middle_index = length / 2 value = sequence[middle_index] _tree = _Tree(value, nodeClass) _leftnode = build(sequence[:middle_index], nodeClass) _rightnode = build(sequence[middle_index+1:], nodeClass) _tree.setLeftNode(_leftnode) _tree.setRightNode(_rightnode) else: _tree = None return _treeif __name__ == '__main__': class d(object): def __init__(self, value): self.v = '$%s'%str(value) def __str__(self): return self.v __repr__ = __str__ sequence = range(5) b = build(sequence) print b b = build(sequence, d) print b
http://www.cnblogs.com/junzhi1989/archive/2012/07/11/2586820.html
another
0 0
- Python实现二叉树
- python实现二叉树
- python实现二叉树
- Python实现二叉树
- Python实现二叉树
- Python实现二叉树
- python实现二叉树
- Python实现二叉树
- 二叉查找树:Python实现
- python实现二叉查找树
- 二叉查找树python实现
- python实现二叉树遍历
- python实现二叉查找树
- 二叉树的实现 Python
- 二叉查找树python实现
- 二叉搜索树 python实现
- python实现二叉树排序
- python实现二叉树,以及二叉树的遍历
- 测试博文
- putty最佳的的颜色配置
- CodeForces 4B Before an Exam(数学+模拟)
- leetcode之Longest Common Prefix
- shell中类似for循环的还有一个就是select
- python实现二叉树
- 如何获取用户手机当前是否启动了android机的模拟位置功能
- Linux命令学习手册-rsync命令
- 试论计算机病毒及防护
- microsoft word has stopped working解决方法
- 如何安全修改cocoapods上的第三方代码
- vs2010快捷键大全
- Eclipse中10个最有用的快捷键组合
- MyEclipse设置注释格式