CS1321: Introduction to Programming - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

CS1321: Introduction to Programming

Description:

A new dynamic structure: Trees. List was our main dynamic data structure... From a given person in the family tree, we will be able to easily access ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 36
Provided by: ccGa
Category:

less

Transcript and Presenter's Notes

Title: CS1321: Introduction to Programming


1
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Module 11
  • More Self-Referential Data Structures Ancestral
    Trees

2
Where are We?
Chapter 14 of the book More Self-referential
Data Definitions http//www.htdp.org/2002-05-09/
Book/curriculum-Z-H-19.html
3
Introducing Trees
  • Ancestor trees
  • Binary search trees

4
Background
  • We know structures
  • Heterogeneous collections of data
  • Specified using (define-struct )
  • Instantiated using (make- )
  • Accessed using (- )
  • We know lists
  • (usually) Homogeneous collections of data
  • Specification list-of-something is
  • Either empty
  • Or (cons list-of-something)
  • Instantiated using (list ) or (cons )
  • Manipulated recursively using (first ) and
    (rest )

5
More Background
  • Our choices so far appear to be
  • Structures
  • fixed size,
  • name the fields,
  • direct access to all fields
  • Lists
  • dynamically sized,
  • contain anything you cons onto them,
  • access individual pieces of data recursively from
    the front.
  • A new dynamic structure Trees

6
List was our main dynamic data structure
Weve been dealing with the fairly simple
self-referential data definition of a list.
7
This has served us well
Weve modeled sets of data where it is only
necessary to have one reference to the next
location in the set of data. For example, a list
of grocery items found on most college freshmens
shopping lists
8
We know the current grocery item and have a link
to the next grocery item if were not at the end
of our list
9
But what about more complicated situations?
Say you want to model data that inherently has
more than one relationship between items? Like
a Family Tree
10
The Family Tree
There are many different ways to represent a
family tree. The style we will show is an
ancestor family tree. From a given person in the
family tree, we will be able to easily access
information about the persons ancestors, but not
their descendants. Which means.
11
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, date of birth dob, date of death dod)
as well as who the parents are (mother, father).
12
But if we stop to think about it, arent the
father and mother both children themselves?
So wouldnt they too have properties like name,
dob, dod and father and mother?
So we need a data definition that contains two
references one to the father and one to the
mother
13
Lets develop a data definition
(define-struct child (father mother name dob
dod)) A child is a structure
(make-child father mother name dob dod) where
father and mother are child structures name
is a symbol and dob and dod are numbers
Looks good, right?
14
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 were destroyed in a tragic fire you
dont actually know who your great6 grandfather
or grandmother actually is
15
So we have to reflect that fact.
Your father and mother values could be
non-existent. They could be false. So we change
our data definition a little
  • A family-tree-node is either
  • false or
  • (make-child father mother name dob dod) where
    father mother are family-tree-nodes, name is a
    symbol, and dob and dod are numbers

16
(define-struct child (father mother name dob
dod)) A child is a structure
(make-child father mother name dob dod) where
father and mother are child structures name
is a symbol and dob and dod are numbers
Note the difference in this abstract data
definition and the structure we created before
A family-tree-node is either 1) false
or 2) (make-child father mother name dob
dod) where father and mother
are family-tree-nodes, name
is a symbol, and dob and dod
are numbers
17
(define-struct child (father mother name dob
dod)) A child is a structure
(make-child father mother name dob dod) where
father and mother are child structures name
is a symbol and dob and dod are numbers
before
Note the difference in this abstract data
definition and the structure we created before
A family-tree-node is either 1) false
or 2) (make-child father mother name dob
dod) where father and mother
are family-tree-nodes, name
is a symbol, and dob and dod
are numbers
after
18
So what have we made?
A data structure that references itself twice If
we wanted to think about it visually
19
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
dob
dod
father
mother
20
Lets say we start off with information about you
false
false
21
Lets say we start off with information about you
false
false
(make-child ??? ??? Bubba 1981 ????)
22
Lets say we start off with information about you
false
false
(make-child false false Bubba 1981 -1)
23
Lets say we start off with information about you
Until we enter information about a particular
parent, well just start them off as false
false
false
24
As time goes on we add more information about our
family
false
false
false
false
25
false
false
false
false
(make-child (make-child false
false PaBubba 1960 1993)
(make-child false false MaBubba 1961 -1)
Bubba 1981
-1)
26
(define Dave (make-child
(make-child false false Carl 1926 1982)
(make-child false false Bettina 1926 1992)
Dave 1955
-1))) Could be done like this (define Carl
(make-child false false Carl 1926 1982)) (define
Bettina (make-child false false Bettina 1926
1992)) (define Dave (make-child Carl Bettina
Dave 1955 -1))
27
And more information
false
false
false
false
false
false
false
false
false
false
false
false
28
What about code?
Is this much more complex than what weve done
before? Lets see what a function template would
look like for a family tree Well derive a
process-family-tree-node template
29
Observations
  • We made some choices
  • to indicate unknown parents
  • we choose false
  • makes for an odd looking question(not false) on
    the cond (youll see)
  • to indicate the no date of death
  • we choose -1
  • to help with defining test data
  • can use a (define ) for each person
  • Hint helps to draw a picture first

30
Template for Manipulating Structures
(define (process-family-tree-node node) (cond
(not node) check if node is false
else do what you need to do using
(process-family-tree-node (child-father node))
(process-family-tree-node (child-mother
node)) (child-name node)
(child-dob node) (child-dod node)
))

Use a recursive call for each self-reference
31
Manipulating Structures
node reminds us that we are moving recursively
through the tree
  • Example counting ancestors
  • ancestors child - number
  • (define (ancestors node)
  • (cond (not node) answer when not there
  • else do what you need to do
    using
  • (ancestors (child-father node))
  • (ancestors (child-mother node))
  • (child-name node)
  • (child-dob node)
  • (child-dod node) ))

32
Self-referencing Structure ExampleFamily Tree
false
false
false
false
false
false
Carl, 1926, 1978
Bettina, 1928, 1986
Fred, 1949, alive
Eva, 1951, alive
Dave, 1948, 1997
Adam, 1946, alive
33
Code Used
  • (define-struct child (father mother name dob
    dod))
  • father and mother may be either another child
    structure or false
  • dod (date of death) will be -1 if they are
    still alive
  • ancestors child - number
  • (define (ancestors node)
  • (cond (not node) 0
  • else ( 1
  • (ancestors (child-father node))
  • (ancestors (child-mother
    node)))))
  • (define Carl (make-child false false 'Carl 1926
    1978))
  • (define Bettina (make-child false false 'Bettina
    1928 1986))
  • (define Adam (make-child Carl Bettina 'Adam 1946
    -1))
  • (define Dave (make-child Carl Bettina 'Dave 1948
    1997))
  • (define Eva (make-child Carl Bettina 'Eva 1951
    -1))
  • (define Fred (make-child false false 'Fred 1949
    -1))
  • (define Gustav (make-child Fred Eva 'Gustav 1985
    -1))

34
Exercises
  • How would you change (ancestors ) to count only
    living ancestors?
  • How would you change (ancestors ) to prevent it
    from counting the child in question?

35
Questions?
Write a Comment
User Comments (0)
About PowerShow.com