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 二叉树的更多相关文章

  1. Python --- 二叉树的层序建立与三种遍历

    二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有....(此处省略好多字)....等的优良特点. 之前在刷LeetCode的时候把有关树的题目全部跳过了,(ORZ:我这种连数据结 ...

  2. Python - 二叉树, 堆, headq 模块

    二叉树 概念 二叉树是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树), 或者由一个根结点和两棵互不相交的.分别称为根结点的左子树和右子树组成. 特点 每个结点最多有两颗子树,所 ...

  3. python 二叉树实现带括号的四则运算(自学的孩子好可怜,不对的地方请轻责)

    #!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...

  4. python二叉树递归算法之后序遍历,前序遍历,中序遍历

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2016-11-18 08:53:45 # @Author : why_not_try ...

  5. python 二叉树实现

    二叉树实现思想 1.把每个节点都看作是一个对象包含以下特征: 节点的当前值 节点的左孩子(存储比当前节点值小的节点对象) 节点右孩子(存储比当前节点值大的节点对象) 2.二叉树就是以根节点开始的连续的 ...

  6. python 二叉树实现带括号的四则运算

    #!/usr/bin/python #* encoding=utf-8 s = "20-5*(0+1)*5^(6-2^2)" c = 0 top = [0,s[c],0] op = ...

  7. python二叉树简单实现

    二叉树简单实现: class Node: def __init__(self,item): self.item = item self.child1 = None self.child2 = None ...

  8. 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 ...

  9. python 二叉树计算器

    例子:计算1+2+3+4的值 代码: class Buffer(object): """字符串处理函数""" def __init__(se ...

  10. python二叉树的遍历,递归和非递归及相关其它

    # encoding=utf-8class node(object): def __init__(self,data,left=None,right=None): self.data = data s ...

随机推荐

  1. #VSTS日志# 2015/12/10 – 终于可以删除工作项了

    最近的更新不少,废话少说,直接上干货 定制工作项字段 本周的更新后,所有的用户都可以在vsts上直接给工作项添加字段了,具体内容包括– 添加新字段(日期,字符串,整形,数字)– 字段显示位置配置– 过 ...

  2. Hello BIEE

      这篇文章提供了一个Hello World式的例子,讲述如何创建一个最简单的BIEE资料库.本文使用的示例数据可以在从此链接下载:http://www.zw1840.com . 目录 创建资料库 创 ...

  3. w3wp.exe(IIS ) CPU 占用 100% 的常见原因及解决办法

    对于IIS 管理员来说,经常会碰到 Web 服务器 CPU 占用 100% 的情况,以下是个人的日常工作总结和一些解决办法,主要用来剖析 w3wp.exe(IIS )  占用 CPU 100% 的一些 ...

  4. ASP.NET features need application service database support

    搭建的web程序出现如上图所示的错误 原因程序使用以下ASP.NET 特性 Membership (the SqlMembershipProvider class). Role management ...

  5. Druid 介绍及配置

    1. Druid是什么? Druid是Java语言中最好的数据库连接池.Druid能够提供强大的监控和扩展功能. 2. 在哪里下载druid 正式版本下载:maven中央仓库: http://cent ...

  6. Opensource开源精神

    现在如火如荼的开源运动和互联网自由开放的精神是一致的,互联网上有无数非常优秀的像Linux一样的开源代码,我们千万不要高估自己写的代码真的有非常大的“商业价值”.那些大公司的代码不愿意开放的更重要的原 ...

  7. 获取下拉框的value和值

    jsp: <td class="formItem_content"> <select name="label" id = "labe ...

  8. android java数组应用与说明

    如果定义类或结构数据则需每个都进行创建才可以使用,不然都为null 如: PointF pts[] = new PointF[5] ;//pts[0].x =CSSliderSize / 2;//这样 ...

  9. 8、FTP,二种文本传输模式

    一.基本知识 1. FTP是 TCP/IP协议族 的协议之一,简称文件传输协议,主要用于远距离文件传输,如文件的上传和下载 2. 下面都是以VSFTP服务器为例 VSFTP服务器的用户有三种形式: 匿 ...

  10. spring注入静态成员变量提示invalid setter method

    果然还是不够细心啊,被坑一晚上.. 一个极其简单的小程序,但是需要通过xml文件配置注入一个值,唯一的特别是要注入的属性是类中的静态成员变量.. 如下,然后自动生成get和set方法..坑就从此开始了 ...