tree construction almost implemented