-working code for Tree construction