working on tree generation function