这段二叉排序树的创建代码,二叉排序树,代码出自《Program
这段二叉排序树的创建代码,二叉排序树,代码出自《Program
代码出自《Programming Python》4th Edition
class BinaryTree: def __init__(self): self.tree = EmptyNode() def __repr__(self): return repr(self.tree) def lookup(self, value): return self.tree.lookup(value) def insert(self, value): self.tree = self.tree.insert(value)class EmptyNode: def __repr__(self): return '*' def lookup(self, value): # fail at the bottom return False def insert(self, value): return BinaryNode(self, value, self) # add new node at bottomclass BinaryNode: def __init__(self, left, value, right): self.data, self.left, self.right = value, left, right def lookup(self, value): if self.data == value: return True elif self.data > value: return self.left.lookup(value) # look in left else: return self.right.lookup(value) # look in right def insert(self, value): if self.data > value: self.left = self.left.insert(value) # grow in left elif self.data < value: self.right = self.right.insert(value) # grow in right return self def __repr__(self): return ('( %s, %s, %s )' % (repr(self.left), repr(self.data), repr(self.right)))>>>bt = BinaryTree()>>>for i in [3, 1, 9, 2, 7]:>>> bt.insert(i)>>> bt( ( *, 1, ( *, 2, * ) ), 3, ( ( *, 7, * ), 9, * ) )#该片段来自于http://byrx.net
相关内容
- 求一个集合的所有真子集 Python 2.7.6编译,python2.7.6,ge
- django使用装饰器进行请求认证,django装饰,NOPERMISSION
- BING DESKTOP ON WINDOW$ USING PYTHON3 ONLY!,bingpython3,bing桌面,
- 更快速、准确、简单的中文摘要实现,更快速中文摘要
- Python操作mysql(增删改查),pythonmysql,#!/usr/bin/e
- python写的掷骰子小游戏,python掷骰子小游戏,规则来自于
- 查找包含多个字符的文件,包含多个字符,查找指定目录
- Mac下Goagent打开部分https链接无法访问,goagenthttps,调试后
- cutescan.py,,cutescan.py
- Python一句话生成N个随机数并挑出某个范围内的,pytho
评论关闭