1 / 27

1321

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.

aherndon
Download Presentation

1321

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1321 CS

  2. CS1321:Introduction to Programming Georgia Institute of Technology College of Computing Lecture 11 Sept 27th, 2001 Fall Semester

  3. Where are We? Section 14 of the book… “More Self-referential Data Definitions” http://www.htdp.org/2001-09-22/Book/node73.htm

  4. Today’s Menu More Self-referential Data Structures Trees

  5. 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.

  6. 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…

  7. 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…

  8. 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…

  9. 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….

  10. 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).

  11. 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!

  12. 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?

  13. 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…

  14. 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

  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

  17. 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…

  18. 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

  19. ‘Bubba 1981 ‘brown father mother Let’s say we start off with information about you… empty empty

  20. ‘Bubba 1981 ‘brown father mother Let’s say we start off with information about you… empty empty (make-child ??? ??? ‘Bubba 1981 ‘brown)

  21. ‘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)

  22. ‘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

  23. ‘Bubba ‘PaBubba ‘MaBubba 1981 1960 1961 ‘brown ‘green ‘brown As time goes on we add more information about our family… empty empty empty empty

  24. ‘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)

  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))

  26. And more information empty empty empty empty empty empty empty empty empty empty empty empty

  27. 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….

More Related