代码如下:

# coding=utf-8

class myNode(object):
def __init__(self, data=-1, lchild=None, rchild=None):
self.data = data
self.lchild = lchild
self.rchild = rchild class BTTree(object):
def __init__(self):
self.root = None # 建立二叉树是以层序遍历方式输入,节点不存在时以 'None' 表示
def creatTree(self, nodeList):
if nodeList[0] == None:
return None
head = myNode(nodeList[0])
Nodes = [head]
j = 1
for node in Nodes:
if node != None:
node.lchild = (myNode(nodeList[j]) if nodeList[j] != None else None)
Nodes.append(node.lchild)
j += 1
if j == len(nodeList):
return head
node.rchild = (myNode(nodeList[j])if nodeList[j] != None else None)
j += 1
Nodes.append(node.rchild)
if j == len(nodeList):
return head def digui_qianxu(self, root):
if root is None:
return
print root.data
self.digui_qianxu(root.lchild)
self.digui_qianxu(root.rchild) def digui_zhongxu(self, root):
if root is None:
return
self.digui_zhongxu(root.lchild)
print root.data
self.digui_zhongxu(root.rchild) def digui_houxu(self, root):
if root is None:
return
self.digui_houxu(root.lchild)
self.digui_houxu(root.rchild)
print root.data def feidigui_qianxu(self, root):
#通过堆栈来存储根节点,遍历根节点从root开始一直往下走到最左边的根节点将之加入到栈中,在加入栈中之前就进行访问
#将栈中的内容不断从最后弹出,然后查找其是否有右孩子,没有则继续弹出栈中的元素,有的话则访问
myStack = []
node = root
while node or myStack:
while node!= None:
print node.data
myStack.append(node)
node = node.lchild
elem = myStack.pop()
if elem.rchild != None:
node = elem.rchild def feidigui_zhongxu(self, root):
myStack = []
node = root
while node or myStack:
while node != None:
myStack.append(node)
node = node.lchild
elem = myStack.pop()
print elem.data
if elem.rchild != None:
node = elem.rchild def feidigui_houxu(self, root):
#因为是左右然后根,需要保留根才能得到左和右,首先从根pop之后找到左加入到栈,找到右加入到栈
#之后将根加入到另一个栈中,这样另一个栈中得到的就是:根右左的顺序
#等将另一个栈进行持续pop,得到的就是:左右根这样的顺序
myStack1 = []
myStack2 = []
myStack1.append(root)
while myStack1:
node = myStack1.pop()
if node.lchild != None:
myStack1.append(node.lchild)
if node.rchild != None:
myStack1.append(node.rchild)
myStack2.append(node)
while myStack2:
print myStack2.pop().data if __name__ == "__main__":
test1 = BTTree()
nodeList = [1,2,3,4,5,6]
print '建树:'
test1.root = test1.creatTree(nodeList)
print '递归前序:'
test1.digui_qianxu(test1.root)
print '----------------------'
print '递归中序:'
test1.digui_zhongxu(test1.root)
print '----------------------'
print '递归后序:'
test1.digui_houxu(test1.root)
print '----------------------'
print '非递归前序:'
test1.feidigui_qianxu(test1.root)
print '----------------------'
print '非递归中序:'
test1.feidigui_zhongxu(test1.root)
print '----------------------'
print '非递归后序:'
test1.feidigui_houxu(test1.root)
print '----------------------'

结果如下:

建树:
递归前序:
1
2
4
5
3
6
----------------------
递归中序:
4
2
5
1
6
3
----------------------
递归后序:
4
5
2
6
3
1
----------------------
非递归前序:
1
2
4
5
3
6
----------------------
非递归中序:
4
2
5
1
6
3
----------------------
非递归后序:
4
5
2
6
3
1
----------------------

【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历的更多相关文章

  1. 【转载】Python中如何高效实现两个字典合并,三种方法比较。

    本文转载自:http://www.pythoner.com/13.html Python中将两个字典进行合并操作,是一个比较常见的问题.本文将介绍几种实现两个字典合并的方案,并对其进行比较. 对于这个 ...

  2. Python中字符串拼接的三种方式

    在Python中,我们经常会遇到字符串的拼接问题,在这里我总结了三种字符串的拼接方式:     1.使用加号(+)号进行拼接 加号(+)号拼接是我第一次学习Python常用的方法,我们只需要把我们要加 ...

  3. 记住 Python 变量类型的三种方式

    title: 记住变量类型的三种方式 date: 2017-06-11 15:25:03 tags: ['Python'] category: ['Python'] toc: true comment ...

  4. python实现单例模式的三种方式及相关知识解释

    python实现单例模式的三种方式及相关知识解释 模块模式 装饰器模式 父类重写new继承 单例模式作为最常用的设计模式,在面试中很可能遇到要求手写.从最近的学习python的经验而言,singlet ...

  5. python 全栈开发,Day94(Promise,箭头函数,Django REST framework,生成json数据三种方式,serializers,Postman使用,外部python脚本调用django)

    昨日内容回顾 1. 内容回顾 1. VueX VueX分三部分 1. state 2. mutations 3. actions 存放数据 修改数据的唯一方式 异步操作 修改state中数据的步骤: ...

  6. 命令行运行Python脚本时传入参数的三种方式

    原文链接:命令行运行Python脚本时传入参数的三种方式(原文的几处错误在此已纠正) 如果在运行python脚本时需要传入一些参数,例如gpus与batch_size,可以使用如下三种方式. pyth ...

  7. Python实现定时执行任务的三种方式简单示例

    本文实例讲述了Python实现定时执行任务的三种方式.分享给大家供大家参考,具体如下: 1.定时任务代码 import time,os,sched schedule = sched.scheduler ...

  8. Python格式化输出的三种方式

    Python格式化输出的三种方式 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age ...

  9. Python|读、写Excel文件(三种模块三种方式)

    python读写excel的方式有很多,不同的模块在读写的讲法上稍有区别: 用xlrd和xlwt进行excel读写: 用openpyxl进行excel读写: 用pandas进行excel读写: imp ...

  10. Python实现微信支付(三种方式)

    Python实现微信支付(三种方式) 关注公众号"轻松学编程"了解更多. 如果需要python SDk源码,可以加我微信[1257309054] 在文末有二维码. 一.准备环境 1 ...

随机推荐

  1. 全志A33 linux led驱动编程(附实测参考代码)

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 开发平台 * 芯灵思Sinl ...

  2. iso系统镜像刻录到光盘和U盘

    使用UltraISO刻录 刻录U盘,点击文件,打开,选择镜像 启动,写入硬盘镜像选择U盘即可 刻录光盘 工具,刻录光盘映像,选择镜像,需要先插入光盘刻录机(有些电脑可能自带光驱盘,且有刻录功能,那么我 ...

  3. 登录ssh提示:ssh_exchange_identification: read: Connection reset by peer error

    vim /etc/hosts.allow 添加 sshd : ALL

  4. dubbo 基础入门

    一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...

  5. 支付宝 生活号 获取 userId 和 生活号支付

    第一:申请生活号. 第二:激活开发者 模式 ,并且上创 自己的 公钥  ( 支付宝 demo 里面有 ) 第三: 配置 回调地址 ( 用于前端 调用获取 auth_code 的时候 填写的回调地址,支 ...

  6. centos 7.5安装docker-CE 18

    1.查看系统版本 cat /etc/centos-release CentOS Linux release 7.5.1804 (Core) uname -r 3.10.0-862.el7.x86_64 ...

  7. Mysql数据库主从复制搭建

    Mysql数据库主从复制原理: 主库开启bin-log日志,同时生成IO线程.IO线程负责将用户写入数据库的sql语句记录在二进制日志bin-log,该记录过程可并发进行:生成标识号 server i ...

  8. scoping作用域,anonymous function匿名函数,built-in functions内置函数

    作用域练习1 def test1(): print('in the test1') def test(): print('in the test') return test1 res = test() ...

  9. CSS之checkbox&radio&textarea&select

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Python · 进度条

    (这里是本章会用到的 GitHub 地址) 我实现的这个进度条可能是可以当做一个第三方库来使用的(这个人好自大,啧),它支持记录并发程序的进度且损耗基本只来源于 Python 本身 先来看看我们的进度 ...