JiveJdon Community Forums
在线246人 Home | 论坛 | 培训咨询 | 精华 | 查搜 | 注册 | 登陆 |
首页 » 论坛 » 项目工程开发经验谈
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表
???en_US.forumThreadNext.name??? 下一主题
这个主题共有 4 回复 / 1 页 [ ]  发表新帖子  回复该主题贴
zingers

发表文章: 178
注册时间: 2002年08月14日 16:11
给他发消息
关于一个树状结构的通用类 发表: 2003年05月29日 21:02 回复
因为在网上找不到,所以尝试自己写一下。
如果你有网络资源,请告诉我,谢谢。

import java.util.*;

public class TreeModule
{
TreeModule son ;//最左边的儿子节点
TreeModule bro ;//最左边的兄弟节点
TreeModule father;


public boolean hasSon()
{
if (son != null)
{
return true;
}
return false;
}

public boolean hasBro()
{
if (bro !=null )
{
return true;
}
return false;
}

public boolean hasFather()
{
if (this.father!=null)
{
return true;
}
return false;
}

public TreeModule getFather()
{
return this.father;
}

public TreeModule getSon()
{
return son;
}

public TreeModule getBro()
{
return bro;
}

public TreeModule getLastson()
{
TreeModule point = this.getSon();
while(point !=null && point.hasBro())
{
point = point.getBro();
}
return point;
}

public TreeModule getLastbro()
{
TreeModule point = this.getBro();
while (point != null && point.hasBro())
{
point = point.getBro();
}
return point;
}



public ArrayList getSons()
{
ArrayList list = new ArrayList();
TreeModule temp = null;
if (this.hasSon())
{
temp = this.getSon();
list.add(temp);
while (temp.hasBro())
{
temp = temp.getBro();
list.add(temp);
}
}
return list;
}



public ArrayList getBrothers()
{
ArrayList list = new ArrayList();
TreeModule temp = this;
while (temp.hasBro())
{
temp = temp.getBro();
list.add(temp);
}
return list;
}





public void addSon(TreeModule a)
{
TreeModule temp = this;
if (temp==null)
return;

if(!temp.hasSon())
{
temp.son = a;
}
else
{
temp=temp.getSon();
while (temp.hasBro())
{
temp=temp.getBro();
}

temp.bro=a;
}

a.father = this;
}


public void addSons(ArrayList list)
{
if (list.size()>=1)
{
for (int j=0;j<list.size() ;j++ )
{
this.addSon((TreeModule)list.get(j));
}
}
}



public void delSon(TreeModule son)
{
ArrayList list = getSons();
for (int i=0;i<list.size() ;i++)
{
if ( ((TreeModule)list.get(i)).equals(son) )
{
((TreeModule)list.get(i-1)).bro=(TreeModule)list.get(i+1);
ArrayList list2 = ((TreeModule)list.get(i)).getSons();
this.addSons(list2);
//list2.get(i) = null;
return;
}
}
}



public void clearSon(TreeModule son)
{
ArrayList list = getSons();
for (int i=0;i<list.size() ;i++ )
{
if ( ((TreeModule)list.get(i)).equals(son) )
{
((TreeModule)list.get(i-1)).bro=(TreeModule)list.get(i+1);
//((TreeModule)list.get(i)).deltree();
return;
}
}
}

public void deltree()
{
ArrayList list = getSons();

if(list.size()==0)
{
/*cant be compiled
this = null;
*/
return;
}

Iterator ite = list.iterator();
TreeModule temp = null;
while (ite.hasNext())
{
temp=(TreeModule)ite.next();
temp.deltree();
}


}

public TreeModule getTop()
{
TreeModule temp = this.father;
if (temp==null)
{
return this;
}
while (temp!=null)
{
temp = temp.father;
}
return temp;
}

}

我感觉有很多问题,特别是在clearSon时内存没清干净。
希望大家多提意见。
banq

发表文章: 9074
注册时间: 2002年08月03日 17:08
给他发消息
Re: 关于一个树状结构的通用类 发表: 2003年05月29日 21:03 回复
参考jive中LongTree
zingers

发表文章: 178
注册时间: 2002年08月14日 16:11
给他发消息
Re: 关于一个树状结构的通用类 发表: 2003年05月29日 21:07 回复
哈,banq,我是觉得LongTree有问题才这么做的。
它要求初始化时,知道容量大小,我这个不需要知道的。
我觉得用数组来模拟总归不通用。
unixboom

发表文章: 12
注册时间: 2003年01月06日 21:54
给他发消息
Re: 关于一个树状结构的通用类 发表: 2003年08月13日 17:18 回复
可以用swing中JTree
iceant

发表文章: 462
注册时间: 2002年10月13日 22:32
给他发消息
Re: 关于一个树状结构的通用类 发表: 2003年08月14日 17:49 回复
Swing 中有好像有 TreeModel 的缺省实现?
这个主题有 4 回复 / 1 页 [ ]
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表    返回页首  返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Spring Struts
查询本论坛内 回复超过的热门帖子
快速发表回复
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com

anti spam