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

2

6

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.

Ombre answered 18/4, 2017 at 6:54 Comment(3)
PS: I want to print the tree so that I know for sure it is the same tree that I generated for my experimental language.Ombre
This question seems too broad as it stands. I wouldn't be surprised if there are 100 Java API's that can generate either a graphical or ASCII tree for you. Probable duplicate - How to draw a tree representing a graph of connected nodes?Grade
Can you add an example of what the output you're looking for would look like ?Arthritis
C
1

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.

Canvasback answered 29/4, 2017 at 4:15 Comment(0)
H
1

Instead of using object arrays, use a List implementation like ArrayList. For an improved better result subclass ArrayList to also hold a 'level' value and add indentation to the toString() method.

Hysteria answered 1/5, 2017 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.