Building tree information from database - using recursive function