• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python数据结构之二叉树的遍历实例

python数据结构之二叉树的遍历实例

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 二叉树,python实现二叉树,二叉树的建立 python,python 二叉树的深度,python 二叉树的遍历等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

遍历方案
    从二叉树的递归定义可知,一棵非空的二叉树由根结点及左、右子树这三个基本部分组成。因此,在任一给定结点上,可以按某种次序执行三个操作:
    1).访问结点本身(N)
    2).遍历该结点的左子树(L)
    3).遍历该结点的右子树(R)

有次序:
    NLR、LNR、LRN

遍历的命名

    根据访问结点操作发生位置命名:
NLR:前序遍历(PreorderTraversal亦称(先序遍历))  ——访问结点的操作发生在遍历其左右子树之前。
LNR:中序遍历(InorderTraversal)  ——访问结点的操作发生在遍历其左右子树之中(间)。
LRN:后序遍历(PostorderTraversal)    ——访问结点的操作发生在遍历其左右子树之后。

注:由于被访问的结点必是某子树的根,所以N(Node)、L(Left subtlee)和R(Right subtree)又可解释为根、根的左子树和根的右子树。NLR、LNR和LRN分别又称为先根遍历、中根遍历和后根遍历。

遍历算法

1).先序遍历的递归算法定义:
若二叉树非空,则依次执行如下操作:
a.访问根结点
b.遍历左子树
c.遍历右子树

2).中序遍历的递归算法定义:
若二叉树非空,则依次执行如下操作:
a.遍历左子树
b.访问根结点
c.遍历右子树

3).后序遍历得递归算法定义:
若二叉树非空,则依次执行如下操作:
a.遍历左子树
b.遍历右子树
c.访问根结点

一、二叉树的递归遍历:

class TreeNode(object):

    def __init__(self, left=0, right=0, data=0):
        self.left = left
        self.right = right
        self.data = data

     
class BTree(object):

    def __init__(self, root=0):
        self.root = root

    def is_empty(self):
        if self.root is 0:
            return True
        else:
            return False

    def preorder(self, treenode):
        '前序(pre-order,NLR)遍历'
        if treenode is 0:
            return
        print treenode.data
        self.preorder(treenode.left)
        self.preorder(treenode.right)

    def inorder(self, treenode):
        '中序(in-order,LNR'
        if treenode is 0:
            return
        self.inorder(treenode.left)
        print treenode.data
        self.inorder(treenode.right)

    def postorder(self, treenode):
        '后序(post-order,LRN)遍历'
        if treenode is 0:
            return
        self.postorder(treenode.left)
        self.postorder(treenode.right)
        print treenode.data

     
node1 = TreeNode(data=1)
node2 = TreeNode(node1, 0, 2)
node3 = TreeNode(data=3)
node4 = TreeNode(data=4)
node5 = TreeNode(node3, node4, 5)
node6 = TreeNode(node2, node5, 6)
node7 = TreeNode(node6, 0, 7)
node8 = TreeNode(data=8)
root = TreeNode(node7, node8, 'root')

bt = BTree(root)

print u'''

#生成的二叉树

# ------------------------
#          root
#       7        8
#     6
#   2   5
# 1    3 4
#
# -------------------------

'''
print '前序(pre-order,NLR)遍历 :\n'
bt.preorder(bt.root)

print '中序(in-order,LNR) 遍历 :\n'
bt.inorder(bt.root)

print '后序(post-order,LRN)遍历 :\n'
bt.postorder(bt.root)
</div>

二、.二叉树的非递归遍历

下面就用非递归的方式实现一遍。主要用到了 stack 和 queue维护一些数据节点:

     

您可能想查找下面的文章:

  • Python实现二叉树结构与进行二叉树遍历的方法详解
  • Python 类与元类的深度挖掘 II【经验】
  • Python 类与元类的深度挖掘 I【经验】
  • Python解析树及树的遍历
  • Python中的二叉树查找算法模块使用指南
  • python数据结构之二叉树的统计与转换实例
  • python数据结构之二叉树的遍历实例
  • python数据结构之二叉树的建立实例
  • python数据结构树和二叉树简介
  • python数据结构之二叉树的统计与转换实例

相关文章

  • 举例讲解Python设计模式编程中的访问者与观察者模式
  • python正则表达式match和search用法实例
  • 按日期打印Python的Tornado框架中的日志的方法
  • python实用代码片段收集贴
  • wxpython 学习笔记 第一天
  • Python通过解析网页实现看报程序的方法
  • Python实现八大排序算法
  • Python实现模拟登录及表单提交的方法
  • 使用python统计文件行数示例分享
  • 关于Python中Inf与Nan的判断问题详解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python+Selenium自动化实现分页(pagination)处理
    • Python中的exec、eval使用实例
    • Python中几个比较常见的名词解释
    • Python 绘图和可视化详细介绍
    • python 类详解及简单实例
    • Python内置函数的用法实例教程
    • Python3基础之输入和输出实例分析
    • python网络编程之TCP通信实例和socketserver框架使用例子
    • python查找指定具有相同内容文件的方法
    • python登录pop3邮件服务器接收邮件的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有