java遍历树结构数据_Java数据结构——二叉树的遍历(汇总)

二叉树的遍历分为深度优先遍历(DFS)和广度优先遍历(BFS)

DFS遍历主要有:

前序遍历

中序遍历

后序遍历

一、递归实现DFS

Node.java:

public class Node {

private Object data;

Node richild;

Node lechild;

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

public Node getRichild() {

return richild;

}

public void setRichild(Node richild) {

this.richild = richild;

}

public Node getLechild() {

return lechild;

}

public void setLechild(Node lechild) {

this.lechild = lechild;

}

public Node(Object data, Node lechild, Node richild) {

super();

this.data = data;

this.richild = richild;

this.lechild = lechild;

}

public Node() {

super();

}

}

递归遍历:

public class BTree {

private static Node root;

//构造树

public static void init() {

Node node1 = new Node("A", null, null);

Node node2 = new Node("B", node1, null);

Node node3 = new Node("C", null, null);

Node node4 = new Node("D", node2, node3);

Node node5 = new Node("E", null, null);

Node node6 = new Node("F", null, node5);

Node node7 = new Node("G", node4, node6);

root = node7;

}

//访问节点

public static void visited(Node n) {

System.out.print(n.getData() + " ");

}

//前序遍历

public static void preOrder(Node n) {

if (n != null) {

visited(n);

preOrder(n.getLechild());

preOrder(n.getRichild());

}

}

//中序遍历

public static void inOrder(Node n) {

if (n != null) {

inOrder(n.getLechild());

visited(n);

inOrder(n.getRichild());

}

}

//后序遍历

public static void postOrder(Node n) {

if (n != null) {

postOrder(n.getLechild());

postOrder(n.getRichild());

visited(n);

}

}

public static void main(String[] args) {

init();

System.out.print("递归前序:");

preOrder(root);

System.out.println();

System.out.print("递归中序:");

inOrder(root);

System.out.println();

System.out.print("递归后序:");

postOrder(root);

System.out.println();

}

}

二、非递归实现DFS(依靠栈)

//前序遍历

public static void preOrder(Node n) {

System.out.print("非递归前序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

System.out.print(n.getData() + " ");

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

index--;

n = n.getRichild();

}

}

//中序遍历

public static void inOrder(Node n) {

System.out.print("非递归中序:");

Stack stack = new Stack<>();

int index = 0;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.pop();

System.out.print(n.getData() + " ");

index--;

n = n.getRichild();

}

}

//后序遍历

public static void postOrder(Node n) {

System.out.print("非递归后序:");

Stack stack = new Stack<>();

int index = 0;

Node lastVisited = null;

while (n != null || index > 0) {

while (n != null) {

stack.push(n);

index++;

n = n.getLechild();

}

n = stack.peek();

if (n.getRichild() == null || n.getRichild() == lastVisited) {

System.out.print(n.getData() + " ");

lastVisited = n;

index--;

stack.pop();

n = null;

} else {

n = n.getRichild();

}

}

}

三、实现层序遍历(依靠队列)

public static void LevenOrder(Node root) {

if (root == null) {

return;

}

Queue queue = new LinkedList<>();

queue.add(root);

Node temp = null;

while (!queue.isEmpty()) {

temp = queue.poll();

visited(temp);

if (temp.getLeChild() != null) {

queue.add(temp.getLeChild());

}

if (temp.getRChild() != null) {

queue.add(temp.getChild());

}

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/396318.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue 移动端头像裁剪_使用vue-cropper裁剪正方形上传头像-阿里云开发者社区

引用方式在组件内使用import { VueCropper } from vue-croppercomponents: {VueCropper,},main.js里面使用import VueCropper from vue-cropperVue.use(VueCropper)基本使用方法ref"cropper":img"option.img":autoCrop"true":fixedNumber"[…

规则引擎 设计 git_引擎盖下的Git

规则引擎 设计 gitby Wassim Chegham由Wassim Chegham 引擎盖下的Git (Git under the hood) Let’s explore some common Git commands, and dive into its internals to see what Git does when you run them.让我们探索一些常见的Git命令&#xff0c;并深入了解其内部&#…

练习题之死锁

public class PrintMain {public static String obj1"obj1";public static String obj2"obj2";public static void main(String[] args) {new Thread(new Runnable() {public void run() {System.out.println(new Date().toString "LockA开始执行&qu…

启用或禁用对 Exchange Server 中的邮箱的 POP3 或 IMAP4 访问

https://docs.microsoft.com/zh-cn/Exchange/clients/pop3-and-imap4/configure-mailbox-access?viewexchserver-2019 记录下转载于:https://www.cnblogs.com/amoy9812/p/9875426.html

java有什么压力_编程语言的心智负担!你学编程得有多大的压力快来测试一下...

很多编程语言对比的文章&#xff0c;总喜欢比较各种编程语言的性能、语法、IO模型。本文将从心智负担这个角度去比较下不同的编程语言和技术。内存越界如&#xff1a;C语言、C(C with class)C/C可以直接操作内存&#xff0c;但编程必须要面对内存越界问题。发生内存越界后&…

什么叫有效物理网卡_如何区分虚拟网卡和物理网卡?-阿里云开发者社区

一、什么是物理网卡和虚拟网卡&#xff1f;图示如下&#xff1a;红色部分包含VMWare的为虚拟网卡。通常&#xff0c;我们部署VMWare虚拟机、VMSphere虚拟集群、XenCenter虚拟集群是都会涉及虚拟网卡。二、辨别物理网卡和虚拟网卡的应用场景场景一&#xff1a;一般部署虚拟集群的…

算法复杂度的表示法_用简单的英语算法:时间复杂度和Big-O表示法

算法复杂度的表示法by Michael Olorunnisola通过Michael Olorunnisola 用简单的英语算法&#xff1a;时间复杂度和Big-O表示法 (Algorithms in plain English: time complexity and Big-O notation) Every good developer has time on their mind. They want to give their us…

Android Studio 开始运行错误

/********************************************************************************* Android Studio 开始运行错误* 说明&#xff1a;* 打开Android Studio就抛出这个错误。* * 2017-4-1 深圳 南…

IOS 计步器

这篇博客介绍的是当前比较流行的“计步器”-只是简单的知识点 计步器的实现在IOS8开始进行了改变。 但是我会对之前之后的都进行简单介绍。 IOS 8 - // // ViewController.m // CX 计步器 // // Created by ma c on 16/4/12. // Copyright © 2016年 bjsxt. All rights…

vue学习之二ECMAScript6标准

一、ECMAScript6标准简述 ECMAScript 6.0&#xff08;以下简称 ES6&#xff09;是 JavaScript 语言的下一代标准&#xff0c;已经在 2015 年 6 月正式发布了。它的目标&#xff0c;是使得 JavaScript 语言可以用来编写复杂的大型应用程序&#xff0c;成为企业级开发语言。 1.1E…

抖音吸粉_抖音吸粉5大实用方法首次分享!轻松实现粉丝10000+

抖音&#xff0c;是一款可以拍短视频的音乐创意短视频社交软件&#xff0c;该软件于2016年9月上线&#xff0c;是一个专注年轻人音乐短视频社区。用户可以通过这款软件选择歌曲&#xff0c;拍摄音乐短视频&#xff0c;形成自己的作品。抖音APP仅推出半年&#xff0c;用户量就突…

mapper mysql 主键_实现通用mapper主键策略兼容mysql和oracle

【原创文章&#xff0c;转载请注明原文章地址&#xff0c;谢谢&#xff01;】1.直接用官方提供的注解方法是无法达到兼容效果的2.跟踪源码看看是否有其他方法3.这里有个genSql&#xff0c;可以看一下这个类4.创建一个自定义的处理类实现GenSql(代码中是我实际项目中用到的策略&…

权限分配界面 纯手工 仅用到bootstrap的架构 以及 c标签

<div class"form-group"> <div class"row"> <label class"col-sm-2 control-label">配置权限</label> <div class"col-sm-10"> <c:forEach var"m" items…

数据管理与数据库 大学课程_根据数据显示的50种最佳免费在线大学课程

数据管理与数据库 大学课程When I launched Class Central back in November 2011, there were around 18 or so free online courses, and almost all of them were from Stanford.当我在2011年11月推出Class Central时&#xff0c;大约有18项免费在线课程&#xff0c;几乎所有…

每天一个linux命令(12):more命令

more命令&#xff0c;功能类似 cat &#xff0c;cat命令是整个文件的内容从上到下显示在屏幕上。 more会以一页一页的显示方便使用者逐页阅读&#xff0c;而最基本的指令就是按空白键&#xff08;space&#xff09;就往下一页显示&#xff0c;按 b 键就会往回&#xff08;back&…

java 面试题 由浅入深_面试官由浅入深的面试套路

阅读文本大概需要3分钟。从上图看来面试官面试是有套路的&#xff0c;一不小心就一直被套路。0x01&#xff1a;Thread面试官&#xff1a;创建线程有哪几种方式&#xff1f;应聘者&#xff1a;继承Thread类、实现Runable接口、使用j.u.c中的线程池面试官&#xff1a;继承Thread类…

怎么用centos7运行c语言程序_centos如何编译c语言代码

centos如何编译c语言代码,文件,选项,作用,链接,程序 centos如何编译c语言代码 易采站长站,站长之家为您整理了centos如何编译c语言代码的相关内容。 编译c,c++代码 安装gcc 1、使用如下命令查询 centos 官方gcc的所有包:yum -list gcc* 可安装的软件包gcc.x86_64gcc-c++.x86…

第四篇:基本数据类型及用法(1)

字符串&#xff08;str型&#xff09; -可以做加法&#xff0c;乘法 乘法例&#xff1a; n1"alex" n2n1*3 print(n2) #结果&#xff1a;alexalexalex -首字母大写: capitalize() -所有字母变小写: casefold()、lower() #casefold更牛&#xff0c;很多未知的对应关系也…

Android Studio 错误集

错误列表与解决方案: 1.Android studio Gradle project sync failed Android studio 构建项目出错 Error:Unable to start the daemon process: could not reserve enough space for object heap.Please assign more memory to Gradle in the projects gradle.properties file.…

需求简报_代码简报:我如何通过做自己喜欢的事情来获得顶级技术实习

需求简报Here are three stories we published this week that are worth your time:这是我们本周发布的三个值得您关注的故事&#xff1a; How I landed a top-tier tech internship by doing something I love: 7 minute read 我如何通过做自己喜欢的事情获得一流的技术实习…