树形结构

14 篇文章 0 订阅
14 篇文章 0 订阅
树形结构(tree)是比较常用的数据结构了,MIDP中没有它的身影,不然我就不用写这篇文章了。
代码如下:
/**
 *
 * 
@author  hunhun1981
 
*/
public   class  HTree {
 
 
private  HNode root;
 
 
private  HNode current;
 
 
private   int  currDepth;
 
 
private   int  maxDepth;
 
 
public  HTree(Object rootValue) {
  root 
=   new  HNode( null , rootValue);
  current 
=  root;
 }
 
 
public   void  goRoot() {
  current 
=  root;
  currDepth 
=   0 ;
 }
 
 
public   boolean  goChild( int  index) {
  
if  (current.childList  !=   null ) {
   
if  (current.childList.size()  >   0
     
&&  index  <  current.childList.size()) {
    current 
=  (HNode) current.childList.elementAt(index);
    currDepth
++ ;
    
if  (currDepth  >  maxDepth) {
     maxDepth 
=  currDepth;
    }
    
return   true ;
   }
  }
  
return   false ;
 }
 
 
public   void  goBack() {
  
if  (current.father  !=   null ) {
   current 
=  current.father;
   currDepth–;
  }
 }
 
 
public  Object getCurrent() {
  
return  current.value;
 }
 
 
public   int  getCurrentDepth() {
  
return  currDepth;
 }
 
 
public   int  getMaxDepth() {
  
return  maxDepth;
 }
 
 
public  Object[] getChilds() {
  
if  (current.childList  !=   null ) {
   
if  (current.childList.size()  >   0 ) {
    Object[] ret 
=   new  Object[current.childList.size()];
    
for  ( int  i  =   0 ; i  <  ret.length; i ++ ) {
     ret[i] 
=  ((HNode) current.childList.elementAt(i)).value;
    }
    
return  ret;
   }
  }
  
return   null ;
 }
 
 
public  Object getChild( int  index) {
  
if  (current.childList  !=   null ) {
   
if  (current.childList.size()  >   0
     
&&  index  <  current.childList.size()) {
    
return  ((HNode) current.childList.elementAt(index)).value;
   }
  }
  
return   null ;
 }
 
 
public   void  addChild(Object obj) {
  
if  (current.childList  ==   null ) {
   current.childList 
=   new  Vector();
  }
  current.childList.addElement(
new  HNode(current, obj));
 }
 
 
public   void  addChilds(Object[] objs) {
  
if  (current.childList  ==   null ) {
   current.childList 
=   new  Vector();
  }
  
for  ( int  i  =   0 ; i  <  objs.length; i ++ ) {
   current.childList.addElement(
new  HNode(current, objs[i]));
  }
 }
 
 
public   int  hasChild() {
  
if  (current.childList  ==   null   ||  current.childList.size()  <=   0 ) {
   
return   0 ;
  } 
else  {
   
return  current.childList.size();
  }
 }
 
 
private   class  HNode {
 
  
public  Vector childList;
 
  
public  HNode father;
 
  
public  Object value;
 
  
public  HNode(HNode father, Object value) {
   
this .value  =  value;
   
this .father  =  father;
   
this .childList  =   null ;
  }
 }
}


这个类实现简单,没有包含复杂的功能,仅仅用来做树形数据的存储还是不错的。比如游戏中用来管理场景,管理资源;应用中用来作分类数据的表现等等。完全足以胜任。
使用方法如下:
HTree tree  =   new  HTree(”root”); // 会自动创建一个默认的根节点
tree.addChild(”天才”); // 在根节点添加新的节点
tree.addChild(”白痴”);
tree.goChild(
0 ); // 进入到当前节点的第一个节点(天才)。
tree.addChild(”天才1号”); // 在当前节点(天才)添加新的节点
tree.addChild(”天才2号”);
tree.goBack();
// 返回当前节点(天才)的父节点(根)
tree.goChild( 1 ); // 进入到当前节点的第二个节点(白痴)。
tree.addChild(”白痴1号”); // 在当前节点(白痴)添加新的节点
tree.addChild(”白痴2号”);
tree.goRoot();
// 完成创建后将当前节点设置为根节点。

上面的代码创建了一棵完整的树,当然,您可以使用任何对象代替这里存储的String对象。
还有一些方法,一看函数名大概都能明白,就不再唠叨了。
遍历的方法于上面创建树的方法相似,总之,要注意当前节点的位置,以免下次使用时处在错误的位置。
有兴趣的朋友可以扩展一下遍历方法。不过我觉得没必要。因为J2ME环境下更需要的是树形结构,而不是强大的tree对象。

总之,我比较倾向于简单实现,希望它不太让人觉得简陋就好。从实用出发,它还是能够满足大部分受限平台的需求的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值