博客
关于我
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/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>