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 ...
随机推荐
- A - Next_permutation
首先介绍一下next_permutation函数的用途! 按照STL文档的描述,next_permutation函数将按字母表顺序生成给定序列的下一个较大的排列,直到整个序列为降序为止. prev_p ...
- inline-block布局方式
刚研究了一下inline-block布局方式 inline-block布局方式是一种比float浮动更优的一种布局方式. 一个超简单的demo html: <!doctype html> ...
- php 字符串
<?php /* * 字符串输出 * echo() 输出多个或多个字符串 * print() 输出字符串 * printf()格式化输出字符串 * 字符串截取 * substr() 对字符进行指 ...
- microwindows Win32 API demo
初次使用microwindows,资料有限,我也是费了很多功夫才明白.所以记录下来,好帮助那些爱学习的童鞋,另外请大虾们多多指教. 什么是microwindows,什么作用,等背景介绍我就不多说了,因 ...
- 定制样式插入到ueditor
AngularJs定制样式插入到ueditor中的问题总结 总结一下自己给编辑器定制样式的过程中所遇到的问题,主要是编辑器的二次开发接口,以及用angular定制样式,问题不少,终于在**的帮助下,完 ...
- nginx请求体读取
上节说到nginx核心本身不会主动读取请求体,这个工作是交给请求处理阶段的模块来做,但是nginx核心提供了ngx_http_read_client_request_body()接口来读取请求体,另外 ...
- C# 几种常见的排序方法
1.冒泡排序 //冒泡排序 public void BubbleSort(int[] list) { int i, j, temp; bool done = false; j = ; while (( ...
- 浅谈38K红外发射接受编码
之前做接触过一次红外遥控器,现在有空想用简单的话来聊一聊,下面有错误的地方欢迎改正指出:1:红外的概念不聊,那是一种物理存在.以下聊38K红外发射接收,主要讲可编程的红外编码.2:红外遥控 红外遥控首 ...
- 四大图像库:OpenCV/FreeImage/CImg/CxImage
1.对OpenCV 的印象:功能十分的强大,而且支持目前先进的图像处理技术,体系十分完善,操作手册很详细,手册首先给大家补计算机视觉的知识,几乎涵盖了近10年内的主流算法: 然后将图像格式和矩阵运算, ...
- Java报表开发组件DynamicReports
DynamicReports 是一个基于 JasperReports 进行扩展的 Java 报表库,可用它来快速创建报表而无需可视化报表设计工具. From : http://www.oschina ...