教你如何使用Python实现二叉树结构及三种遍历
作者:小白地瓜 发布时间:2021-04-30 14:08:00
标签:Python,二叉树结构,遍历
一:代码实现
class TreeNode:
"""节点类"""
def __init__(self, mid, left=None, right=None):
self.mid = mid
self.left = left
self.right = right
# 树类
class Tree:
"""树类"""
def __init__(self, root=None):
self.root = root
def add(self, item):
# 将要添加的数据封装成一个node结点
node = TreeNode(item)
if not self.root:
self.root = node
return
queue = [self.root]
while queue:
cur = queue.pop(0)
if not cur.left:
cur.left = node
return
else:
queue.append(cur.left)
if not cur.right:
cur.right = node
return
else:
queue.append(cur.right)
tree = Tree()
tree.add(0)
tree.add(1)
tree.add(2)
tree.add(3)
tree.add(4)
tree.add(5)
tree.add(6)
二:遍历
在上述树类代码基础上加遍历函数,基于递归实现。
先序遍历:
先序遍历结果是:0 -> 1 -> 3 -> 4 -> 2 -> 5 -> 6
# 先序遍历
def preorder(self, root, result=[]):
if not root:
return
result.append(root.mid)
self.preorder(root.left, result)
self.preorder(root.right, result)
return result
print("先序遍历")
print(tree.preorder(tree.root))
"""
先序遍历
[0, 1, 3, 4, 2, 5, 6]
"""
中序遍历:
中序遍历结果是:3 -> 1 -> 4 -> 0 -> 5 -> 2 -> 6
# 中序遍历
def inorder(self, root, result=[]):
if not root:
return result
self.inorder(root.left, result)
result.append(root.mid)
self.inorder(root.right, result)
return result
print("中序遍历")
print(tree.inorder(tree.root))
"""
中序遍历
3, 1, 4, 0, 5, 2, 6]
"""
后续遍历
后序遍历结果是:3 -> 4 -> 1 -> 5 -> 6 -> 2 -> 0
# 后序遍历
def postorder(self, root, result=[]):
if not root:
return result
self.postorder(root.left, result)
self.postorder(root.right, result)
result.append(root.mid)
return result
print("后序遍历")
print(tree.postorder(tree.root))
"""
后序遍历
[3, 4, 1, 5, 6, 2, 0]
"""
来源:https://blog.csdn.net/qq_39434183/article/details/117927680


猜你喜欢
- 例如:preds = to_numpy(preds)#preds是[2985x16x2]preds = preds.transpose(2,
- 临时表与内存表内存表,指的是使用Memory引擎的表,建表语法是create table … engine=memory。这种 表的数据都保
- 针对之前安装mysql的笔记进行了总结,分享给大家。1.下载下载地址:http://dev.mysql.com/downloads/mysq
- 本文实例为大家分享了python和pip安装教程,供大家参考,具体内容如下1.安装python第一步,windows下面的Python安装一
- Hello, 大家好,又是我~ 大家有看过font set和一些要注意的基本问题以及通用字体族两篇文章后,应该对字体的基本有了一些了解。现
- 1。下载mysql-noinstall-5.1.33-win32.zip,然后解压 2。复制my-huge配置文件为my.ini 在 [my
- 引言通过一张照片居然发现女友在宿舍里没去上课!强大的照片位置信息获取,快来一起学习吧!一、exifread函数库要怎样获得拍摄图片的GPS呢
- 下面,我们就从当前时间来取得随机数,调用的时候用包含文件就可以了:<!--#INCLUDE VIRTUAL="/q
- #!/usr/bin/env python# -*- coding:utf-8 -*-# *************************
- 将数据库中的信息存储至XML文件中:save.asp<!-- #include file="adovbs
- 本文实例讲述了Python实现读写INI配置文件的方法。分享给大家供大家参考,具体如下:# -*- coding: utf-8 -*-imp
- 今天出于需要,要将爬虫爬取的一些数据整理成二维数组,再编码成json字符串传入数据库那么问题就来了,在php中这个过程很简便 ,类似这样:
- 注:Unicode相关知识的详细介绍请参考UTF-8, UTF-16, UTF-32 & BOM。 对于UTF-8/16/32而言,
- 问题复现:连接钱包后,会调用函数,弹出窗口让用户签名if (signatureMessage) {
- Go流程控制1、条件语句IF1、简单格式(不支持三目运算符)if 布尔表达式 { // 执行语句}2、if里面包含多个表达式的时
- 本文实例讲述了Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)。分享给大家供大家参考,具体
- 这篇文章主要介绍了python线程join方法原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 方法一:简单,得不到参数,只有一个虚拟路径 代码如下:GetUrl =request("url") 例如:http://
- python自带了日志模块logging,可以用来记录程序运行过程中的日志信息。同时python还有logbook模块用来取代logging
- 本文实例讲述了Python挑选文件夹里宽大于300图片的方法。分享给大家供大家参考。具体分析如下:这段代码需要用到PIL库。代码如下所示:i