樹形結(jié)構(gòu)
樹形結(jié)構(gòu)(tree)是比較常用的數(shù)據(jù)結(jié)構(gòu)了,MIDP中沒有它的身影,不然我就不用寫這篇文章了。
代碼如下:
這個(gè)類實(shí)現(xiàn)簡(jiǎn)單,沒有包含復(fù)雜的功能,僅僅用來(lái)做樹形數(shù)據(jù)的存儲(chǔ)還是不錯(cuò)的。比如游戲中用來(lái)管理場(chǎng)景,管理資源;應(yīng)用中用來(lái)作分類數(shù)據(jù)的表現(xiàn)等等。完全足以勝任。
使用方法如下:
上面的代碼創(chuàng)建了一棵完整的樹,當(dāng)然,您可以使用任何對(duì)象代替這里存儲(chǔ)的String對(duì)象。
還有一些方法,一看函數(shù)名大概都能明白,就不再嘮叨了。
遍歷的方法于上面創(chuàng)建樹的方法相似,總之,要注意當(dāng)前節(jié)點(diǎn)的位置,以免下次使用時(shí)處在錯(cuò)誤的位置。
有興趣的朋友可以擴(kuò)展一下遍歷方法。不過我覺得沒必要。因?yàn)镴2ME環(huán)境下更需要的是樹形結(jié)構(gòu),而不是強(qiáng)大的tree對(duì)象。
總之,我比較傾向于簡(jiǎn)單實(shí)現(xiàn),希望它不太讓人覺得簡(jiǎn)陋就好。從實(shí)用出發(fā),它還是能夠滿足大部分受限平臺(tái)的需求的。
代碼如下:
/**
*
* @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;
}
}
}
這個(gè)類實(shí)現(xiàn)簡(jiǎn)單,沒有包含復(fù)雜的功能,僅僅用來(lái)做樹形數(shù)據(jù)的存儲(chǔ)還是不錯(cuò)的。比如游戲中用來(lái)管理場(chǎng)景,管理資源;應(yīng)用中用來(lái)作分類數(shù)據(jù)的表現(xiàn)等等。完全足以勝任。
使用方法如下:
HTree tree = new HTree(”root”);//會(huì)自動(dòng)創(chuàng)建一個(gè)默認(rèn)的根節(jié)點(diǎn)
tree.addChild(”天才”);//在根節(jié)點(diǎn)添加新的節(jié)點(diǎn)
tree.addChild(”白癡”);
tree.goChild(0);//進(jìn)入到當(dāng)前節(jié)點(diǎn)的第一個(gè)節(jié)點(diǎn)(天才)。
tree.addChild(”天才1號(hào)”);//在當(dāng)前節(jié)點(diǎn)(天才)添加新的節(jié)點(diǎn)
tree.addChild(”天才2號(hào)”);
tree.goBack();//返回當(dāng)前節(jié)點(diǎn)(天才)的父節(jié)點(diǎn)(根)
tree.goChild(1);//進(jìn)入到當(dāng)前節(jié)點(diǎn)的第二個(gè)節(jié)點(diǎn)(白癡)。
tree.addChild(”白癡1號(hào)”);//在當(dāng)前節(jié)點(diǎn)(白癡)添加新的節(jié)點(diǎn)
tree.addChild(”白癡2號(hào)”);
tree.goRoot();//完成創(chuàng)建后將當(dāng)前節(jié)點(diǎn)設(shè)置為根節(jié)點(diǎn)。
…
tree.addChild(”天才”);//在根節(jié)點(diǎn)添加新的節(jié)點(diǎn)
tree.addChild(”白癡”);
tree.goChild(0);//進(jìn)入到當(dāng)前節(jié)點(diǎn)的第一個(gè)節(jié)點(diǎn)(天才)。
tree.addChild(”天才1號(hào)”);//在當(dāng)前節(jié)點(diǎn)(天才)添加新的節(jié)點(diǎn)
tree.addChild(”天才2號(hào)”);
tree.goBack();//返回當(dāng)前節(jié)點(diǎn)(天才)的父節(jié)點(diǎn)(根)
tree.goChild(1);//進(jìn)入到當(dāng)前節(jié)點(diǎn)的第二個(gè)節(jié)點(diǎn)(白癡)。
tree.addChild(”白癡1號(hào)”);//在當(dāng)前節(jié)點(diǎn)(白癡)添加新的節(jié)點(diǎn)
tree.addChild(”白癡2號(hào)”);
tree.goRoot();//完成創(chuàng)建后將當(dāng)前節(jié)點(diǎn)設(shè)置為根節(jié)點(diǎn)。
…
上面的代碼創(chuàng)建了一棵完整的樹,當(dāng)然,您可以使用任何對(duì)象代替這里存儲(chǔ)的String對(duì)象。
還有一些方法,一看函數(shù)名大概都能明白,就不再嘮叨了。
遍歷的方法于上面創(chuàng)建樹的方法相似,總之,要注意當(dāng)前節(jié)點(diǎn)的位置,以免下次使用時(shí)處在錯(cuò)誤的位置。
有興趣的朋友可以擴(kuò)展一下遍歷方法。不過我覺得沒必要。因?yàn)镴2ME環(huán)境下更需要的是樹形結(jié)構(gòu),而不是強(qiáng)大的tree對(duì)象。
總之,我比較傾向于簡(jiǎn)單實(shí)現(xiàn),希望它不太讓人覺得簡(jiǎn)陋就好。從實(shí)用出發(fā),它還是能夠滿足大部分受限平臺(tái)的需求的。
posted on 2008-08-15 14:51 LukeW 閱讀(199) 評(píng)論(0) 編輯 收藏 所屬分類: Tips, Tricks, Hints & Code