python 二叉树
class Node(object):
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
class BTree(object):
def __init__(self, root=None):
self.root = root
def is_empty(self):
if self.root is None:
return True
else:
return False
# 先序遍历(递归,recursion)
def pre_order(self, node):
if node is None:
return
print(node.data)
self.pre_order(node.left)
self.pre_order(node.right)
# 中序遍历(递归)
def in_order(self, node):
if node is None:
return
self.in_order(node.left)
print(node.data)
self.in_order(node.right)
# 后序遍历(递归)
def post_order(self, node):
if node is None:
return
self.post_order(node.left)
self.post_order(node.right)
print(node.data)
# 先序遍历(非递归)
def preorder(self, node):
stack = []
while node or stack:
if node is not None:
print(node.data)
stack.append(node)
node = node.left
else:
node = stack.pop()
node = node.right
# 中序遍历(非递归)
def inorder(self, node):
stack = []
while node or stack:
if node:
stack.append(node)
node = node.left
else:
node = stack.pop()
print(node.data)
node = node.right
# 后序遍历(非递归)
def postorder(self, node):
stack = []
queue = []
queue.append(node)
while queue:
node = queue.pop()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
stack.append(node)
while stack:
print(stack.pop().data)
# 水平遍历
def levelorder(self, node):
if node is None:
return
queue = [node]
while queue:
node = queue.pop(0)
print(node.data)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
# 根据前序遍历和中序遍历,求后序遍历
def findTree(self, preList, midList, afterList):
'''
preList = list('DBACEGF')
midList = list('ABCDEFG')
afterList = ['A', 'C', 'B', 'F', 'G', 'E', 'D']
'''
if len(preList)==0:
return
if len(preList)==1:
afterList.append(preList[0])
return
root = preList[0]
n = midList.index(root)
self.findTree(preList[1:n+1], midList[:n], afterList)
self.findTree(preList[n+1:], midList[n+1:], afterList)
afterList.append(root)
#return afterList
'''
n1 = Node(data=1)
n2 = Node(2,n1,None)
n3 = Node(3)
n4 = Node(4)
n5 = Node(5,n3,n4)
n6 = Node(6,n2,n5)
n7 = Node(7,n6,None)
n8 = Node(8)
root = Node(0,n7,n8)
'''
root = Node(0, Node(7, Node(6, Node(2, Node(1)), Node(5, Node(3), Node(4)))), Node(8))
bt = BTree(root)
print('pre_order......')
print(bt.pre_order(bt.root))
print('in_order......')
print(bt.in_order(bt.root))
print('post_order.....')
print(bt.post_order(bt.root))
preList = list('DBACEGF')
midList = list('ABCDEFG')
afterList = []
bt.findTree(preList, midList, afterList)
print(afterList)
python 二叉树的更多相关文章
- Python --- 二叉树的层序建立与三种遍历
二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有....(此处省略好多字)....等的优良特点. 之前在刷LeetCode的时候把有关树的题目全部跳过了,(ORZ:我这种连数据结 ...
- Python - 二叉树, 堆, headq 模块
二叉树 概念 二叉树是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树), 或者由一个根结点和两棵互不相交的.分别称为根结点的左子树和右子树组成. 特点 每个结点最多有两颗子树,所 ...
- python 二叉树实现带括号的四则运算(自学的孩子好可怜,不对的地方请轻责)
#!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...
- python二叉树递归算法之后序遍历,前序遍历,中序遍历
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2016-11-18 08:53:45 # @Author : why_not_try ...
- python 二叉树实现
二叉树实现思想 1.把每个节点都看作是一个对象包含以下特征: 节点的当前值 节点的左孩子(存储比当前节点值小的节点对象) 节点右孩子(存储比当前节点值大的节点对象) 2.二叉树就是以根节点开始的连续的 ...
- python 二叉树实现带括号的四则运算
#!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...
- python二叉树简单实现
二叉树简单实现: class Node: def __init__(self,item): self.item = item self.child1 = None self.child2 = None ...
- python二叉树染色-有严重BUG
#coding:utf-8 ''' 二叉树涂黑 输入: 5 2 1 -1 4 2 -1 5 4 -1 3 1 1 2 输出: 3 第二题是:斗地主 ''' import sys b=list() cl ...
- python 二叉树计算器
例子:计算1+2+3+4的值 代码: class Buffer(object): """字符串处理函数""" def __init__(se ...
- python二叉树的遍历,递归和非递归及相关其它
# encoding=utf-8class node(object): def __init__(self,data,left=None,right=None): self.data = data s ...
随机推荐
- asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现
asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现 用这个可以做模板首页进行输出,也可以自已自定义进行扩展 //得到读取到的文本到string中 string resultTe ...
- Visual Studio 开发平台的安装与单元测试
一.安装VS2013 1.运行安装文件夹中的.exe文件,选择好安装路径与所需功能后开始安装 2.安装后第一次打开,需要一段时间 3.安装成功后,要打开VS2013,在工具栏中找到帮助选项卡,点击注册 ...
- INBOUND_CONNECT_TIMEOUT与SQLNET.INBOUND_CONNECT_TIMEOUT小结
关于sqlnet.ora的参数SQLNET.INBOUND_CONNECT_TIMEOUT,它表示等待用户认证超时的时间,单位是秒,缺省值是60秒,如果用户认证超时了,服务器日志alert.log显示 ...
- 【Python】-【类解析】--【脚本实例】
通过脚本事例,解析下Python中类的几个概念在脚本中的应用 脚本如下: ++++++++++++++++++++++++++++++++++++++++ #!/usr/bin/env python# ...
- Javascript刷新页面的几种方法
Javascript刷新页面的几种方法: window.navigate(location)location.reload()location=locationlocation.assign(loca ...
- SQLServer查询锁表
查看被锁表: select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys. ...
- MySQL用户无法登陆问题
安装完MySQL后,我们通常添加拥有相应权限的普通用户用来访问数据库.在使用普通用户(假设为tom)本地登录数据库的时候,经常会出现无法登录的情况,但是从其他的mysql客户端却可以登录.在本地使用t ...
- [SqlServer]创建链接服务器
把一个数据库中数据表中的内容,从一个SQL SERVER服务器 导出到另一个SQL Server服务器 不同服务器数据库之间的数据操作 --创建链接服务器 exec sp_addlinkedserv ...
- 150922-写写博客监督下不自觉的自己-PPT,Linux,HTML
开始学PHP的日子里,总是懒散的有一天没一天的.无意间听闻写博客来展示代码(现在还远远做不到哇),来监督个人每天的学习进度,鉴于自己还是爱写一点文字,但愿可以坚持下去. 凡是都喜欢有个计划,骨子里的理 ...
- iScroll4下表单元素聚焦及键盘的异常问题
本文是zawa同事写的一篇博文,相信很多在webapp开发中的同学使用iscroll4会遇到的该问题,问过zawa兄的建议,在这里分享给大家,希望能帮助到各位~ 原文地址:http://www.zaw ...