社区
Java SE
帖子详情
请用java写二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来
guoming1
2005-02-26 07:33:26
如题啊!!!!
...全文
471
3
打赏
收藏
请用java写二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来
如题啊!!!!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
关注中:)
先
序
遍历二
叉树
的非递归
算法
程
序
编
写
先
序
遍历二
叉树
的非递归
算法
程
序
,要求: (1)以二叉链表建立二
叉树
。 (2)输出遍历的结点
序
列。 (3)有实例验算。
建立二
叉树
,并输出二
叉树
的
先
序
,中
序
和后
序
遍历
序
列,以及二
叉树
的叶子数
二
叉树
可执行代码,用了就知道 。 二
叉树
的遍历、线索及应用( 用递归或非递归的方法都可以) [问题描述] 建立二
叉树
,并输出二
叉树
的
先
序
,中
序
和后
序
遍历
序
列,以及二
叉树
的叶子数。 [基本要求] 要求根据读取的元素建立二
叉树
,能输出各种遍历。 [
实现
提示] 可通过输入带空格的前
序
序
列建立二叉链表。
图解
Java
数据
结构和
算法
教程内容:本教程是使用
Java
来讲解
数据
结构和
算法
,考虑到
数据
结构和
算法
较难,授课采用图解加
算法
游戏的
方式
。内容包括: 稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀...
遍历二
叉树
(
java
实现
)
用
java
实现
二
叉树
遍历 包括
先
序
,中
序
,后
序
后续是自己
写
的
算法
可以运行
剑指offer
算法
实现
java
版——面试题19二
叉树
的镜像
分析:所谓“镜像”就是从镜子里看到的样子。我们可以画一棵二
叉树
,然后画出该二
叉树
的镜像。画完图之后我们会发现,所谓“二
叉树
的镜像”就是把二
叉树
中所有子树的左孩子和右孩子进行交换。因此需要遍历二
叉树
所有的结点,在遍历的同时交换非叶子结点的左右子树。遍历我们可以使用
先
序
遍历,首
先
判断当前根结点是否为叶子结点,若非叶子结点,则交换左右孩子,然后再分别对左右孩子进行相同的操作。
Java SE
62,615
社区成员
307,318
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章