Tree generation code (C++)