(define-struct person (name year eye mother father)) ;; a TreeNode is one of ;; false ;; no more data ;; (make-person String Natural String TreeNode TreeNode) ;; interp: ;; false means no more data in that part of the tree ;; for a person, name is the person's name ;; year is the year of birth ;; eye is the eye color ;; mother and father are the left and right subtrees (define MaryTree (make-person "Mary" 1980 "blue" (make-person "Ann" 1960 "green" false (make-person "Fred" 1936 "blue" false false)) (make-person "Joe" 1960 "blue" false false))) ;;; fcn-for-treenode: TreeNode -> ;;; ;(define (fcn-for-treenode atn) ; (cond [(false? atn) ] ; [(person? atn) (person-name atn) ; (person-year atn) ; (person-eye atn) ; (fcn-for-treenode (person-mother atn)) ; (fcn-for-treenode (person-father atn))])) ;; count-blue-eyed: TreeNode -> Natural ;; produces the number of blue-eyed people in the tree (define (count-blue-eyed atn) (cond [(false? atn) 0 ] [(person? atn) (if (string=? "blue" (person-eye atn)) (+ 1 (count-blue-eyed (person-mother atn)) (count-blue-eyed (person-father atn))) (+ (count-blue-eyed (person-mother atn)) (count-blue-eyed (person-father atn))))])) (check-expect (count-blue-eyed false) 0) (check-expect (count-blue-eyed MaryTree) 3) ;; gather-green-eyed: TreeNode -> ListOfString ;; produces a list of the names of all green-eyed people in the tree (define (gather-green-eyed atn) (cond [(false? atn) empty ] [(person? atn) (if (string=? "green" (person-eye atn)) (append (list (person-name atn)) (gather-green-eyed (person-mother atn)) (gather-green-eyed (person-father atn))) (append (gather-green-eyed (person-mother atn)) (gather-green-eyed (person-father atn))))])) (check-expect (gather-green-eyed false) empty) (check-expect (gather-green-eyed MaryTree) (list "Ann")) ;; update-year: TreeNode String Natural -> TreeNode ;; produces a tree in which the year of birth of the named person has ;; been changed to the given year (assume that names in the tree are unique) (define (update-year atn name new-year) (cond [(false? atn) false ] [(person? atn) (if (string=? name (person-name atn)) (make-person (person-name atn) new-year (person-eye atn) (person-mother atn) (person-father atn)) (make-person (person-name atn) (person-year atn) (person-eye atn) (update-year (person-mother atn) name new-year) (update-year (person-father atn) name new-year)))])) (check-expect (update-year false "Ann" 1958) false) (check-expect (update-year MaryTree "Ann" 1958) (make-person "Mary" 1980 "blue" (make-person "Ann" 1958 "green" false (make-person "Fred" 1936 "blue" false false)) (make-person "Joe" 1960 "blue" false false)))