java实现二叉树的相关操作
import java.util.ArrayDeque;
import java.util.Queue; public class CreateTree { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Node root=new Node();
root.data=9; Node temp01=new Node();
temp01.data=1;
root.left=temp01; Node temp02=new Node();
temp02.data=3;
root.right=temp02; Node temp03=new Node();
temp03.data=2;
root.left.left=temp03; Node temp04=new Node();
temp04.data=4;
root.left.right=temp04; Node temp05=new Node();
temp05.data=8;
root.right.left=temp05; Node temp06=new Node();
temp06.data=6;
root.left.left.left=temp06; Node temp07=new Node();
temp07.data=7;
root.left.left.right=temp07; System.out.println("--------先序遍历----------");
SelectTree1(root);
System.out.println();
System.out.println("---------中序遍历---------");
SelectTree(root);
System.out.println();
System.out.println("---------后序遍历---------");
SelectTree2(root);
System.out.println();
System.out.println("----------叶节点个数-----------");
int i=leafNum(root);
System.out.println(i);
System.out.println("----------层次遍历二叉树-----------------");
levelOrder(root); System.out.println();
int j=deep(root);
System.out.println("---------高度---------");
System.out.println(j); }
// 中序遍历
public static void SelectTree(Node root){
if(root==null)
return;
SelectTree(root.left);
System.out.print(root.data+" ");
SelectTree(root.right);
} // 先序遍历
public static void SelectTree1(Node root){
if(root==null)
return;
System.out.print(root.data+" ");
SelectTree1(root.left);
SelectTree1(root.right);
} // 后序遍历
public static void SelectTree2(Node root){
if(root==null)
return;
SelectTree2(root.left);
SelectTree2(root.right);
System.out.print(root.data+" "); } // 叶子数
public static int leafNum(Node node) {
if (node != null) {
if (node.left == null && node.right == null) {
return 1;
}
return leafNum(node.left)+leafNum(node.right);
}
return 0;
} //求二叉树的深度
public static int deep(Node node){
int h1, h2;
if(node == null)
{return 0;
}
else{
h1= deep(node.left);
h2= deep(node.right);
return (h1<h2)?h2+1:h1+1;
} } // 层次遍历
public static void levelOrder(Node node) {
if (node == null)
return;
Queue<Node> queue = new ArrayDeque<Node>();
queue.add(node);
while (!queue.isEmpty()) {
Node temp = queue.poll();
System.out.print(temp.data);
if (temp.left != null)
queue.add(temp.left);
if (temp.right != null)
queue.add(temp.right);
}
}
}
class Node{
boolean visited=false;
int data=0;
Node left=null;
Node right=null;
}
java实现二叉树的相关操作的更多相关文章
- 数据结构Java实现04---树及其相关操作
首先什么是树结构? 树是一种描述非线性层次关系的数据结构,树是n个数据结点的集合,这些集结点包含一个根节点,根节点下有着互相不交叉的子集合,这些子集合便是根节点的子树. 树的特点 在一个树结构中,有且 ...
- 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...
- Java实现二叉树及相关遍历方式
Java实现二叉树及相关遍历方式 在计算机科学中.二叉树是每一个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(r ...
- java 线程 原子类相关操作演示样例 thinking in java4 文件夹21.3.4
java 线程 原子类相关操作演示样例 package org.rui.thread.volatiles; import java.util.Timer; import java.util.Time ...
- java 的Date 日期相关操作
String 与 Date互转(1)基于SimpleDateFormat实现: package com.bky.df; import java.text.ParseException; import ...
- Java 二叉树遍历相关操作
BST二叉搜索树节点定义: /** * BST树的节点类型 * @param <T> */ class BSTNode<T extends Comparable<T>&g ...
- java实现安全证书相关操作
https://blog.csdn.net/zhushanzhi/article/details/77864516 版权声明:本文为博主原创文章,未经博主允许不得转载. package test; i ...
- POI开发:Java中的Excel相关操作
一.Apache POI 1.简介: Apache POI支持大多数中小规模的应用程序开发,提供API给Java程序对Microsoft Office格式档案读和写的功能,呈现和文本提取是它的主要特点 ...
- elasticsearch Java High Level REST 相关操作封装
pox.xml文件添加以下内容 <dependency> <groupId>org.elasticsearch.client</groupId> <artif ...
随机推荐
- 2014年1月9日 Oracle 内存与结构
Oracle启动时为启动一个实例 主要为 实例 SVG 数据库文件 其它文件 1.Oracle: 内存 进程 其他文件 1.1 SVG内存(Cache) 1.1.1 共享池(Shared Poo ...
- 利用JPEGImageEncoder进行简单的图片压缩
import java.awt.Dimension; import java.awt.Image; import java.awt.image.BufferedImage; import java.i ...
- 个人的IDE制作(vim)——适用于C++/C
引用文章A:http://learnvimscriptthehardway.onefloweroneworld.com/ 引用介绍:初学者建议通读一遍.对VIM能有整体性的了解. 引用文章B:http ...
- 为什么所有浏览器的userAgent都带Mozilla
参看下面链接:<为什么所有的浏览器的userAgent都带Mozilla>
- Mysql 记录
1.创建用户命令: <!---->mysql> CREATE USER yy IDENTIFIED BY '123'; yy表示你要建立的用户名,后面的123表示密码 上面建立的用户 ...
- oracle recyclebin详解(闪回删除的表)
今天在SOA应用数据库上运用DBMS_REDEFITION包进行在线非分区表转换分区表操作时,本想DROP掉建的临时表cube_scope_temp不小心后面忘记加"temp"直接 ...
- pat_1
2-0 2-1 #include <stdio.h> int main() { int inch,foot,cm; scanf("%d",&cm); foot= ...
- css3投影讲解、投影
迷茫了好一段时间,今天开始整理一下自己,同时也整理下新的知识. CSS3: 从头开始做起:现在在页面中用到最多的是图片/容器投影,文字投影: 接下来就总结一个投影问题: box-shadow:阴影类型 ...
- Retrofit的使用
参照文档:http://gank.io/post/56e80c2c677659311bed9841 一.创建Retrofit mRetrofit = new Retrofit.Builder() .b ...
- MySQL命令记录1
mysql命令行 开启:net start mysql56关闭:net start mysql56(这两种情况必须有管理员权限) 登陆:mysql -h localhost -u root -p(lo ...