用java实现二叉树排序 如题,烦请留下源码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JAVA实现二叉树遍历算法这篇文章就不错,一般都是用递归实现的,这篇文章中的算法里还给出了非递归的,LZ可以参考下 public class BinaryTreeTest { public static void main(String args[]) { BinaryTreeTest b=new BinaryTreeTest(); int data[]={12,11,34,45,67,89,56,43,22,98}; BinaryTree root =new BinaryTree(data[0]); System.out.print("二叉树的中的数据: "); for(int i=1;i<data.length;i++) { root.insertTree(root,data[i]); System.out.print(data[i-1]+";"); } System.out.println(data[data.length-1]); int key=Integer.parseInt(args[0]); if(b.searchkey(root,key)) { System.out.println("找到了:"+key); } else { System.out.println("没有找到:"+key); } } public boolean searchkey(BinaryTree root, int key) { boolean bl=false; if(root==null) { bl=false; return bl; } else if(root.data==key) { bl=true; return bl; } else if(key>=root.data) { return searchkey(root.rightpoiter,key); } return searchkey(root.leftpoiter,key); } } class BinaryTree { int data; BinaryTree leftpoiter; BinaryTree rightpoiter; BinaryTree(int data) { this.data=data; leftpoiter=null; rightpoiter=null; } public void insertTree(BinaryTree root, int data) { if(data>=root.data) { if(root.rightpoiter==null) { root.rightpoiter=new BinaryTree(data); } else { insertTree(root.rightpoiter,data); } } else { if(root.leftpoiter==null) { root.leftpoiter=new BinaryTree(data); } else { insertTree(root.leftpoiter,data); } } } } //end Java创建csv文件,并向csv文件中写入数据 数组拆分 先学EJB还是框架(struts,spring,hibernate)? 关于java中的Servlet的问题 初学者的疑问!!! JDBC对数据库操作 Struts标签配置遇到了问题,请兄弟们帮忙解决一下,谢谢啦! 问个Struts问题 运用类反射机制简化Struts应用程序的开发 struts.xml文件中result能不能配置类似target之类的东西? 学习Java必须学HTML吗?Java EE的话必须学这个吗? struts2 国际化问题,求高手帮忙! 在线等!!!
{
public static void main(String args[])
{
BinaryTreeTest b=new BinaryTreeTest();
int data[]={12,11,34,45,67,89,56,43,22,98};
BinaryTree root =new BinaryTree(data[0]);
System.out.print("二叉树的中的数据: ");
for(int i=1;i<data.length;i++) {
root.insertTree(root,data[i]);
System.out.print(data[i-1]+";");
}
System.out.println(data[data.length-1]);
int key=Integer.parseInt(args[0]);
if(b.searchkey(root,key))
{
System.out.println("找到了:"+key);
}
else
{
System.out.println("没有找到:"+key);
}
}
public boolean searchkey(BinaryTree root, int key)
{
boolean bl=false;
if(root==null)
{
bl=false;
return bl;
}
else if(root.data==key)
{
bl=true;
return bl;
}
else if(key>=root.data)
{
return searchkey(root.rightpoiter,key);
}
return searchkey(root.leftpoiter,key);
}
}
class BinaryTree
{
int data;
BinaryTree leftpoiter;
BinaryTree rightpoiter;
BinaryTree(int data)
{
this.data=data;
leftpoiter=null;
rightpoiter=null;
}
public void insertTree(BinaryTree root, int data)
{
if(data>=root.data)
{
if(root.rightpoiter==null)
{
root.rightpoiter=new BinaryTree(data);
}
else
{
insertTree(root.rightpoiter,data);
}
}
else
{
if(root.leftpoiter==null)
{
root.leftpoiter=new BinaryTree(data);
}
else
{
insertTree(root.leftpoiter,data);
}
}
}
}
//end