python---二叉树遍历
重学。
# coding = utf-8
# 二叉树遍历
class Node:
"""节点类"""
def __init__(self, element=None, left_child=None, right_child=None):
self.element = element
self.left_child = left_child
self.right_child = right_child
class Tree:
"""树类"""
def __init__(self):
self.root = Node()
self.tree_queue = []
def add(self, element):
"""为树添加节点"""
node = Node(element)
if self.root.element is None:
self.root = node
self.tree_queue.append(self.root)
else:
tree_node = self.tree_queue[0]
if tree_node.left_child is None:
tree_node.left_child = node
self.tree_queue.append(tree_node.left_child)
else:
tree_node.right_child = node
self.tree_queue.append(tree_node.right_child)
self.tree_queue.pop(0)
def front_recursion(self, root):
"""利用递归实现树的先序遍历"""
if root is None:
return
print(root.element, end=' ')
self.front_recursion(root.left_child)
self.front_recursion(root.right_child)
def middle_recursion(self, root):
"""利用递归实现树的中序遍历"""
if root is None:
return
self.middle_recursion(root.left_child)
print(root.element, end=' ')
self.middle_recursion(root.right_child)
def later_recursion(self, root):
"""利用递归实现树的后序遍历"""
if root is None:
return
self.later_recursion(root.left_child)
self.later_recursion(root.right_child)
print(root.element, end=' ')
def front_stack(self, root):
"""利用堆栈实现树的先序遍历"""
if root is None:
return
tree_stack = []
node = root
while node or tree_stack:
while node:
print(node.element, end=' ')
tree_stack.append(node)
node = node.left_child
node = tree_stack.pop()
node = node.right_child
def middle_stack(self, root):
"""利用堆栈实现树的中序遍历"""
if root is None:
return
tree_stack = []
node = root
while node or tree_stack:
while node:
tree_stack.append(node)
node = node.left_child
node = tree_stack.pop()
print(node.element, end=' ')
node = node.right_child
def later_stack(self, root):
"""利用堆栈实现树的后序遍历"""
if root is None:
return
tree_stack_a = []
tree_stack_b = []
node = root
tree_stack_a.append(node)
while tree_stack_a:
node = tree_stack_a.pop()
if node.left_child:
tree_stack_a.append(node.left_child)
if node.right_child:
tree_stack_a.append(node.right_child)
tree_stack_b.append(node)
while tree_stack_b:
print(tree_stack_b.pop().element, end=' ')
def level_queue(self, root):
"""利用队列实现树的层次遍历"""
if root is None:
return
tree_queue = []
node = root
tree_queue.append(node)
while tree_queue:
node = tree_queue.pop(0)
print(node.element, end=' ')
if node.left_child is not None:
tree_queue.append(node.left_child)
if node.right_child is not None:
tree_queue.append(node.right_child)
if __name__ == '__main__':
elem_s = range(10)
# 新建一个二叉树对象
tree = Tree()
for elem in elem_s:
tree.add(elem)
print('广度优先遍历---队列实现层次遍历:')
tree.level_queue(tree.root)
print('\n深度优先遍历---递归实现先序遍历:')
tree.front_recursion(tree.root)
print('\n深度优先遍历---递归实现中序遍历:')
tree.middle_recursion(tree.root)
print('\n深度优先遍历---递归实现后序遍历:')
tree.later_recursion(tree.root)
print('\n深度优先遍历---堆栈实现先序遍历:')
tree.front_stack(tree.root)
print('\n深度优先遍历---堆栈实现中序遍历:')
tree.middle_stack(tree.root)
print('\n深度优先遍历---堆栈实现后序遍历:')
tree.later_stack(tree.root)
C:\Users\Sahara\.virtualenvs\test\Scripts\python.exe C:/Users/Sahara/PycharmProjects/test/python_search.py 广度优先遍历---队列实现层次遍历: 深度优先遍历---递归实现先序遍历: 深度优先遍历---递归实现中序遍历: 深度优先遍历---递归实现后序遍历: 深度优先遍历---堆栈实现先序遍历: 深度优先遍历---堆栈实现中序遍历: 深度优先遍历---堆栈实现后序遍历: Process finished with exit code
python---二叉树遍历的更多相关文章
- Python --- 二叉树的层序建立与三种遍历
二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有....(此处省略好多字)....等的优良特点. 之前在刷LeetCode的时候把有关树的题目全部跳过了,(ORZ:我这种连数据结 ...
- python实现二叉树遍历算法
说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...
- Python -二叉树 创建与遍历算法(很详细)
树表示由边连接的节点.它是一个非线性的数据结构.它具有以下特性. 一个节点被标记为根节点. 除根节点之外的每个节点都与一个父节点关联. 每个节点可以有一个arbiatry编号的chid节点. 我们使用 ...
- 二叉树遍历(非递归版)——python
二叉树的遍历分为广度优先遍历和深度优先遍历 广度优先遍历(breadth first traversal):又称层次遍历,从树的根节点(root)开始,从上到下从从左到右遍历整个树的节点. 深度优先遍 ...
- 算法随笔-二叉树遍历的N种姿势
最近在练习用Python刷算法,leetcode上刷了快300题.一开始怀疑自己根本不会写代码,现在觉得会写一点点了,痛苦又充实的刷题历程.对我这种半路出家的人而言,收获真的很大. 今天就从二叉树遍历 ...
- Python - 二叉树, 堆, headq 模块
二叉树 概念 二叉树是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树), 或者由一个根结点和两棵互不相交的.分别称为根结点的左子树和右子树组成. 特点 每个结点最多有两颗子树,所 ...
- python 实时遍历日志文件
首先尝试使用 python open 遍历一个大日志文件, 使用 readlines() 还是 readline() ? 总体上 readlines() 不慢于python 一次次调用 readlin ...
- C++ 二叉树遍历实现
原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- hdu 4605 线段树与二叉树遍历
思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...
随机推荐
- 这才是你想要的 Python 可视化神器
Plotly Express 是一个新的高级 Python 可视化库:它是 Plotly.py 的高级封装,它为复杂的图表提供了一个简单的语法. 受 Seaborn 和 ggplot2 的启发,它专门 ...
- C. Multiplicity 简单数论+dp(dp[i][j]=dp[i-1][j-1]+dp[i-1][j] 前面序列要满足才能构成后面序列)+sort
题意:给出n 个数 的序列 问 从n个数删去任意个数 删去的数后的序列b1 b2 b3 ......bk k|bk 思路: 这种题目都有一个特性 就是取到bk 的时候 需要前面有个bk-1的序列前 ...
- 【Spring】手写Spring MVC
Spring MVC原理 Spring的MVC框架主要由DispatcherServlet.处理器映射.处理器(控制器).视图解析器.视图组成. 完整的Spring MVC处理 流程如下: Sprin ...
- 用python 发 帝国cms 文章
在e\extent下面放一个jiekou.php #!/usr/bin/env python3 # -*- coding: utf-8 -*- import time import urlli ...
- echarts 修改y轴name的样式
option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sa ...
- redis简单命令总结
1.连接到redis服务器:redis-cli -h 127.0.0.1 -p 6379 -a 密码 select index 切换 redis 数据库 flushdb 删除当前数据库所有的 key ...
- module 'pip' has no attribute 'main'
摘录自:http://www.cnblogs.com/Fordestiny/p/8901100.html 问题分析: 问题解决: 找到安装目录下 helpers/packaging_tool.py文件 ...
- LINQ To SQL 语法及实例大全【转】
转http://blog.csdn.net/pan_junbiao/article/details/7015633 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查 ...
- EF CodeFirst系列(6)---配置1对1,1对多,多对多关系
这一节介绍EF CodeFirst模式中的1对0/1,1对多,多对多关系的配置,只有梳理清楚实体间的关系,才能进行愉快的开发,因此这节虽然很简单但是还是记录了一下. 1. 1对0/1关系配置 1. 通 ...
- Java WebService接口生成和调用 图文详解>【转】【待调整】
webservice简介: Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间 ...