Add function to extract S from tree