package net.com.tree;
public class BinaryTree {
public static void main(String[] args){
BinaryTree binaryTree=new BinaryTree();
int[]array={8,3,10,9,1,5};
TreeNode treeNode=binaryTree.bulidSortTree(array);
binaryTree.printTreeInorder(treeNode);
}
public void createTree(TreeNode treeNode,int[] array){
for(int i=1;i<array.length;i++){
bulidTreeSty(treeNode,array[i]);
}
}
public TreeNode bulidSortTree(int[] array){
TreeNode treeNode=new TreeNode(array[0]);
createTree(treeNode, array);
return treeNode;
}
private void bulidTreeSty(TreeNode treeNode,int data){
if(data<=treeNode.data){
if(treeNode.left==null){
treeNode.setLeft(new TreeNode(data));
}else{
bulidTreeSty(treeNode.getLeft(), data);
}
}else{
if(treeNode.right==null){
treeNode.setRight(new TreeNode(data));
}else{
bulidTreeSty(treeNode.getRight(), data);
}
}
}
public void printTreeInorder(TreeNode treeNode){//中序遍历二叉树
if(treeNode!=null){
printTreeInorder(treeNode.left);
System.out.println(treeNode.data);
printTreeInorder(treeNode.right);
}
}
}
class TreeNode{
TreeNode left;//左子树
TreeNode right;//右子树
int data;
public TreeNode(){
this.left=null;
this.right=null;
this.data=0;
}
public TreeNode(int data){
this.left=null;
this.right=null;
this.data=data;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
分享到:
相关推荐
二叉树排序二叉树排序二叉树排序二叉树排序二叉树排序二叉树排序二叉树排序
一、目的 用二叉树排序 二、实验内容 二叉树排序 三、实验使用环境 C++
运用xml相关技术,实现二叉树的排序。先输入一组数字,排序之后插入到数据库,最后通过xml导出。
CPU:T6670 2.20GHZ 内存:2G 系统:WIN7 用VB调用VC编写的二叉树排序DLL排序,10万个数排序只需要0.3秒,100万个数排序只需要5.3秒左右
对一原有数组进行二叉树排序,并对其进行中序遍历。然后当我们输入一个数时,算出比较的次数,并打印出来
本程序使用的是二叉排序树以及二叉树的中序遍历的知识,程序段较短,并且比较容易理解。
自己做的数据结构课程设计,二叉树的,内含源程序、报告、演讲PPT,适合数据结构课程设计,如有运行问题可联系我(内含我的联系方式)。
python编写 实现 快速排序 和 二叉树排序 并对比速度
堆排序 二叉树排序 关键路径 Dijkstra算法,C语言,已实现
二叉树排序 采用中序遍历 按从大到小的方式输出结果
python实现的二叉树排序
二叉树排序算法及 demo
二叉树排序,000000000000 数据结构。
VC++2012编程演练数据结构《23》二叉树排序
用二叉树先序遍历算法创建一组数据构成的二叉树排序,然后用二叉树中序遍历算法实现数据排序输出。
VC++2012编程演练数据结构《21》二叉树排序