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. exchange邮箱的”单点登陆“

    在跟exchange集成登陆时,通常有这样的需求,用户需要点击邮件链接的时候直接打开,不再需要输入用户名密码,实现所谓的单点登陆. 通常有两种方式 1.form认证 登陆原理:用js模拟表单登陆 代码 ...

  2. [转]C# 中的.pdb/ .vshost.exe/ .vshost.exe.manifest文件讨论

    原文出处:http://blog.163.com/chwei_sunshine/blog/static/19412628320125893656652/ pdb文件: 英文全称:Program Dat ...

  3. hibernate:XXX is not mapped

    hibernate:XXX is not mapped  检查项目中是否将hbm.xml引入

  4. V - stl 的 优先队列 Ⅱ

    Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...

  5. [转]C++实现系统服务暂停、停止、启动

    /* 名称:系统服务管理 语言:C++ 介绍:对Windows系统服务的状态获取,服务暂停,开启,停止操作代码 */ void CStartServiceDlg::OnBnClickedButton1 ...

  6. SimpleDateFormat的使用

    SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (date -> text).语法分析 (text -> date)和标准化. 下面是个小 ...

  7. HttpClient 请求WebApi

    HttpClient client = new HttpClient(); client.BaseAddress = new Uri(ConfigurationManager.AppSettings[ ...

  8. win32 清空ListBox所有内容

    Q:clear listbox hi i am working in VC++ 6 using Win32 App. .............tell me how to clear the lis ...

  9. Yii console 创建命令行应用

    大家都知道PHP的程序没有进程概念,而且生命周期极短,无法实现一些定时计划或者是计划任务,今天我们看看在YII框架中如何使用计划任务创建命令行应用. 1.在 console/controllers 文 ...

  10. 记录一下自己用到的python logging

    最近想把自己零零散散写的代码嵌成一个应用,要考虑到各方面的debug,把logging看了一下,把用到的记下来. 将日志打印到屏幕 import logging logging.debug(u'调试' ...