Generating a balanced binary tree