adapted tree data structure from literateprograms.org