通过二叉树形式给一个数组排序
来源:互联网 发布:linux 监听端口 编辑:程序博客网 时间:2024/05/16 13:54
/*
*通过二叉树形式给一个数组排序
*1.创建对应的二叉树模型
*2.把数组以怎样的形式放入到模型里面
*3.把数组以怎样的形态给取出来
*4.获取数组的根节点
* 5.测试(把数组通过二叉树模型得到对应的二叉树类型,然后再取出来)
*/
public class zz {
public static void main(String[] args) {
Tree root = buildTree();
System.out.println(root.getNodes());
System.out.println(root.sum());
System.out.println("---------------------");
root.fTraverse();
System.out.println();
root.mTraverse();
System.out.println();
root.hTraverse();
System.out.println("---------------------");
int[] array={3,6,1,2,8};
Tree t = buildTree(array);
t.mTraverse();
}
public static Tree buildTree(){
Tree n1 = new Tree(10);
Tree n2 = new Tree(20);
Tree n3 = new Tree(30);
Tree n4 = new Tree(40);
Tree n5 = new Tree(50);
Tree n6 = new Tree(60);
Tree n7 = new Tree(70);
n5.setIchild(n7);
n3.setIchild(n5);
n3.setRchild(n6);
n2.setIchild(n4);
n1.setIchild(n2);
n1.setRchild(n3);
return n1;
}
//获取创建二叉树对应数组的根元素
public static Tree buildTree(int[] array){
Tree root = new Tree(array[0]);
for(int i=1;i<array.length;i++){
root.addNode(new Tree(array[i]));
}
return root;
}
}
//把一个数组放到二叉树的节点上,数组的第一个元素作为根节点,大于根节点的放在右子树,小于根节点的放在左子树以递归形式创建
class Tree{
public void addNode(Tree node){
if(node.date<this.date){
if(this.ichild==null){
this.ichild=node;
}else{
this.ichild.addNode(node);
}
}else{
if(this.rchild==null){
this.rchild=node;
}else{
this.rchild.addNode(node);
}
}
}
//后序遍历所有二叉树节点数组,先遍历左子树,再遍历右子树,再访问根节点
public void hTraverse(){
if(this.ichild!=null){
this.ichild.hTraverse();
}
if(this.rchild!=null){
this.rchild.hTraverse();
}
System.out.print("后序遍历"+this.date+"-");
}
//中序遍历所有二叉树节点数组,先遍历左子树,再访问根节点,再遍历右子树
public void mTraverse(){
if(this.ichild!=null){
this.ichild.mTraverse();
}
System.out.print("中序遍历"+this.date+"-");
if(this.rchild!=null){
this.rchild.mTraverse();
}
}
//先序遍历所有二叉树节点数组,先访问根节点,再遍历左子树,再遍历右子树
public void fTraverse(){
System.out.print("先序遍历"+this.date+"-");
if(this.ichild!=null){
this.ichild.fTraverse();
}
if(this.rchild!=null){
this.rchild.fTraverse();
}
}
//所有二叉树节点对应数组的总和
public int sum(){
return (this.ichild==null?0:this.ichild.sum())+(this.rchild==null?0:this.rchild.sum())+this.date;
}
//所有二叉树的节点总数
public int getNodes(){
return (this.ichild==null?0:this.ichild.getNodes())+(this.rchild==null?0:this.rchild.getNodes())+1;
}
private int date;
private Tree ichild;
private Tree rchild;
public Tree(int date){
this.date=date;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public Tree getIchild() {
return ichild;
}
public void setIchild(Tree ichild) {
this.ichild = ichild;
}
public Tree getRchild() {
return rchild;
}
public void setRchild(Tree rchild) {
this.rchild = rchild;
}
}
阅读全文
0 0
- 通过二叉树形式给一个数组排序
- 通过数组给您的文件排序
- 通过数组给您的文件排序
- 写一个js工具,作用是给一个数组排序
- 堆栈和通过一个数组还原二叉树
- js中通过<s:iterator>标签给一个数组赋值
- 判断一个数组是不是排序二叉树后序遍历
- 给数组排序
- PostgreSQL 给数组排序
- 给字符串数组排序
- 给数组随机排序
- 给数组进行排序
- 如何给二维数组排序
- php中给数组排序
- 通过反射将数组中的元素给一个对象中的各个字段赋值
- 详解数组实现隐式二叉堆及堆排序
- 将一个已排序的链表或数组转化成一棵平衡二叉树
- java通过数组描述二叉树
- Java byte数组与十六进制字符串互转
- Java泛型
- java引用粗解
- nagios监控平台
- SQL Server 2008 R2 Developer Edition图文安装教程
- 通过二叉树形式给一个数组排序
- RxjavaforServer
- vb.net SQL语句 太长用Append
- row_number() 函数
- (亿图绘制)软件升级调度流程
- bootstrap学习27---带有响应式特性的嵌入式内容
- 牛客网专项练习基础概念总结(一)
- 粒子群算法优化BP神经网络
- Corba的学习笔记(二)