当前位置: 首页 > news >正文

数据结构-5(二叉树)

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

 二、遍历        

    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=" ")

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

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

四、完整代码 

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

http://www.dtcms.com/a/295836.html

相关文章:

  • pytorch-geometric包(torch_scatter、torch_sparse、torch_cluster)
  • 服务器带宽具体是指什么意思?
  • PyTorch中神经网络的模型构建
  • 钉钉DingTalk完整版下载离线安装包2025
  • 【小董谈前端】【样式】 CSS与样式库:从实现工具到设计思维的跨越
  • ThinkPHP8集成RabbitMQ的完整案例实现
  • C# 方法执行超时策略
  • [Python] -进阶理解5- Python 模块与包的导入机制解析
  • uniapp中mp-html使用方法
  • 特定日志输出aop实现
  • day62-可观测性建设-全链路监控zabbix+grafana
  • Redis的事务和Lua之间的区别
  • day13 flash
  • 「iOS」黑魔法——方法交换
  • 告别束缚:这款“隐形心电监测仪”让心脏健康管理更自由
  • JavaSE:开发环境的搭建(Eclipse)
  • 企业级数据分析创新实战:基于表格交互与智能分析的双引擎架构
  • 从0到1学习c++ 命名空间
  • 《 java 随想录》| 数组
  • MySQL的命令行客户端
  • 探索双链表:C语言中的链式结构魔法
  • 光谱仪杂散光性能分析
  • 大疆无人机炸机后视频损坏的完美修复案例解析
  • uni-file-picker vue3二次封装(本地上传 + v-model)
  • Mysql命令show processlist
  • Linux基础服务(autofs和Samba)
  • 论文阅读:《针对多目标优化和应用的 NSGA-II 综述》一些关于优化算法的简介
  • OpenCV —— color_matrix_numpy_mat_reshape
  • 新mac电脑软件安装指南(前端开发用)
  • 解决http的web服务中与https服务交互的问题