The Computer Language
23.03 Benchmarks Game

binary-trees Java #3 program

source code

/* The Computer Language Benchmarks Game
 * https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
 *
 * contributed by Jarkko Miettinen
 * modified by Daryl Griffith
 * *reset*
*/

public class binarytrees {

    public static void main(String[] args) {
        int maxDepth;
        
        {
            int n = 0;
            
            if (args.length > 0) {
                n = Integer.parseInt(args[0]);
            }
            maxDepth = (6 > n) ? 6 : n;
        }
        {
            int stretchDepth = maxDepth + 1;

            System.out.println("stretch tree of depth " + stretchDepth + 
		"\t check: " + checkTree(createTree(stretchDepth)));
        }
        trees(maxDepth);
    }
    
    public static void trees(int maxDepth) {
        TreeNode longLastingNode = createTree(maxDepth);
        int depth = 4;

        do {
            int iterations = 16 << (maxDepth - depth);

            loops(iterations, depth);
            depth += 2;
        } while (depth <= maxDepth);
        System.out.println("long lived tree of depth " + maxDepth
		 + "\t check: " + checkTree(longLastingNode));
    }
    
    public static void loops(int iterations, int depth) {
        int check = 0;
        int item = 0;

        do {
            check += checkTree(createTree(depth));
            item++;
        } while (item < iterations);
        System.out.println(iterations + "\t trees of depth " +
		depth + "\t check: " + check);
    }
    
    public static TreeNode createTree(int depth) {
        TreeNode node = new TreeNode();

        if (depth > 0) {
            depth--;
            node.left = createTree(depth);
            node.right = createTree(depth);
        }
        return node;
    }
    
    public static int checkTree(TreeNode node) {
        if (node.left == null) {
            return 1;
        }
        return checkTree(node.left) + checkTree(node.right) + 1;
    }

    public static class TreeNode {

        private int item;
        private TreeNode left, right;
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
openjdk 20 2023-03-21
OpenJDK Runtime Environment
(build 20+36-2344)
OpenJDK 64-Bit Server VM
(build build 20+36-2344
mixed mode, sharing)


Thu, 23 Mar 2023 18:18:45 GMT

MAKE:
mv binarytrees.java-3.java binarytrees.java
/opt/src/jdk-20/bin/javac -d . -cp . binarytrees.java

1.50s to complete and log all make actions

COMMAND LINE:
/opt/src/jdk-20/bin/java  -cp . binarytrees 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303