关于一个树状结构的通用类

因为在网上找不到,所以尝试自己写一下。
如果你有网络资源,请告诉我,谢谢。

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时内存没清干净。
希望大家多提意见。

参考jive中LongTree

哈,banq,我是觉得LongTree有问题才这么做的。
它要求初始化时,知道容量大小,我这个不需要知道的。
我觉得用数组来模拟总归不通用。

可以用swing中JTree

Swing 中有好像有 TreeModel 的缺省实现?