implementing recursive tree