Title: CS
11321
2CS1321Introduction to Programming
- Georgia Institute of Technology
- College of Computing
- Lecture 11
- Sept 27th, 2001
- Fall Semester
3Where are We?
Section 14 of the book More Self-referential
Data Definitions http//www.htdp.org/2001-09-22/
Book/node73.htm
4Todays Menu
More Self-referential Data Structures Trees
5Up to this point
Weve been dealing with the fairly simple
self-referential data definition of a list.
6This has served us well
Weve modeled sets of data where it is only
necessary to have one reference to another
location in the set of data. For example, a list
of grocery items found on most college freshmens
shopping lists
7We didnt really need to know anything more than
another grocery item will come after the current
one if were not at the end of our list
8But what about more complicated situations?
Lets say you wanted to model data that
inherently had more than just one relationship
between items? Like a Family Tree
9The Family Tree
There are a couple of different ways to represent
a family tree. The method that were going to go
through will show an ancestor family tree. From
a given location in the family tree, we will be
able to easily access information about their
ancestors, but not their descendants. Which
means.
10In this particular family tree, we add a new
node to the tree every time a child is born.
We record certain information about the child
(name, year, eye color) as well as who the
parents are (mother, father).
11But if we stop to think about it, arent the
father and mother both children themselves?
So wouldnt they too have properties like name,
year, eye color and Father and Mother?
So we need a self-referencing data definition
that has two references within it!
12Lets develop a data definition
(define-struct child (father mother name bday
eyes)) A child is a structure
(make-child father mother name bday eyes)
where father and mother are child structures
name and eyes are symbols and bday is a number
Looks good, right?
13That almost works but
That data definition seems to workbut there are
problems. Lets say you have quite an extensive
family historygoes all the way back to your
great-great-great-great-great grandfather
Billy-Bob Bubba Jones born in the year 1810 to
Well, the records of that portion of your
family tree was destroyed in a tragic fireso you
dont actually know who your great (x6)
grandfather or grandmother actually was
14So we have to reflect that fact.
- Your father and mother values could be
non-existent. They could be empty. So we change
our data definition a little - A family-tree-node is either
- empty or
- (make-child father mother name bday eyes) where
father mother are family-tree-nodes, name and
eyes are symbols, and bday is a number
15 A child is a structure (make-child
father mother name bday eyes) where father and
mother are child structures name and eyes
are symbols and bday is a number
Note the difference in this abstract data
definition and the structure we were creating
before
- A family-tree-node is either
- empty or
- (make-child father mother name bday eyes) where
father and mother are family-tree-nodes, name and
eyes are symbols, and bday is a number
16 A child is a structure (make-child
father mother name bday eyes) where
father and mother are child structures name
and eye are symbols and bday is a number
before
Note the difference in this abstract data
definition and the structure we were creating
before
after
- A family-tree-node is either
- empty or
- (make-child father mother name bday eyes) where
father and mother are family-tree-nodes, name and
eyes are symbols, and bday is a number
17So what have we made?
Weve created a data structure that references
itself twice If we wanted to think about it
visually
18So what have we made?
Weve created a data structure that references
itself twice If we wanted to think about it
visually
Why the vertical line? It makes things easier to
visualize. Youll see
name
bday
eyes
father
mother
19Lets say we start off with information about you
empty
empty
20Lets say we start off with information about you
empty
empty
(make-child ??? ??? Bubba 1981 brown)
21Lets say we start off with information about you
empty
empty
(make-child empty empty Bubba 1981 brown empty
empty)
22Lets say we start off with information about you
Until we enter information about a particular
parent, well just start them off as empty
empty
empty
23As time goes on we add more information about our
family
empty
empty
empty
empty
24empty
empty
empty
empty
(make-child (make-child empty
empty PaBubba 1960 green)
(make-child empty empty MaBubba 1961 brown)
Bubba 1981
brown)
25(define Dave (make-child
(make-child empty empty Carl 1926 green)
(make-child empty empty Bettina 1926
green) Dave 1955
black))) Could be done like
this (define Carl (make-child empty empty Carl
1926 green)) (define Bettina (make-child empty
empty Bettina 1926 green)) (define Dave
(make-child Carl Bettina Dave 1955 black))
26And more information
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
27What about code?
Is this much more complex than what weve done
before? Lets see what a function template would
look like for this family tree Well derive a
fun-for-ftn template short for function-for-proce
ssing-family-tree-node.