B树排序算法之Python版
2015-12-10 17:50
323 查看
#!/usr/bin/python # -*- coding: UTF-8 -*- # added by kangye, dependent on python27 class BTree: def __init__(self,value): self.left=None self.data=value self.right=None def insertLeft(self,value): self.left=BTree(value) return self.left def insertRight(self,value): self.right=BTree(value) return self.right def show(self): print self.data def inorder(node): if node.data: if node.left: inorder(node.left) node.show() if node.right: inorder(node.right) def rinorder(node): if node.data: if node.right: rinorder(node.right) node.show() if node.left: rinorder(node.left) def insert(node,value): if value > node.data: if node.right: insert(node.right,value) else: node.insertRight(value) else: if node.left: insert(node.left,value) else: node.insertLeft(value) if __name__ == "__main__": l=[88,11,2,33,22,4,55,33,221,34] Root=BTree(l[0]) node=Root for i in range(1,len(l)): insert(Root,l[i]) print "1---->10" inorder(Root) print "10--->1" rinorder(Root)
相关文章推荐
- 老李分享:pep8 python代码规范 2
- 老李分享:pep8 python代码规范 1
- python argparse 模板
- 老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具3
- 老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具2
- 老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具1
- python+stomp+activemq
- 【详解】Python多线程的实现
- python基础教程共60课-第27课操作list2
- 10. python sqlite3应用
- 第一个python程序出错,SyntaxError: invalid syntax
- Python字典dictionary
- python相关概念
- python基础教程共60课-第26课操作list
- 【python】python实例集<一>
- python基础教程共60课-第25课初探list
- python使用tab补齐
- python实现哈夫曼编码
- Python批量插入SQL Server数据库
- Python -- 使用requests模块发送GET和POST请求