Optimal binary tree coded with Javascript