PSU CS199 — Introduction to Computer Science

Homework 3: Ancestor Family Trees

Due Monday 3 August 2009

Language Level: Beginning Student
Teachpacks: none

This homework will let you gain experience with functions that manipulate complex recursive data-structures.

Preliminaries

Read Sections 14.1 and 14.2 from HTDP.

Your Assignment

Part I

Using the code in HTDP for family trees (or the code from class, if you prefer), do the following two exercises. As you work, follow the design recipe and the templates! This worksheet may help you.

(Exercise 14.1.4.)   Develop the function average-age. It consumes a family tree node and the current year. It produces the average age of all people in the family tree.  

(Exercise 14.1.5.)   Develop the function eye-colors, which consumes a family tree node and produces a list of all eye colors in the tree. An eye color may occur more than once in the list.

Part II

These exercises are based on the Binary Tree (BT) data structure described in Section 14.2 of HTDP.

(Exercise 14.2.1).   Draw the two trees

(make-node
  15
  'd
  false
  (make-node 24 'i false false))

  
(make-node
  15
  'd
  (make-node 87 'h false false)
  false)

in the manner of figure 38. Then develop contains-bt. The function consumes a number and a BT and determines whether the number occurs in the tree. 

(Exercise 14.2.2).   Develop search-bt. The function consumes a number n and a BT. If the tree contains a node structure whose soc field is n, the function produces the value of the pn field in that node. Otherwise, the function produces false.

Hint: Use contains-bt. Or, use boolean? to find out whether search-bt was successfully when applied to a subtree.

Hand in your work.

Put your names as a comment at the top of the definitions window. Save your file from DrScheme, attach it to an email message. In addition, put the names of both partners in the body of the email and in the name of the file. Submit your email to CS199Homework.

If you don't have an easy way of handing in your drawings (for Exercise 14.2.1) electronically, just hand in a piece of paper in class on Monday).

Acknowledgements

These Exercises are taken from HTDP.