樹形結構
樹形結構(tree)是比較常用的數據結構了,MIDP中沒有它的身影,不然我就不用寫這篇文章了。
代碼如下:
這個類實現簡單,沒有包含復雜的功能,僅僅用來做樹形數據的存儲還是不錯的。比如游戲中用來管理場景,管理資源;應用中用來作分類數據的表現等等。完全足以勝任。
使用方法如下:
上面的代碼創建了一棵完整的樹,當然,您可以使用任何對象代替這里存儲的String對象。
還有一些方法,一看函數名大概都能明白,就不再嘮叨了。
遍歷的方法于上面創建樹的方法相似,總之,要注意當前節點的位置,以免下次使用時處在錯誤的位置。
有興趣的朋友可以擴展一下遍歷方法。不過我覺得沒必要。因為J2ME環境下更需要的是樹形結構,而不是強大的tree對象。
總之,我比較傾向于簡單實現,希望它不太讓人覺得簡陋就好。從實用出發,它還是能夠滿足大部分受限平臺的需求的。
代碼如下:
/**
*
* @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;
}
}
}
*
* @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();//完成創建后將當前節點設置為根節點。
…
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對象。
總之,我比較傾向于簡單實現,希望它不太讓人覺得簡陋就好。從實用出發,它還是能夠滿足大部分受限平臺的需求的。
posted on 2008-08-15 14:51 LukeW 閱讀(201) 評論(0) 編輯 收藏 所屬分類: Tips, Tricks, Hints & Code