java 建树源码_Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】...
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
//二叉树的建树,前中后 递归非递归遍历 层序遍历
//Node节点
class Node {
int element;
Node left;
Node right;
public Node() {
}
public Node(int element) {
this.element = element;
}
}
// BinaryTree
public class Tree {
// creat tree from array
public static Node creatTree(int[] data, int i) {
if (i >= data.length || data[i] == -1)
return null;
Node temp = new Node(data[i]);
temp.left = creatTree(data, i * 2 + 1);
temp.right = creatTree(data, i * 2 + 2);
return temp;
}
// pre前序遍历递归
public static void pre(Node temp) {
if (temp == null)
return;
System.out.print(temp.element + " ");
pre(temp.left);
pre(temp.right);
}
// mid中序遍历递归
public static void mid(Node temp) {
if (temp == null)
return;
mid(temp.left);
System.out.print(temp.element + " ");
mid(temp.right);
}
// last后序遍历递归
public static void last(Node temp) {
if (temp == null)
return;
last(temp.left);
last(temp.right);
System.out.print(temp.element + " ");
}
// pre1前序遍历非递归
public static void pre1(Node temp) {
Stack stack = new Stack<>();
while (temp != null || !stack.isEmpty()) {
while (temp != null) {
stack.push(temp);
System.out.print(temp.element + " ");
temp = temp.left;
}
if (!stack.isEmpty()) {
temp = stack.pop().right;
}
}
}
// mid1中序遍历非递归
public static void mid1(Node temp) {
Stack stack = new Stack<>();
while (temp != null || !stack.isEmpty()) {
while (temp != null) {
stack.push(temp);
temp = temp.left;
}
if (!stack.isEmpty()) {
temp = stack.pop();
System.out.print(temp.element + " ");
temp = temp.right;
}
}
}
// last1后序遍历非递归
public static void last1(Node temp) {
Stack stack = new Stack<>();
Stack stack2 = new Stack<>();
while (temp != null || !stack.isEmpty()) {
while (temp != null) {
stack.push(temp);
stack2.push(temp);
temp = temp.right;
}
if (!stack.isEmpty()) {
temp = stack.pop().left;
}
}
while (!stack2.isEmpty())
System.out.print(stack2.pop().element + " ");
}
// ceng层序遍历
public static void ceng(Node temp) {
if (temp == null)
return;
Queue queue = new ArrayDeque<>();
queue.offer(temp);
while (!queue.isEmpty()) {
temp = queue.poll();
System.out.print(temp.element + " ");
if (temp.left != null)
queue.offer(temp.left);
if (temp.right != null)
queue.offer(temp.right);
}
}
// Demo
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 };
Node tree = creatTree(array, 0);
System.out.println("脚本之家测试结果:");
pre(tree);
System.out.println();
pre1(tree);
System.out.println();
mid(tree);
System.out.println();
mid1(tree);
System.out.println();
last(tree);
System.out.println();
last1(tree);
System.out.println();
ceng(tree);
}
}
运行结果:
希望本文所述对大家java程序设计有所帮助。
您可能感兴趣的文章:
java 建树源码_Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】...相关推荐
- java 操作oracle 源码_java对oracle的常用操作示例源码
将做工程过程重要的代码片段做个收藏,下面资料是关于java对oracle的常用操作示例的代码. import java.sql.Connection; import java.sql.DriverMa ...
- 二叉树N叉数的前中后序遍历总结,python实现递归法和迭代法
关于二叉树的前序遍历(preoder).中序遍历(inorder)和后序遍历(postorder),实际上只需要记住:左子节点一定在右子节点的左边(左右),所谓前中后序遍历就是根节点的位置不同,前序是 ...
- java商城源码_java 多商户商城系统源码分享
三勾商城多商户是开发友好的微信小程序商城,框架支持SAAS,支持发布 iOS + Android + 公众号 + H5 + 各种小程序(微信/支付宝/百度/头条/QQ/钉钉/淘宝)等多个平台,不可多得 ...
- java建树_Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】...
本文实例讲述了Java实现的二叉树常用操作.分享给大家供大家参考,具体如下: import java.util.ArrayDeque; import java.util.Queue; import j ...
- 代码随想录——二叉树(一):前中后序遍历,层序遍历,翻转
题目来自:https://www.programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80. ...
- 二叉树的相关性质及其前中后层序遍历实现
写在前面: 本文前面部分介绍相关概念,后面部分是程序. 点击下面链接查看更多! 点击此处发现更多 一.二叉树的概念 1.1 相关术语 ①结点:包含一个数据元素及若干指向子树分支的信息 . ②结点的度: ...
- java验证码源码_Java通用验证码程序及应用示例(提供源码下载)
评论 # re: Java通用验证码程序及应用示例(提供源码下载) 2009-11-27 17:09 零全零美 多谢博主写出这么好的总结,如果能加上汉字验证码,会更完美! 回复 更多评论 # re ...
- java hashset 源码_Java集合源码分析-HashSet和LinkedHashSet
前两篇文章分别分析了Java的ArrayList和LinkedList实现原理,这篇文章分析下HashSet和LinkedHashSet的源码.重点讲解HashSet,因为LinkedHashSet是 ...
- java linkedlist源码_Java集合之LinkedList源码分析
一.LinkedList简介 LinkedList是一种可以在任何位置进行高效地插入和移除操作的有序序列,它是基于双向链表实现的. ps:这里有一个问题,就是关于实现LinkedList的数据结构是否 ...
最新文章
- 基于椭圆拟合的环岛识别方法
- c语言第七章函数选择答案,C语言选择题及答案
- oracle 本地使用命令导入数据到远程主机
- 引路蜂地图API:Gis.Navigation包定义
- struts2中一些常用的写法 记录
- JavaScript,等比例缩放图片的函数,很好用。
- SAP License:备选统驭科目问题
- Java 清除指定目录文件夹下文件
- 机器学习重塑供应链管理的10个途径
- VirtualBox中,WIN虚拟机与WIN共享文件夹
- wps 打开xml格式乱码_“WPS不兼容EXCEL打开后出现乱码怎么解决“xlsx文件打开是乱码怎么办...
- 升余弦滤波器与无码间串扰(一)
- vsftp虚拟账户登录失败331 Please specify the password.
- 使用Mybatis进行更新操作成功,数据库却没更新的原因
- 2014522420145238《信息安全系统设计基础》实验一 开发环境的熟悉
- 对抗博弈搜索——吃豆人
- 笔记1之极大值分布问题
- MP4/MP3解封装ffmpeg(十三)
- Python之高等数学(定积分与不定积分,重积分)
- DM10分区图文教程