Comp 200 Lecture 12
Family Tree Lab

Mon 11 Feb 2002

Family Tree Lab


You will need to copy the Scheme FamTree Definitions from the Course Reference Library into your DrScheme definitions window before writing these functions.
You will write functions dealing with FamTrees.

Copy this definition into the definitions window as well as the FamTree definitions, so you can test your code!

(define the-simpsons
  (make-child 'bart 
            (make-child 'marge 
                        (make-child 'jackie (make-unknown) (make-unknown) 1926)
                        (make-unknown)
                        1958)
            (make-child 'homer 
                        (make-child 'abraham (make-unknown) (make-unknown) 1903)
                        (make-child 'mona    (make-unknown) (make-unknown) 1929)
                        1959)
            1983))

Write the following; don't peek at the solutions until you have answered the suggested qeustions to yourself!
Warning: You might not need to use all the code that the template provides.
  1. ;; size: FamTree --> number
    ;; Return how many (non-unknown) children ft contains.
    ;;
    (define (size ft)
       (if (unknown? ft)
           ...
           ... (size (child-ma ft)) ... (size (child-pa ft)) ... (child-name ft) ... ))
    
    What type of thing is (child-ma ft) (list, number, FamTree, symbol, ...?)
    Yep -- it's a FamTree. And what can we do with FamTrees? Yep -- call size on them, as indicated
    Now: What type of thing is (size (child-ma ft)) (list, number, FamTree, symbol, ...?)
    Use the pieces indicated (size of mother, father and the name) to determine the size of the whole thing. (Hint: the name doesn't matter, for this particular problem.)
    (How would this code change, if we wanted the size including unknowns?)
  2. ;; related-to-abraham? : FamTree --> boolean
    ;; Is 'abraham the name of anybody in the FamTree "ft"?
    ;;
    (define (related-to-abraham? ft)
       (if (unknown? ft)
           ...
           ..(related-to-abraham? (child-ma ft))..(related-to-abraham? (child-pa ft))..(child-name ft)..(child-birthyear ft).. ))
    
    Ask yourself the same questions as before, when filling in the parts.
  3. We call the name at the bottom of the tree a root, and the unknowns at the top leaves. Note that leaves are at different heights: Relative to 'bart, marge and homer are at height 1, homer's parent abraham is at height 2, and abraham's parents are at height 3.
    New concept: the height of a FamTree is the height of the highest leaf in the tree.
    Something to think about: if you have a person whose mother's family tree has height 17, and their fahter's familty tree has height 5, what is the height of that person's family tree, relative to that person?
    ;; (height ft) : FamTree --> boolean
    ;; Return the height of ft.
    ;;
    (define (height ft)
       (if (unknown? ft)
           ...
           ..(height (child-ma ft))..(height (child-pa ft))..(child-name ft)..(child-birthyear ft).. ))
    
    Ask yourself the same questions as before, when filling in the parts.
  4. If you finish the three questions above, here's a couple quick easy ones, if you use one as a helper for another:
    ;; earliest-birthyear: FamTree --> number
    ;; Return the earliest birthyear of any child in ft.
    ;;
    (define (earliest-birthyear ft)
       (if (unknown? ft)
           ...
           ..(earliest-birthyear (child-ma ft))..(earliest-birthyear (child-pa ft))..(child-name ft)..(child-birthyear ft)..))
    
    ;; oldest-age : FamTree number --> number
    ;; Return the age of the oldest person in ft, 
    ;; given the current year is this-year.
    ;; (We want to know what anniversary of the earliest birth we have.
    ;; Note that our choice of representing data doesn't let us know
    ;; whether or not somebody is still alive!)
    ;;
    (define (oldest-age ft this-year)
       . . . )
    
    Ask yourself the same questions as before, when filling in the parts.
  5. Finished already? Still Hungry for more? Here goes:

    Topiaries: What is a tree which is tall, but has few nodes?
    What is a tree which is not so tall, but has many nodes?
    Can you write functions to create these trees? E.g.,
    ;;  ivy : number --> famTree
    ;;  Return a tree with height "ht",
    ;;  but it's a stringy tree, with as few nodes as possible.
    ;;
    (define (ivy ht)
        . . . )
    
    
    ;;  shrubbery : number --> famTree
    ;;  Return a tree with height "ht",
    ;;  but it's a bushy tree, with as many nodes as possible.
    ;;
    (define (shrubbery ht)
        . . . )
    
    
    
    (Be sure to have some examples written down!) In writing these, we have the glitch that we need to provide a name/birthyear for each child. No prob -- we can just use the same name and birthyear for each (like a colony of mold).

Once you've written the functions, you can take a look at my solutions.