请用java写二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来

guoming1 2005-02-26 07:33:26
如题啊!!!!
...全文
476 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
icebluenet 2005-03-09
  • 打赏
  • 举报
回复
去找一本《数据结构》java描述的,图书馆里面好像很多
tim90 2005-03-09
  • 打赏
  • 举报
回复
下面是3个类和一个测试文件,放在一个包下
/**
* @author liuhui
*1,Component 是抽象组件
Tree 和Leaf 继承Component

private String name; //树或叶子的名称
addChild(Component leftChild,Component rightChild);
//给一个树上加上一个左孩子,一个右孩子
getName(){return name;}
getTreeInfo(){} //得到树或叶子的详细信息
getLength(); //得到树的高度

2,Tree 二叉树,一个左孩子,一个右孩子

*3,Leaf 是叶子节点
*4,Test 是测试节点
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public abstract class Component {
private String name;
public abstract Component addChild(Component leftChild,Component rightChild);
public String getName(){return name;}
public void getTreeInfo(){}
public abstract int getLength();
}

--------------------------------------
/**
* @author liuhui
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Leaf extends Component{
private String name;
private Component leaf=null;
public Leaf(String name) {
this.name=name;
}
public Component addChild(Component leftChild,Component rightChild){
return this;
}
public String getName(){
return name;
}
public int getLength() {
return 1;
}
public static void main(String[] args) {
}
}
-------------------------------------
/**
* @author liuhui
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Tree extends Component {
private String name;
private Component leftChild;
private Component rightChild;
public Tree(String name,Component leftChild,Component rightChild) {
this.name=name;
this.leftChild=leftChild;
this.rightChild=rightChild;
}
public Tree(String name) {
this.name=name;
this.leftChild=null;
this.rightChild=null;
}
public Component addChild(Component leftChild,Component rightChild){
this.leftChild=leftChild;
this.rightChild=rightChild;
return this;
}
public String getName(){
return name;
}
public void getTreeInfo()
//得到树或叶子的详细信息
//先打印自己的名字,再遍例左孩子,再遍例右孩子
//如果左孩子或右孩子是树,递归调用
{
System.out.println(" this tree's name is "+getName());
if(this.leftChild instanceof Leaf)
{
System.out.println(getName()+"'s left child is "+this.leftChild.getName()+",it is a Leaf");
}
if(this.leftChild instanceof Tree){
System.out.println(getName()+"'s left child is "+this.leftChild.getName()+",it is a Tree");
this.leftChild.getTreeInfo();
}
if(this.leftChild==null)
{
System.out.println(getName()+"'s left child is a null");
}
if(this.rightChild instanceof Leaf)
{
System.out.println(getName()+"'s right child is "+this.rightChild.getName()+",it is a Leaf");
}
if(this.rightChild instanceof Tree) {
System.out.println(getName()+"'s right child is "+this.rightChild.getName()+",it is a Tree");
this.rightChild.getTreeInfo();
}
if(this.rightChild==null)
{
System.out.println(getName()+"'s right child is a null");
}
//System.out.println(getName()+"'s 高度 是 "+getLength());
}
public int getLength() {
//比较左孩子或右孩子的高度,谁大,+1 返回
// 空孩子的处理
if(this.leftChild==null) {
if(this.rightChild==null)
return 1;
else
return this.rightChild.getLength()+1;
}
else {
if(this.rightChild==null) {
return this.leftChild.getLength()+1;
}
else {
if((this.leftChild.getLength())>=(this.rightChild.getLength()))
return this.leftChild.getLength()+1;
else
return this.rightChild.getLength()+1;
}
}
}
public static void main(String[] args) {
}
}
-----------------------
/**
* @author liuhui
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Test {

public Test() {
}
public static void main(String[] args) {
Component tree=new Tree("luopeng");
Component leaf_child=new Leaf("luopeng1");
Component right_child=new Leaf("luopeng2");
tree=tree.addChild(leaf_child,right_child);
//tree=tree.addRightChild(right_child);
tree.getTreeInfo();
Component tree1=new Tree("luopeng2");
tree1.addChild(tree,leaf_child);
tree1.getTreeInfo();
Component tree2=new Tree("luopeng3");
tree2.addChild(tree,null);
tree2.getTreeInfo();
Component tree4=new Tree("luopeng4");
tree4.addChild(null,tree);
tree4.getTreeInfo();
System.out.println(tree4.getName()+"的高度是 "+tree4.getLength());
}
}

mxp2002 2005-03-07
  • 打赏
  • 举报
回复
关注中:)
内容概要:本文详细介绍了Anaconda的下载、安装与配置方法(2025最新版)。Anaconda是一个开源的Python/R数据科学集成开发平台,预装了1500多个科学计算库,并提供conda包管理和环境管理功能。文章首先列出了系统要求,接着分别讲述了适用于不同操作系统的下载方式,包括官方下载和国内镜像下载。然后,具体讲解了Windows、macOS和Linux三种操作系统的安装步骤,以及环境变量的手动配置方法。此外,还提供了验证安装是否成功的命令和配置国内镜像源的方法,以提高下载速度。最后,列出了一些常用conda命令和常见问题的解决方案。 适合人群:从事数据科学、机器学习领域的研究人员和开发者,特别是需要频繁使用Python科学计算库的用户。 使用场景及目标:①帮助用户快速搭建Python开发环境,尤其是需要多个Python版本共存或隔离环境的情况下;②解决因网络原因导致的下载速度慢的问题;③提供详细的安装指南,确保安装过程顺利进行;④指导用户正确配置环境变量,避免常见的安装后无法使用的错误。 阅读建议:由于Anaconda涉及多平台安装和配置,建议读者根据自己的操作系统选择相应的章节重点阅读,并严格按照步骤操作。对于初次使用者,建议先从简单的安装入手,再逐步学习环境管理和包管理的相关命令。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧