4-node print tree (plain forest) for benchmarking

I implemented an experimental OOP language and now collected garbage collection using Storage Tests . Now I want to check / print the next test for shallow depths (n = 2, 3, 4, ..).

A tree (a forest with 4 subnode) is generated by the method buildTreeDepth. The code is as follows:

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()));
    }   
}

Is there a slightly simple / direct way to print the tree generated by buildTreeDepth? Just short trees n = 3, 4, 5.

+6
source share
2 answers

, lib . , , , ( , , ). , , 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());
}

:

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
...

, . โ€‹โ€‹

+1

List, โ€‹โ€‹ ArrayList. ArrayList "level" toString().

+1

Source: https://habr.com/ru/post/1016597/


All Articles