CS - PowerPoint PPT Presentation

About This Presentation
Title:

CS

Description:

(define-struct child (father mother name bday eyes)) ;; A child is a structure: ... Your father and mother values could be non-existent. They could be empty. ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 28
Provided by: ccGa
Category:
Tags: father

less

Transcript and Presenter's Notes

Title: CS


1
1321
  • CS

2
CS1321Introduction 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
Todays Menu
More Self-referential Data Structures Trees
5
Up to this point
Weve been dealing with the fairly simple
self-referential data definition of a list.
6
This 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
7
We 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
8
But 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
9
The 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.
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, 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!
12
Lets 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 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
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
So what have we made?
Weve created a data structure that references
itself twice If we wanted to think about it
visually
18
So 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
19
Lets say we start off with information about you
empty
empty
20
Lets say we start off with information about you
empty
empty
(make-child ??? ??? Bubba 1981 brown)
21
Lets say we start off with information about you
empty
empty
(make-child empty empty Bubba 1981 brown empty
empty)
22
Lets 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
23
As time goes on we add more information about our
family
empty
empty
empty
empty
24
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 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.
Write a Comment
User Comments (0)
About PowerShow.com