打印树有4个节点(简单森林)用于检查基准

mrs*_*eve 6 java tree benchmarking microbenchmark

我使用实施实验OOP语言和现在的基准垃圾收集存储性能基准测试.现在我想检查/打印下面的小深度基准(n = 2,3,4,..).

通过该buildTreeDepth方法生成树(具有4个子节点的林).代码如下:

import java.util.Arrays;

public final class StorageSimple {

    private int count;
    private int seed = 74755;

    public int randomNext() {
        seed = ((seed * 1309) + 13849) & 65535;
        return seed;
    }

    private Object buildTreeDepth(final int depth) {
        count++;
        if (depth == 1) {
            return new Object[randomNext() % 10 + 1];
        } else {
            Object[] arr = new Object[4];
            Arrays.setAll(arr, v -> buildTreeDepth(depth - 1));
            return arr;
        }
    }

    public Object benchmark() {
        count = 0;
        buildTreeDepth(7);
        return count;
    }

    public boolean verifyResult(final Object result) {
        return 5461 == (int) result;
    }


    public static void main(String[] args) {
        StorageSimple store = new StorageSimple();
        System.out.println("Result: " + store.verifyResult(store.benchmark()));
    }   
}
Run Code Online (Sandbox Code Playgroud)

是否有一种简单/直接的方式来打印buildTreeDepth生成的树?只是n = 3,4,5的短树.

Ton*_*ony 1

正如其他人已经建议的那样,您可以选择一些库来执行此操作。但是如果你只想在命令行中测试一个简单的算法,你可以执行以下操作,我在命令行中打印树时总是使用它(通过句柄编写,可能有一些错误。相信你可以得到这个BFS算法的工作原理) :

queue.add(root);
queue.add(empty);
int count = 1;
while (queue.size() != 1) {
    Node poll = queue.poll();
    if (poll == empty) {
        count = 1;
        queue.add(empty);
    }
    for (Node n : poll.getChildNodes()) {
        n.setNodeName(poll.getNodeName(), count++);
        queue.add(n);
    }
    System.out.println(poll.getNodeName());
}
Run Code Online (Sandbox Code Playgroud)

示例输出:

1
1-1 1-2 1-3 1-4
1-1-1 1-1-2 1-1-3 1-2-1 1-2-2 1-3-1 1-3-2 1-4-1
...
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您使用数组,这似乎更容易打印。