简体   繁体   English

Java 树形数据结构

[英]Java Tree data structure

when we do node.getChildCount(), where node is the one of tree node,in that case the method will only return the child count, but it will not consider the child's child (eg- grand child)...so is there any predefined method, for get the child as well as grandchild count....??当我们执行 node.getChildCount() 时,其中 node 是树节点之一,在这种情况下,该方法将只返回子计数,但它不会考虑孩子的孩子(例如,孙子)......所以存在任何预定义的方法,用于获取孩子和孙子的数量......?? or any other way to get the count?或任何其他方式来获得计数?

Enumeration e = node.depthFirstEnumeration();
int count = 0;
while(e.hasMoreElements()) {
    count++;
    e.nextElement();
}

count now holds number of nodes of the subtree rooted at node. count 现在保存以节点为根的子树的节点数。 You might want to subtract 1.您可能想减去 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM