270 likes | 287 Views
1321. CS. CS1321: Introduction to Programming. Georgia Institute of Technology College of Computing Lecture 11 Sept 27th, 2001 Fall Semester. Where are We?. Section 14 of the book… “More Self-referential Data Definitions” http://www.htdp.org/2001-09-22/Book/node73.htm. Today’s Menu.
E N D
1321 CS
CS1321:Introduction to Programming Georgia Institute of Technology College of Computing Lecture 11 Sept 27th, 2001 Fall Semester
Where are We? Section 14 of the book… “More Self-referential Data Definitions” http://www.htdp.org/2001-09-22/Book/node73.htm
Today’s Menu More Self-referential Data Structures Trees
A list-of-numbers is either: • the empty list empty or • (cons n lon) where n is a number and lon is a list-of-numbers empty 2 3 1 Up to this point… We’ve been dealing with the fairly simple self-referential data definition of a list.
Sugar-laden “food” of choices empty non-alcoholic beverages No-Doz This has served us well… We’ve 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 freshmen’s shopping lists…
Sugar-laden “food” of choices empty non-alcoholic beverages No-Doz We didn’t really need to know anything more than another grocery item will come after the current one if we’re not at the end of our list…
But what about more complicated situations? Let’s say you wanted to model data that inherently had more than just one relationship between items? Like a Family Tree…
The Family Tree… There are a couple of different ways to represent a family tree. The method that we’re 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….
In 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).
But if we stop to think about it, aren’t the father and mother both children themselves? So wouldn’t 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!
Let’s 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?
That almost works but… That data definition seems to work…but there are problems. Let’s say you have quite an extensive family history…goes 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 fire…so you don’t actually know who your great (x6) grandfather or grandmother actually was…
So 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
;; 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
;; 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
name bday eyes father mother So what have we made? We’ve created a data structure that references itself twice… If we wanted to think about it visually…
So what have we made? We’ve 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. You’ll see… name bday eyes father mother
‘Bubba 1981 ‘brown father mother Let’s say we start off with information about you… empty empty
‘Bubba 1981 ‘brown father mother Let’s say we start off with information about you… empty empty (make-child ??? ??? ‘Bubba 1981 ‘brown)
‘Bubba 1981 ‘brown father mother Let’s say we start off with information about you… empty empty (make-child empty empty ‘Bubba 1981 ‘brown empty empty)
‘Bubba 1981 ‘brown father mother Let’s say we start off with information about you… Until we enter information about a particular parent, we’ll just start them off as empty… empty empty
‘Bubba ‘PaBubba ‘MaBubba 1981 1960 1961 ‘brown ‘green ‘brown As time goes on we add more information about our family… empty empty empty empty
‘Bubba ‘PaBubba ‘MaBubba 1981 1960 1961 ‘brown ‘green ‘brown empty empty empty empty (make-child (make-child empty empty ‘PaBubba 1960 ‘green) (make-child empty empty ‘MaBubba 1961 ‘brown) ‘Bubba 1981 ‘brown)
(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))
And more information empty empty empty empty empty empty empty empty empty empty empty empty
What about code? Is this much more complex than what we’ve done before? Let’s see what a function template would look like for this family tree… We’ll derive a fun-for-ftn template short for function-for-processing-family-tree-node….