数据结构-5(二叉树)

一、思维导图(相关概念)

二、遍历

python 复制代码
    def prior_show(self, node):
        """
        先序遍历、根左右
        """
        if node is not None:
            print(node.data, end=" ")
            self.prior_show(node.lchild)
            self.prior_show(node.rchild)

    def mid_show(self, node):
        """
        中序遍历、左根右

        """
        if node is not None:
            self.mid_show(node.lchild)
            print(node.data, end=" ")
            self.mid_show(node.rchild)

    def post_show(self, node):
        """
        后序遍历、左 右 根

        """
        if node is not None:
            self.post_show(node.lchild)
            self.post_show(node.rchild)
            print(node.data, end=" ")

三、二叉树的初始化以及树的创建

python 复制代码
class Node():
    def __init__(self, data, lchild=None, rchild=None):
        self.data = data
        self.lchild = lchild
        self.rchild = rchild


class Tree():
    def __init__(self, root=None):
        self.root = root  # 根节点
        self.index = 0  # 用于遍历一串数据的索引

    def tree_creat(self, data_str):
        if self.index >= len(data_str):  # 最大条件,数据读取完之后直接返回
            return None

        # 开始创建,开始之前先读取对应的数据并且后移index
        data = data_str[self.index]
        self.index += 1
        # 开始递归,先写一个可以终止递归的目标条件
        if data == "#":  # 终止递归的条件:直到没有左or右子树
            return None
        node = Node(data)
        node.lchild = self.tree_creat(data_str)  # 当满足终止递归的条件时触发return None才能进一步往下走走到return node开始逐步往回收递归函数,并向父函数返回node
        node.rchild = self.tree_creat(data_str)

        return node

四、完整代码

python 复制代码
class Node():
    def __init__(self, data, lchild=None, rchild=None):
        self.data = data
        self.lchild = lchild
        self.rchild = rchild


class Tree():
    def __init__(self, root=None):
        self.root = root  # 根节点
        self.index = 0  # 用于遍历一串数据的索引

    def tree_creat(self, data_str):
        if self.index >= len(data_str):  # 最大条件,数据读取完之后直接返回
            return None

        # 开始创建,开始之前先读取对应的数据并且后移index
        data = data_str[self.index]
        self.index += 1
        # 开始递归,先写一个可以终止递归的目标条件
        if data == "#":  # 终止递归的条件:直到没有左or右子树
            return None
        node = Node(data)
        node.lchild = self.tree_creat(data_str)  # 当满足终止递归的条件时触发return None才能进一步往下走走到return node开始逐步往回收递归函数,并向父函数返回node
        node.rchild = self.tree_creat(data_str)

        return node


    def prior_show(self, node):
        """
        先序遍历、根左右
        """
        if node is not None:
            print(node.data, end=" ")
            self.prior_show(node.lchild)
            self.prior_show(node.rchild)

    def mid_show(self, node):
        """
        中序遍历、左根右

        """
        if node is not None:
            self.mid_show(node.lchild)
            print(node.data, end=" ")
            self.mid_show(node.rchild)

    def post_show(self, node):
        """
        后序遍历、左 右 根

        """
        if node is not None:
            self.post_show(node.lchild)
            self.post_show(node.rchild)
            print(node.data, end=" ")




if __name__ == '__main__':
    # data_str = input("输入二叉树:")
    data_str = "ABD##E##CFH###G##"
    # 创建二叉树
    tree = Tree()
    tree.root = tree.tree_creat(data_str)

    # 遍历
    print("先序遍历结果")
    tree.prior_show(tree.root)
    print()
    print("中序遍历结果")
    tree.mid_show(tree.root)
    print()
    print("后序遍历结果")
    tree.post_show(tree.root)
    print()

# ABD##E##CFH###G##
相关推荐
冷雨夜中漫步5 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴5 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再5 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
m0_736919107 小时前
C++代码风格检查工具
开发语言·c++·算法
喵手7 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934737 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy7 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
黎雁·泠崖8 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472469 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
肖永威9 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos