// Binary Trees benchmark - recursive data structures
class Tree {
    constructor(left, right) {
        this.left = left;
        this.right = right;
    }
}

function make(depth) {
    if (depth === 0) return null;
    return new Tree(make(depth - 1), make(depth - 1));
}

function check(tree) {
    if (tree === null) return 1;
    return 1 + check(tree.left) + check(tree.right);
}

const minDepth = 4;
const maxDepth = 14;

// Stretch tree
const stretchDepth = maxDepth + 1;
const stretchTree = make(stretchDepth);
console.log(`stretch tree check: ${check(stretchTree)}`);

// Long lived tree
const longLivedTree = make(maxDepth);

// Iterate through depths
for (let depth = minDepth; depth <= maxDepth; depth += 2) {
    const iterations = 1 << (maxDepth - depth + 4);
    let sum = 0;
    for (let i = 0; i < iterations; i++) {
        sum += check(make(depth));
    }
    console.log(`${iterations} trees of depth ${depth} check: ${sum}`);
}

console.log(`long lived tree check: ${check(longLivedTree)}`);
