Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Tree with 4 nodes (simple forest) for checking a benchmark

I implemented an experimental OOP language and now benchmark garbage collection using a Storage benchmark. Now I want to check/print the following benchmark for small depths (n=2, 3, 4,..).

The tree (forest with 4 subnode) is generated by the buildTreeDepth method. 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 somewhat simple/straight forward way to print the tree generated by buildTreeDepth? Just the short trees of n=3, 4, 5.

like image 626
mrsteve Avatar asked Apr 18 '17 06:04

mrsteve


1 Answers

As other has already suggested, you may choose some lib to do so. But if you just want a simple algo to test in command line, you may do the following, which I always use when printing tree in command line (write by handle, may have some bug. Believe you can get what this BFS algo works):

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

Sample output:

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

And in your case you use array, which seems even easier to print.

like image 192
Tony Avatar answered Oct 01 '22 13:10

Tony