博客
关于我
java遍历二叉树
阅读量:202 次
发布时间:2019-02-28

本文共 3518 字,大约阅读时间需要 11 分钟。

问题分析

由于不论先序还是中序、后序遍历,在程序运行流程上没有本质区别,区别仅仅是访问语句的执行时间点。因此,只要有了先序的程序流程,改变一下访问语句代码位置,就可以实现前三种遍历方式。

先序遍历(非递归)

先序遍历是从树的根节点开始,依次访问左节点和右节点。非递归实现使用栈来模拟调用栈,确保每个节点只被访问一次。

public void visitFirst_N(Node tree) {    if (tree != null) {        Stack
stack = new Stack<>(); stack.push(tree); Node current = tree; while (current != null) { System.out.print(current.value + " "); current = current.left; if (current != null) { stack.push(current); } else { while (!stack.isEmpty()) { Node node = stack.pop(); current = node.right; if (current != null) { stack.push(current); break; } } } } }}

中序遍历(非递归)

中序遍历是从树的根节点开始,先访问左节点,后访问右节点。非递归实现仍然使用栈,但处理方式有所不同。

public void midTest(Node tree) {    if (tree != null) {        Stack
stack = new Stack<>(); stack.push(tree); Node current = tree; while (current != null) { current = current.left; if (current != null) { stack.push(current); } else { while (!stack.isEmpty()) { Node node = stack.pop(); System.out.print(node.value + " "); current = node.right; if (current != null) { stack.push(current); break; } } } } }}

后序遍历(非递归)

后序遍历是从树的根节点开始,先访问右节点,再访问左节点。由于栈的先进后出特性,需要额外处理栈中的节点,确保左节点在栈底部。

public void LastTest(Node tree) {    if (tree != null) {        Stack
stack = new Stack<>(); stack.push(tree); Node current = tree; while (current != null) { current = current.left; if (current != null) { stack.push(current); } else { while (!stack.isEmpty()) { Node node = stack.peek(); current = node.right; if (current == null) { Node x = stack.pop(); System.out.print(x.value + " "); Node y = stack.pop(); if (y.left == x) { stack.push(y); current = y.right; } else { Node k = stack.peek(); System.out.print(y.value + " "); if (k.right == y) { while (!stack.isEmpty()) { System.out.print(stack.pop().value + " "); } return; } current = k.right; } } if (current != null) { stack.push(current); break; } } } } }}

层次遍历(非递归)

层次遍历是按层访问树节点。使用队列来实现,先处理队列中的节点,输出其值,然后将左孩子和右孩子入队。

private void visitLevel(Node x) {    Queue
queue = new Queue<>(); while (x != null) { System.out.print(x.value + " "); if (x.left != null) { queue.enqueue(x.left); } if (x.right != null) { queue.push(x.right); } x = queue.deQueue(); }}

通过上述实现,可以清晰地看到不同遍历方式在非递归情况下的实现差异。

转载地址:http://mpvi.baihongyu.com/

你可能感兴趣的文章
Nginx运维与实战(二)-Https配置
查看>>
Nginx配置ssl实现https
查看>>
Nginx配置TCP代理指南
查看>>
Nginx配置——不记录指定文件类型日志
查看>>
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
Nginx配置参数中文说明
查看>>
Nginx配置好ssl,但$_SERVER[‘HTTPS‘]取不到值
查看>>
Nginx配置如何一键生成
查看>>
Nginx配置实例-负载均衡实例:平均访问多台服务器
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
查看>>
NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
查看>>
Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
查看>>
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>