Mutually Referential Structures - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Mutually Referential Structures

Description:

... family tree ... does this suggest for the family tree node? Family Tree Nodes ... Collections that refer to each other. Example family tree ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 14
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Mutually Referential Structures


1
Mutually Referential Structures
2
Outline
  • Prerequisites
  • Structures
  • Complex Lists
  • Recursion
  • Objectives
  • Collections that refer to each other
  • Example family tree

3
Background
  • When we built the ancestral tree from the
    children upwards, it was easy because any child
    has only two parents
  • However, when we build it downwards, parents may
    have any number of children
  • What kind of structure does this suggest for the
    family tree node?

4
Family Tree Nodes
person structure (refers to a people-list)
people-list a list of person structures
5
Mutual References
  • The person structure contains a people-list
  • The items in the people-list are other person
    structures
  • This circular definition process is called Mutual
    Reference
  • Good abstraction suggests that there ought to be
    two functions
  • One function (parent-offspring ) determines the
    number of offspring a parent has
  • One function (list-offspring ) determines the
    number of offspring on a list

6
Example Counting Offspring
Carl, 1926, 1978
Bettina, 1928, 1986
CarlBettina
Fred, 1949, alive
Eva, 1951, alive
Dave, 1948, 1997
Adam, 1946, alive
FredEva
Gustav, 1985, alive
7
Program Design
  • (parent-offspring person) conditions
  • There is no children list -
  • There is a children list -
  • (list-offspring a-list) conditions
  • List is empty -
  • List not empty -

return 0 return (list-offspring )
return 0 return (parent-offspring (first) )
(list-offspring (rest))
8
(No Transcript)
9
Code Used - 1
  • (define-struct person (name dob dod children))
  • children is a list of 0 or more person nodes
  • dod (date of death) will be -1 if they are
    still alive
  • parent-offspring person - number
  • (define (parent-offspring here)
  • (cond (empty? (person-children here)) 1
  • else ( 1 (list-offspring
    (person-children here)))))
  • list-offspring people-list - number
  • (define (list-offspring here)
  • (cond (empty? here) 0
  • else ( (parent-offspring (first here))
  • (list-offspring (rest here)))))

10
Code Used - 2
  • (define Gustav (make-person 'Gustav 1985 -1
    empty))
  • (define FredEva (list Gustav))
  • (define Fred (make-person 'Fred 1949 -1
    FredEva))
  • (define Eva (make-person 'Eva 1951 -1 FredEva))
  • (define Adam (make-person 'Adam 1946 -1 empty))
  • (define Dave (make-person 'Dave 1948 1997 empty))
  • (define CarlBettina (list Adam Dave Eva))
  • (define Carl (make-person 'Carl 1926 1978
    CarlBettina))
  • (define Bettina (make-person 'Bettina 1928 1986
    CarlBettina))

11
Questions?
12
Summary
  • You should now know
  • Collections that refer to each other
  • Example family tree

13
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com