Optimizations in tree generation