数据结构-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##
相关推荐
瓦特what?15 分钟前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
祁同伟.1 小时前
【C++】动态内存管理
开发语言·c++
一只鲲1 小时前
40 C++ STL模板库9-容器2-vector
开发语言·c++
重生之我是Java开发战士1 小时前
【数据结构】深入理解单链表与通讯录项目实现
数据结构·链表
励志不掉头发的内向程序员1 小时前
C++基础——内存管理
开发语言·c++
天才测试猿1 小时前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
mortimer1 小时前
一次与“顽固”外部程序的艰难交锋:subprocess 调用exe踩坑实录
windows·python·ai编程
tanxiaomi1 小时前
数据库索引视角:对比二叉树到红黑树再到B树
数据结构·数据库·b树
lifallen1 小时前
JCTools 无锁并发队列基础:ConcurrentCircularArrayQueue
java·开发语言·数据结构·算法
千里镜宵烛2 小时前
深入理解 Linux 线程:从概念到虚拟地址空间的全面解析
开发语言·c++·操作系统·线程