JAVA二叉树递归构造、二叉树普通遍历及递归遍历
二叉树类:
package com.antis.tree;
public class BinaryTree {
int data; //根节点数据
BinaryTree left; //左子树
BinaryTree right; //右子树
public BinaryTree(int data) //实例化二叉树类
{
this.data = data;
left = null;
right = null;
}
/**
* 向二叉树中插入子节点
* @param root
* @param data
*/
public void insert(BinaryTree root,int data){
//二叉树的左节点都比根节点小
if(data>root.data)
{
if(root.right==null){
root.right = new BinaryTree(data);
}else{
this.insert(root.right, data);
}
}else{
//二叉树的右节点都比根节点大
if(root.left==null){
root.left = new BinaryTree(data);
}else{
this.insert(root.left, data);
}
}
}
}
二叉树遍历代码:
package com.antis.tree;
import java.util.Stack;
public class BinaryTreeTraversal {
/**
* 先序遍历--先根遍历递归
* @param root
*/
public static void preOrder(BinaryTree root){ //先根遍历
if (root==null) {
return;
}
System.out.print(root.data+"-");
preOrder(root.left);
preOrder(root.right);
}
/**
* 中序遍历--中根遍历递归
* @param root
*/
public static void inOrder(BinaryTree root){ //中根遍历
if (root==null) {
return;
}
inOrder(root.left);
System.out.print(root.data+"--");
inOrder(root.right);
}
/**
* 后序遍历--后根遍历递归
* @param root
*/
public static void postOrder(BinaryTree root){ //后根遍历
if (root==null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data+"---");
}
// 先序遍历非递归
public static void preOrder2(BinaryTree t) {
Stack<BinaryTree> s = new Stack<BinaryTree>();
while (t != null || !s.empty()) {
while (t != null) {
System.out.print(t.data+"-");
s.push(t);
t = t.left;
}
if (!s.empty()) {
t = s.pop();
t = t.right;
}
}
}
// 中序遍历非递归
public static void InOrder2(BinaryTree t) {
Stack<BinaryTree> s = new Stack<BinaryTree>();
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
t = t.left;
}
if (!s.empty()) {
t = s.pop();
System.out.print(t.data+"--");
t = t.right;
}
}
}
// 后序遍历非递归
public static void PostOrder2(BinaryTree t) {
Stack<BinaryTree> s = new Stack<BinaryTree>();
Stack<Integer> s2=new Stack<Integer>();
Integer i=new Integer(1);
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
s2.push(new Integer(0));
t = t.left;
}
while (!s.empty() && s2.peek().equals(i)) {
s2.pop();
System.out.print(s.pop().data+"---");
}
if (!s.empty()) {
s2.pop();
s2.push(new Integer(1));
t = s.peek();
t = t.right;
}
}
}
public static void main(String[] str){
int[] array = {12,76,35,22,16,48,90,46,9,40};
BinaryTree root = new BinaryTree(array[0]); //创建二叉树
for(int i=1;i<array.length;i++){
root.insert(root, array[i]); //向二叉树中插入数据
}
System.out.println("先根遍历:");
preOrder(root);
System.out.println();
System.out.println("先根遍历:");
preOrder2(root);
System.out.println();
System.out.println("中根遍历:");
inOrder(root);
System.out.println();
System.out.println("中根遍历:");
InOrder2(root);
System.out.println();
System.out.println("后根遍历:");
postOrder(root);
System.out.println();
System.out.println("后根遍历:");
PostOrder2(root);
}
}
运行结果:
先根遍历:
12-9-76-35-22-16-48-46-40-90-
先根遍历:
12-9-76-35-22-16-48-46-40-90-
中根遍历:
9--12--16--22--35--40--46--48--76--90--
中根遍历:
9--12--16--22--35--40--46--48--76--90--
后根遍历:
9---16---22---40---46---48---35---90---76---12---
后根遍历:
9---16---22---40---46---48---35---90---76---12---
JAVA二叉树递归构造、二叉树普通遍历及递归遍历的更多相关文章
- 非递归遍历二叉树Java版的实现代码(没写层次遍历)
直接上代码呵呵,里面有注解 package www.com.leetcode.specificProblem; import java.util.ArrayList; import java.util ...
- java实现二叉树的前中后遍历(递归和非递归)
这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...
- 【Java】 二叉树的遍历(递归与循环+层序遍历)
在[Java] 大话数据结构(9) 树(二叉树.线索二叉树)一文中,已经实现了采用递归方法的前.中.后序遍历,本文补充了采用循环的实现方法.以及层序遍历并进行了一个总结. 递归实现 /* * 前序遍历 ...
- Java实现二叉树的先序、中序、后序、层序遍历(递归和非递归)
二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易 ...
- [LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)、108. 将有序数组转换为二叉搜索树(递归、二分)
题目 05. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次 ...
- Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序 ...
- Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中 ...
- 【算法】二叉树、N叉树先序、中序、后序、BFS、DFS遍历的递归和迭代实现记录(Java版)
本文总结了刷LeetCode过程中,有关树的遍历的相关代码实现,包括了二叉树.N叉树先序.中序.后序.BFS.DFS遍历的递归和迭代实现.这也是解决树的遍历问题的固定套路. 一.二叉树的先序.中序.后 ...
- Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历
package com.example.demo; public class BTree { public int data; public BTree left; public BTree rigt ...
随机推荐
- Java通过流对MP4视频文件进行加密,H5 video播放流
加密目标文件 代码如下: 不建议进行二次加密,若二次加密必须要二次解密 package com.xgt.util; import java.io.*; public class VideoEncode ...
- 自动生成数据库字典(sql2008) 转自 飘渺の云海
每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码. 截取一部分图片: 存储过程: SET ANSI_NULLS ON GO ...
- 在WPF中自定义控件
一, 不一定需要自定义控件在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费周章,因为控件可以嵌套使用以及可以为控件外观打造一套新的样 ...
- 2.Windows服务-->安装卸载服务
1.使用vs组件“VS2012开发人员命令提示” 工具,进行安装卸载服务(必须以“管理员身份运行") 安装和卸载的时候选择合适的安装程序工具地址,例如: 安装服务:C:\Windows\Mi ...
- spring设置字符编码过滤器
一.在web.xml中的配置 <!-- characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>chara ...
- 六、cent OS其它常用命令
进入根目录下的laycloud的目录cd /laycloud 进入当前目录下的目录cd laycloud 查看某个目录下的内容ls /laycloud 查看当前目录下的内容ls 查看当前目录下的内容读 ...
- 十三、nginx 强制下载txt等文件
当前的浏览器能够识别文件格式,如果浏览器本身能够解析就会默认打开,如果不能解析就会下载该文件. 那么使用nginx做资源服务器的时候,如何强制下载文件呢? location /back/upload/ ...
- Linux_vim文本编辑器指令整理
一般指令模式 : 可以移动光标,可以删除字符和删除整列,可以复制粘贴 编辑模式 : 按下"i, I, o, O, a, A, r, R"任意一个字母时进入;按下ESC退出编辑模式 ...
- 嵌套Golang对象的初始化
比如有这样一个对象: type ProductConfig struct { Site map[string]string } 对应的初始化可以如下写: var pc ProductCon ...
- linux设置静态获取ip
vsphere client 创建虚拟机后,默认是动态获取ip ,如果想要改为静态ip: 修改网卡eth0 (不一定每个人都是eth0,比如有的是ens160) vim /etc/sysconfig/ ...