吴旭晓个人博客 繁体中文 简体中文

首页| 日志 |JAVA |ASP |PHP |Android |IOS |ASP.NET |JavaScript |DIV+CSS |SEO |taobaoke |饼哥语录
繁体中文 简体中文

J2me Game开发技巧:树形结构实现

  树形结构(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对象。

  总之,我比较倾向于简单实现,希望它不太让人觉得简陋就好。从实用出发,它还是能够满足大部分受限平台的需求的。

作者:吴旭晓 | 来源:个人博客 | 点击量:1902 | 发布时间:2013-06-26
最新留言 | 返回上一页 | 返回首页

相关文章:

版权所有:吴旭晓个人博客 Copyright © 2013-2023 个人博客