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实现二叉树的相关操作的更多相关文章

  1. 数据结构Java实现04---树及其相关操作

    首先什么是树结构? 树是一种描述非线性层次关系的数据结构,树是n个数据结点的集合,这些集结点包含一个根节点,根节点下有着互相不交叉的子集合,这些子集合便是根节点的子树. 树的特点 在一个树结构中,有且 ...

  2. 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

    将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...

  3. Java实现二叉树及相关遍历方式

    Java实现二叉树及相关遍历方式 在计算机科学中.二叉树是每一个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(r ...

  4. java 线程 原子类相关操作演示样例 thinking in java4 文件夹21.3.4

    java 线程  原子类相关操作演示样例 package org.rui.thread.volatiles; import java.util.Timer; import java.util.Time ...

  5. java 的Date 日期相关操作

    String 与 Date互转(1)基于SimpleDateFormat实现: package com.bky.df; import java.text.ParseException; import ...

  6. Java 二叉树遍历相关操作

    BST二叉搜索树节点定义: /** * BST树的节点类型 * @param <T> */ class BSTNode<T extends Comparable<T>&g ...

  7. java实现安全证书相关操作

    https://blog.csdn.net/zhushanzhi/article/details/77864516 版权声明:本文为博主原创文章,未经博主允许不得转载. package test; i ...

  8. POI开发:Java中的Excel相关操作

    一.Apache POI 1.简介: Apache POI支持大多数中小规模的应用程序开发,提供API给Java程序对Microsoft Office格式档案读和写的功能,呈现和文本提取是它的主要特点 ...

  9. elasticsearch Java High Level REST 相关操作封装

    pox.xml文件添加以下内容 <dependency> <groupId>org.elasticsearch.client</groupId> <artif ...

随机推荐

  1. window服务创建

    第一步:创建服务 第二步:在Service1.cs视图中 右键 选择”添加安装程序” 这里要注意几个细节 设置上面的属性 这两个分别有属性,具体网上查使用方式 3 实例代码编写 主要下面几个方法 pr ...

  2. <转>Java 理论与实践: 正确使用 Volatile 变量

    Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized”:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少, ...

  3. W - stl 的 优先队列 Ⅲ

    Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...

  4. 天天模拟器极速畅玩靠谱游戏《仙境传说RO:复兴》

    在电脑上用模拟器打开手游<仙境传说RO:复兴>,今天小编就来写一写天天模拟器的试玩教学. 首先先打开天天模拟器极速版. 在界面中找到鱼图标的靠谱游戏应用中心. 在应用中心中找到<仙境 ...

  5. IntelliJ IDEA 14使用笔记

    由eclipse到IDEA IDEA与eclipse的区别: IDEA的project对应eclipse的workspace: IDEA的module对应eclipse的project的. 所以要想在 ...

  6. inflate, findViewById与setContentView的区别与联系

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  7. Github获取仓库最新Release版本号API

    package me.chunsheng.hongbao.utils; import android.content.Context; import android.content.Intent; i ...

  8. python执行外部程序模块pyshell

    写python程序的时候需要用到调用外部命令的模块,看了一下,还真不少,头疼,用着不顺手.根据官网推荐,我根据官网的subprocess模块定制了一个自己的shell,同时借鉴了github上面的sh ...

  9. Sublime Text 3 个人配置文件

    { "dpi_scale": 1.0, "draw_white_space": "selection", "fallback_en ...

  10. The package does not support the device architecture (x86). You can change the supported architectures in the Android Build section of the Project Opt

    The package does not support the device architecture (x86). You can change the supported architectur ...