Enumeration children() Returns the children of the receiver as an Enumeration. boolean getAllowsChildren() Returns true if the receiver allows children. TreeNode getChildAt(int childIndex) Returns the child TreeNode at index childIndex. int getChildCount() Returns the number of children TreeNodes the receiver contains. int getIndex(TreeNode node) Returns the index of node in the receivers children. TreeNode getParent() Returns the parent TreeNode of the receiver. boolean isLeaf() Returns true if the receiver is a leaf.
Vector hs = new Vector (); Vector returnChildNode(DefaultMutableTreeNode currNode){ hs.add(currNode); int childCount currNode.getChildCount(); for(int i=0;i<childCount;i++){ returnChildNode((DefaultMutableTreeNode)currNode.getChildAt(i)); } }
不是数据结构中的树!
问题是子树下面又有孙树,可能是一种递归的情况,有没有好用的代码?
宽度优先
Enumeration enumeration = currentNode.breadthFirstEnumeration();
深度优先
Enumeration enumeration = currentNode.depthFirstEnumeration();
使用这两个方法可以得到他的所有子孙节点
Returns the children of the receiver as an Enumeration.
boolean getAllowsChildren()
Returns true if the receiver allows children.
TreeNode getChildAt(int childIndex)
Returns the child TreeNode at index childIndex.
int getChildCount()
Returns the number of children TreeNodes the receiver contains.
int getIndex(TreeNode node)
Returns the index of node in the receivers children.
TreeNode getParent()
Returns the parent TreeNode of the receiver.
boolean isLeaf()
Returns true if the receiver is a leaf.
Vector returnChildNode(DefaultMutableTreeNode currNode){
hs.add(currNode);
int childCount currNode.getChildCount();
for(int i=0;i<childCount;i++){
returnChildNode((DefaultMutableTreeNode)currNode.getChildAt(i));
}
}