class Node:     # 定义树节点def __init__(self, value, left=None, right=None):   # 节点的值及左子树、右子树self.value = valueself.left = leftself.right = rightclass Tree:         # 定义二叉树def __init__(self, list):self.list = listself.build_tree()def build_tree(self):      # 构建二叉树,从上到下,从左往右nodelist = []       # 构建节点列表for i in range(len(list)):nodelist.append(Node(list[i]))  # 将列表元素添加至节点列表self.root = nodelist[0]     # 树的根节点for i in range(len(list) // 2): # 遍历节点,构建二叉树nodelist[i].left = nodelist[i * 2 + 1]if i * 2 + 2 < len(nodelist):nodelist[i].right = nodelist[i * 2 + 2]def recursive_pre_order(self, nodelist, node):         # 递归前序遍历,输出顺序:父节点,左子树,右子树if node is not None:nodelist.append(node.value)self.recursive_pre_order(nodelist, node.left)self.recursive_pre_order(nodelist, node.right)return nodelistdef recursive_in_order(self, nodelist, node):         # 递归中序遍历,输出顺序:左子树,父节点,右子树if node is not None:self.recursive_in_order(nodelist, node.left)nodelist.append(node.value)self.recursive_in_order(nodelist, node.right)return nodelistdef recursive_post_order(self, nodelist, node):        # 递归后序遍历,输出顺序:左子树,右子树,父节点if node is not None:self.recursive_post_order(nodelist, node.left)self.recursive_post_order(nodelist, node.right)nodelist.append(node.value)return nodelist@staticmethoddef not_recursive_pre_order(self, node):         # 非递归前序遍历,输出顺序:父节点,左子树,右子树if node is not None:stack = [node]  # 将根节点压入栈while len(stack) > 0:print(node.value, end=" ")if node.right is not None:  # 根据栈先进后出的特点,将子节点压栈的时候,先将右子树压入栈stack.append(node.right)if node.left is not None:   # 将左子树压入栈stack.append(node.left)node = stack.pop()  # 将栈顶元素弹出@staticmethoddef not_recursive_in_order(self, node):         # 非递归中序遍历,输出顺序:左子树,父节点,右子树if node is not None:stack = []temp = node     # 将根节点复制给中间变量while temp is not None or len(stack) > 0:if temp is not None:stack.append(temp)temp = temp.left    # 中序遍历先遍历节点的左子树else:temp = stack.pop()  # 将栈顶元素弹出print(temp.value, end=" ")  # 输出栈顶元素的值temp = temp.right       # 检查右子树@staticmethoddef not_recursive_post_order(self, node):        # 非递归后序遍历,输出顺序:左子树,右子树,父节点stack1 = [node]     # 栈 1 遍历二叉树stack2 = []     # 栈 2 存储输出顺序while len(stack1) > 0:node = stack1.pop()stack2.append(node)     # 将父节点压入栈底if node.left is not None:stack1.append(node.left)    # 左子树压入栈if node.right is not None:stack1.append(node.right)   # 右子树压入栈while len(stack2) > 0:print(stack2.pop().value, end=" ")  # 输出顺序if __name__ == '__main__':list = [1, 2, 3, 4]tree = Tree(list)print("递归前序排列")retList = tree.recursive_pre_order([], tree.root)print(retList)print("非递归前序排列")tree.not_recursive_pre_order(tree, tree.root)print("\n递归中序排列")retList = tree.recursive_in_order([], tree.root)print(retList)print("非递归中序排列")tree.not_recursive_in_order(tree, tree.root)print("\n递归后序排列")retList = tree.recursive_post_order([], tree.root)print(retList)print("非递归后序排列")tree.not_recursive_post_order(tree, tree.root)

  递归的方法即不断调用自身,非递归采用入栈出栈完成二叉树的遍历

转载于:https://www.cnblogs.com/soloveu/p/10192579.html

二叉树的遍历(递归与非递归)相关推荐

  1. 二叉树的非递归遍历(递归和非递归)

    二 叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是 递归定义,因此采用递归的方法去实现树的三种遍历不仅 ...

  2. 遍历二叉树的各种操作(非递归遍历)

    先使用先序的方法建立一棵二叉树,然后分别使用递归与非递归的方法实现前序.中序.后序遍历二叉树,并使用了两种方法来进行层次遍历二叉树,一种方法就是使用STL中的queue,另外一种方法就是定义了一个数组 ...

  3. 二叉树的几种递归和非递归式遍历:

    二叉树的几种递归和非递归式遍历: 1 #include <fstream> 2 #include <iostream> 3 4 using namespace std; 5 6 ...

  4. 分别用递归和非递归方式实现二叉树先序、中序和后序遍历(java实现)

    分别用递归和非递归方式实现二叉树先序.中序和后序遍历 用递归和非递归方式,分别按照二叉树先序.中序和后序打印所有的节点.我们约定:先序遍历顺序 为根.左.右;中序遍历顺序为左.根.右;后序遍历顺序为左 ...

  5. 二叉树的递归和非递归遍历

    二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易 ...

  6. 二叉树路径应用举例(基于非递归后序遍历)

    #include "stdafx.h" #include <iostream> #include <fstream>using namespace std; ...

  7. 转载:二叉树的前中后和层序遍历详细图解(递归和非递归写法)

    二叉树的前中后和层序遍历详细图解(递归和非递归写法) Monster_ii 2018-08-27 17:01:53 50530 收藏 403 分类专栏: 数据结构拾遗 文章标签: 二叉树 前序 中序 ...

  8. 九十五、二叉树的递归和非递归的遍历算法模板

    @Author:Runsen 刷Leetcode,需要知道一定的算法模板,本次先总结下二叉树的递归和非递归的遍历算法模板. 二叉树的四种遍历方式,前中后加上层序遍历.对于二叉树的前中后层序遍历,每种遍 ...

  9. 二叉树层序遍历递归与非递归_二叉树基础(1)-构建和遍历(递归和非递归)...

    二叉树的构建有2种方式:1.直接输入数字.2.根据两种顺序来判断另外一中顺序(后面会提到) 这里分享第一种构建方式,二叉树的前中后三种遍历方式(递归和非递归版本),和二叉树的层次遍历. 见代码demo ...

  10. 二叉树的遍历:先序 中序 后序遍历的递归与非递归实现及层序遍历

    二叉树的定义:一种基本的数据结构,是一种每个节点的儿子数目都不多于2的树 树节点的定义如下: // 树(节点)定义 struct TreeNode {int data; // 值TreeNode* l ...

最新文章

  1. markdown编辑器的小建议
  2. python跟php如何共用mysql_Python 3 多个函数共用一个mysql连接
  3. ios获得通讯录中联系人的所有属性
  4. 加分二叉树 java_P1040 加分二叉树
  5. java将生成数据写入文件_JAVA-将内容写入文件并导出到压缩包
  6. python添加excel模块,Python Excel操作——xlrd、xlwd,,读取1、导入模块 i
  7. Apache POI 读写 Word、PPT、Excel
  8. 频谱感知4:CCS硬合并中m-out-of-K准则下m与K的联合优化问题
  9. C语言项目源码2022必看必学版
  10. matlab-plot绘制点线图
  11. 微信小程序购物车商品单选、多选、数量变化、结算等
  12. 紧急求助,CSDN帮忙下载HP惠普280G1增霸卡7.0UEFI版硬盘保护卡还原卡驱动光盘
  13. oracle instantclient 64,instantclient 64位
  14. Chrome 插件下载网站+6个实用插件推荐
  15. DeepStream初步学习
  16. C++ 批量修改文件名
  17. java未来三年的工作计划,未来三年的工作计划
  18. IDEA使用技巧 - 修改项目名称和模块名称
  19. 镭速关于高速数据传输的介绍!
  20. 计算机网络码片序列计算问题

热门文章

  1. FastAI 2019课程学习笔记 lesson 2:自行获取数据并创建分类器
  2. 深度学习的分布式训练--数据并行和模型并行
  3. LeetCode简单题之按既定顺序创建目标数组
  4. 如何在TVM上集成Codegen(上)
  5. 3D点云重建原理及Pytorch实现
  6. 自监督学习(Self-Supervised Learning)多篇论文解读(下)
  7. 端云一体人工智能开发平台整体架构
  8. CPU消耗,跟踪定位理论与实践
  9. 模拟Servlet本质
  10. 2021年大数据HBase(十七):❤️HBase的360度全面调优❤️