English 中文(简体)
通过树木寻找面积
原标题:Iteratively traverse through tree to find size

我需要找到使用迭代算法的树中元素的数量,但我发现,从概念上讲,很难写法。

我的做法是,从深层开始,访问儿童节点,然后看这些儿童的孩子们,等等。

这是一部为小树效劳的《第一号法典》,但由于我需要为每一层次的深度增加一个部分,所以它没有真正的解决办法:

// Start the counter at 1 because the root node counts
int size = 1;

for(ITree child1 : root) {
    size++;
    for(ITree child2 : child1) {
        size++;
        for(ITree child3 : child2) {
            size++;
            for(ITree child4 : child3) {
                size++;
                for(ITree child5 : child4) {
                    size++;
                }
            }
        }
    }
}
return size;
最佳回答

从概念上讲,保留一栏(LinkedList等)。 每一名儿童(现在,你们的孩子们)都增加了 st。 在最后空洞之前,继续穿着.。

这只经过了检测,但这应该完全是你所期待的。 I m 仅使用java.io.File,而不是你的“ITree”,因为我可以汇编下列内容:

int sizeOfTree(File root){
    // Start the counter at 1 because the root node counts

    int size = 1;

    LinkedList<File> stack = new LinkedList<File>();
    stack.add(root);

    while(!stack.isEmpty()){
        File f = stack.remove();
        for(File child : f.listFiles()){
            size++;
            stack.add(child);
        }
    }

    return size;
}
问题回答

www.un.org/Depts/DGACM/index_spanish.htm 采用回收数据结构

几乎不可能在理论上篡改数据结构,像一个有点的树木一样,这是因为这些物体的“hide”基本数据要素。

www.un.org/Depts/DGACM/index_spanish.htm 使用不同的数据结构

所有树木都可以储存/执行线性阵列数据结构,在这些结构中,指数可以采用指数性数学计算:

For example, a tree [0, 1, 2, 3, null, 4,null] would describe a tree with 0 at the root, where 0 had direct children 1 and 2. And then 1 has left child "3", and 2 has left child "4".

因此,如果你以这种方式储存树木,元素的数量自然是阵列中非核元素的数量。

更简单地说: 在一个线性结构中储存树木,你可以知道任何特定时间的长度,而无需进行任何计算。

你的任务的关键词是重复。 树木是一种传统的休养结构,因此,你应撰写休养方法,接受根基的节点,计算这个节点的大小,然后呼吁所有儿童。 这里是伪装法:

public int getSize(ITree root) {
    return getSize(root, 0);
}

private int getSize(ITree node, int size) {
    size++;
    for(ITree child : node.children()) {
        size += getSize(child, size)
    }
    return size;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签