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 ...
随机推荐
- 哭瞎!360云盘将关停,你的几十T照片和文件该怎么办
IDO老徐刚得到了一个非常不开心的消息,360云盘将停止个人云盘服务...进行业务转型,在网盘存储.传播内容的合法性和安全性得到彻底解决之前不再考虑恢复,之后转型企业云服务. 而且之前共享的所有资料, ...
- 每日Scrum(9)
今天我们小组进行了软件的测试和界面的美化,特别是在主界面美化方面下了一些功夫,找了很多图片,把格式也处理的很完美,符合界面的一个框架,看起来,美观多了,至此,软件的beta版是基本完成了.
- 挖一挖C#中那些我们不常用的东西之系列(1)——ToDictionary,ToLookup
这个系列我们看看C#中有哪些我们知道,但是又不知道怎么用,又或者懒得去了解的东西,比如这篇我们要介绍的toDictionary 和ToLookup. 从图中我们看到有四个ToXXX的方法,其中ToAr ...
- JVM知识点
先发个链接到两位大牛的主页 http://rednaxelafx.iteye.com/ http://icyfenix.iteye.com/ 目录 1)概述 2)编译 3) ...
- FP并行算法的几个相关方向
1 集群系统中的 FP-tree 并行算法(many for one一个任务 还是 云计算one for many多个任务?) 计算机集群系统利用网络把一组具有高性能的工作站或者 PC 机按一定的结构 ...
- JavaScript目录菜单滚动反显组件的实现
JavaScript目录菜单滚动反显组件,有以下两个特点 每个导航菜单项(nav)对应页面一个内容区域(content) 滚动页面到特定内容区域(content)时,对应的菜单会自动切换,一般会添加一 ...
- [转]在NopCommerce中新增一个Domain Model的步骤
本文转自:http://www.cnblogs.com/aneasystone/archive/2012/08/27/2659183.html 在NopCommerce中新增一个Domain Mode ...
- 阿里云 CentOS6.5 ssh连接慢的解决方案
我租了一台阿里云深圳的服务器,用的是CentOS6.5的系统,最近要在服务器上小改点代码,但是不管用private shell 还是securecrt工具连接,连上去后,都特别慢,经常敲一段代码要过个 ...
- JSP中九大内置对象及其数据类型
JSP中九大内置对象为: request 请求对象 类型 javax.servlet.ServletRequest 作用域 Req ...
- Visio连接数据表实体外键[快捷记录]
打开数据库模型图. 单击“常用”工具栏上的“连接线”工具. 将“连接线”工具放在父表的中心上,使表的四周出现轮廓线,然后拖到子表的中心.当子表出现轮廓线时,松开鼠标按钮. 两个连接点均变为红色,同时父 ...