二叉树java有什么用?用java实现二叉树
老铁们,大家好,相信还有很多朋友对于二叉树java有什么用和用java实现二叉树的相关问题不太懂,没关系,今天就由我来为大家分享分享二叉树java有什么用以及用java实现二叉树的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!
用java实现二叉树
我有很多个(假设10万个)数据要保存起来,以后还需要从保存的这些数据中检索是否存在某
个数据,(我想说出二叉树的好处,该怎么说呢?那就是说别人的缺点),假如存在数组中,
那么,碰巧要找的数字位于99999那个地方,那查找的速度将很慢,因为要从第1个依次往
后取,取出来后进行比较。平衡二叉树(构建平衡二叉树需要先排序,我们这里就不作考虑
了)可以很好地解决这个问题,但二叉树的遍历(前序,中序,后序)效率要比数组低很多,
public class Node{
public int value;
public Node left;
public Node right;
public void store(intvalue)
right.value=value;
}
else
{
right.store(value);
}
}
}
public boolean find(intvalue)
{
System.out.println("happen"+this.value);
if(value==this.value)
{
return true;
}
else if(value>this.value)
{
if(right==null)returnfalse;
return right.find(value);
}else
{
if(left==null)returnfalse;
return left.find(value);
}
}
public void preList()
{
System.out.print(this.value+",");
if(left!=null)left.preList();
if(right!=null) right.preList();
}
public void middleList()
{
if(left!=null)left.preList();
System.out.print(this.value+",");
if(right!=null)right.preList();
}
public void afterList()
{
if(left!=null)left.preList();
if(right!=null)right.preList();
System.out.print(this.value+",");
}
public static voidmain(String [] args)
{
int [] data=new int[20];
for(inti=0;i<data.length;i++)
{
data[i]=(int)(Math.random()*100)+ 1;
System.out.print(data[i]+",");
}
System.out.println();
Node root= new Node();
root.value= data[0];
for(inti=1;i<data.length;i++)
{
root.store(data[i]);
}
root.find(data[19]);
root.preList();
System.out.println();
root.middleList();
System.out.println();
root.afterList();
}
}
java中的遍历是什么意思
首先解释迭代。
迭代简单的理解,重文字上可以才分为迭(叠)加,代入(数)
是利用计算机高速、可从重复性高的特点进行计算的模式
迭代的最简单应用就是,把四维整型数组,中的内容全部输出。那就用四层循环慢慢取吧。
每次循环做的事情基本上是一件事,无外乎就是角标自增,然后取数。
再说遍历。
遍历很好理解,通过某种方式,不论是重头到尾,还是用Hash算法,
反正是从头到尾把数据结构(链表、数组、树、图)所有的节点都访问一遍,就叫遍历。
像刚才,四维数组取数,就是一个遍历的过程,
简单的使用迭代的方式,从第一个元素一直遍历(取)到最后一个元素。
稍微复杂的还有遍历二叉树,遍历欧拉图等。都用相应的算法。
java实现二叉树的问题
/**
*二叉树测试二叉树顺序存储在treeLine中,递归前序创建二叉树。另外还有能
*够前序、中序、后序、按层遍历二叉树的方法以及一个返回遍历结果asString的
*方法。
*/
public class BitTree{
public static Node2 root;
public static String asString;
//事先存入的数组,符号#表示二叉树结束。
public static final char[] treeLine={'a','b','c','d','e','f','g','','','j','','','i','#'};
//用于标志二叉树节点在数组中的存储位置,以便在创建二叉树时能够找到节点对应的数据。
static int index;
//构造函数
public BitTree(){
System.out.print("测试二叉树的顺序表示为:");
System.out.println(treeLine);
this.index= 0;
root= this.setup(root);
}
//创建二叉树的递归程序
private Node2 setup(Node2 current){
if(index>= treeLine.length) return current;
if(treeLine[index]=='#') return current;
if(treeLine[index]=='') return current;
current= new Node2(treeLine[index]);
index= index* 2+ 1;
current.left= setup(current.left);
index++;
current.right= setup(current.right);
index= index/ 2- 1;
return current;
}
//二叉树是否为空。
public boolean isEmpty(){
if(root== null) return true;
return false;
}
//返回遍历二叉树所得到的字符串。
public String toString(int type){
if(type== 0){
asString="前序遍历:\t";
this.front(root);
}
if(type== 1){
asString="中序遍历:\t";
this.middle(root);
}
if(type== 2){
asString="后序遍历:\t";
this.rear(root);
}
if(type== 3){
asString="按层遍历:\t";
this.level(root);
}
return asString;
}
//前序遍历二叉树的循环算法,每到一个结点先输出,再压栈,然后访问它的左子树,
//出栈,访问其右子树,然后该次循环结束。
private void front(Node2 current){
StackL stack= new StackL((Object)current);
do{
if(current== null){
current=(Node2)stack.pop();
current= current.right;
} else{
asString+= current.ch;
current= current.left;
}
if(!(current== null)) stack.push((Object)current);
} while(!(stack.isEmpty()));
}
//中序遍历二叉树
private void middle(Node2 current){
if(current== null) return;
middle(current.left);
asString+= current.ch;
middle(current.right);
}
//后序遍历二叉树的递归算法
private void rear(Node2 current){
if(current== null) return;
rear(current.left);
rear(current.right);
asString+= current.ch;
}
}
/**
*二叉树所使用的节点类。包括一个值域两个链域
*/
public class Node2{
char ch;
Node2 left;
Node2 right;
//构造函数
public Node2(char c){
this.ch= c;
this.left= null;
this.right= null;
}
//设置节点的值
public void setChar(char c){
this.ch= c;
}
//返回节点的值
public char getChar(){
return ch;
}
//设置节点的左孩子
public void setLeft(Node2 left){
this.left= left;
}
//设置节点的右孩子
public void setRight(Node2 right){
this.right= right;
}
//如果是叶节点返回true
public boolean isLeaf(){
if((this.left== null)&&(this.right== null)) return true;
return false;
}
}
一个作业题,里面有你要的东西。
主函数自己写吧。当然其它地方也有要改的。
好了,文章到此结束,希望可以帮助到大家。