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 ...
随机推荐
- 心理控制方法——阅读Notes
1.自助式情感手术 祛除自我意象中的伤疤的要点 2. 你制造错误,但是错误不应造就你 你身上的缺点不是你的错 3. 不仅要原谅别人,也要原谅自己 4. 怨恨是一条通向失败的道路 5. 注意来 ...
- Team Leader炖完石头汤后干嘛
在万众创业的互联网年代,挖人组建全明星团队过于奢侈.面对水平参差不齐的团队咋办? 命运真是捉弄,半年前在大美团打工时准备做个NABC的教学项目 ,结果自己就被挖到"Competitors 竞 ...
- 试用 Nexus OSS 3.0 的docker仓库 (二)
试用 Nexus OSS 3.0 的docker仓库 (一) : http://www.cnblogs.com/wzy5223/p/5410990.html 三. 创建docker私有仓库,docke ...
- Javascript之旅——第四站:parseInt中要注意的坑
前些天信用卡站点要接入一个新功能,不过还真比较坑爹,asp站点,大家都知道信用卡的背面是有一个有效期的,在对接银行中这个信息 一定是要传给银行做数据校验,用户在语音输入信用卡有效期后,系统会做一个有效 ...
- PHP5中的stdClass
PHP5中新增stdClass 官方手册参考:http://www.php.net/manual/en/language.oop5.basic.php#92123 stdClass类是PHP的一个内部 ...
- Hadoop+MongoDB的四种方案
背景: 公司核心业务库现存在MongoDB中,分布在6台MongoDB节点.现面临如下问题: 1.最大的一张表有10多个G,MongoDB在查询方面尚能胜任,但是涉及到复杂计算时会比较吃力. 2.Mo ...
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
在eclipse中用过maven的可能都遇到过这种情况,我以前一直在search.maven里面搜索,然后添加pom信息. 今天在网上搜索时,找到了一个解决方法,在这里分享一下. 第一步,在prefe ...
- C++/CLI——读书笔记《Visual C++/CLI从入门到精通》 第Ⅰ部分
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- 绕过校园网的共享限制 win10搭建VPN服务器实现--从入门到放弃
一.开篇立论= =.. 上次说到博主在电脑上搭建了代理服务器来绕过天翼客户端的共享限制,然而经过实际测试还不够完美,所以本着生命不息,折腾不止的精神,我又开始研究搭建vpn服务器= =... (上次的 ...
- android 关于appcompat v7出错问题与解决
1.appcompat_v7:应用兼容包,V7说的是版本7,即android2.1,这个兼容包支持2.1版本以上系统2.最近谷歌官方将兼容jar包与某些资源文件单独拿出来建立了一个android工程, ...