2.15 Trees and Hierarchy
An n-ary tree can
get a little hairy sometimes:
Just follow the steps
2.15.1 Objectives
that the data definition for a descendant family tree (a variable-width tree) requires two data definitions that refer to each other (mutually recursive data definitions).
write the data definition for a descendant family tree.
write functions over descendant family trees.
2.15.2 Exercises (due 2011/11/09 08:45:00)
Note: Functions over descendant family trees must work correctly on both the tree part AND the list part.
Recall that INFINITY is a double that represents infinity. It requires #include <math.h> to appear at the top of the program.
2. Develop the function countProperDescendents which consumes a parent and produces the number of descendents, not including the parent.
3. Develop the function generationDescendents which consumes a parent and a generation number and produces a list of all information about descendents in that generation.
4. Develop the function truncate which consumes a parent and a generation number and produces a parent that does not have any descendents in that generation (and thus, obviously, subsequent generations.)
2.15.3 Optional Exercises (due 2011/11/14 08:45:00)
5. Write a function countGenerations which counts the number of generations in a descendant family tree.
6. Generalize howFarRemoved so it takes an eye color argument and does not always check for blue-eye descendents.
2.15.4 Notes
These notes are primarily for my sake, but I don’t see any reason to hide them from you.
family trees were ancestor based |
|
hard to learn about children |
|
let's switch the arrows |
|
but now, people can have multiple children... |
|
parent is info, list of children |
|
count-blue-eyed : parent -> number |
|
has-old-and-blue? : parent number -> boolean [has a blue eyed descedent born before a given data] |
|
count-descendents : parent -> number |
|
descendents : parent -> ListOfInfo |
|
averageAge : parent -> number |
|
truncateTree : parent number -> parent [removes everyone after a generation] |
|
advanced-ages : parent -> parent [update ages by 1] |