Representing Structure and Behavior with Trees CS1316: Representing Structure and Behavior

advertisement
Representing Structure
and Behavior with Trees
CS1316: Representing
Structure and Behavior
Story


What we were just doing with LLNode
What linked lists are good for.
• Using linked lists to represent structure and
behavior

What trees are good for.
• Using trees to represent structure and behavior
• Examples
• Equations
• Matching
• Unification
Abstracting
LLNode
abstract LLNode
Knows next
Knows how to do all basic list
operations
CollectableNode

Knows just knows the part that
specializes LLNode for collectable
(sound-returning) nodes.
DrawableNode

Just specializes LLNode to represent
things that can draw with a turtle and
draw down their linked list with a turtle
(drawOn).
LLNode: The abstract definition
of what a linked list node is


Given LLNode, we can make anything
we want into a linked list.
Want a linked list of students?
• Subclass LLNode into StudentNode
• StudentNodes know names and ID numbers,
•
•
and know how to access and return these.
The linked list part is inherited from LLNode.
Every StudentNode knows next and knows
how to getNext, add, etc.
Why do people use linked lists?




Whenever you want dynamic size to the
list,
You may want the ordering to represent
something,
You want insertion and deletion to be
cheap and easy,
You are willing to make finding a
particular item slower.
Examples of Linked Lists





Order of layers in Visio or PowerPoint
Notes in a Phrase in JMusic
Video segments in non-linear video
editing.
Items in a toolbar.
Slides in a PowerPoint presentation.
But what are trees good for?

Trees represent hierarchical structure.
• When just representing ordering (linearity)
isn’t enough.

Trees can store operations (behavior),
as well as data.
• Linked lists can, too, but not as useful.
Examples of Trees



Representing how parts of music assemble to form a
whole.
Representing the elements of a scene.
•
Representing the inheritance relationships among
classes.
•




Scene graph
Class hierarchy
Files and directories on your hard disk.
Elements in an HTML page.
Organization chart
Political affiliations
Example Tree: Equation

It’s fairly easy to turn an
equation into a tree.
•
•


3+4*5
If you see an operation,
make a branch.
Otherwise, make a node.
The structure here
represents order of
operation.
Evaluating this tree is like
collecting() the sounds, but
collection involves
computation (behavior) at
the branches.
+
3
*
4
5
Example Tree: Taxonomies
(Keeping track of meaning)

Let’s say that you want to compare prices at
various websites for the same item.
•
•
•

At one site, they call it the “retail price”
At another, they call it the “customer’s cost”
How do you track that these are similar meanings?
It’s the same as knowing that a MoveBranch
is a kind of Branch is a kind of
DrawableNode!
Tree of Meanings
Here, the arrows mean
the same thing as in a
class hierarchy.
The thing below is a
specialization of the thing
above.
Artificial Intelligence (AI)
and Semantic Web
researchers really do
represent taxonomies in
just this way.
Price
Retail price
Customer
price
Customer
cost
Wholesale price
Business-tobusiness price
Algorithm for matching
1.
2.
3.
Traverse the tree to find phrase1.
Traverse the tree to find phrase2.
Do both have a common ancestor
(parent)?
 That’s the meaning in common!
4.
If not, need to extend the taxonomy.
Example tree: Unification


My cat ate a fat worm.
Sentence Diagrams
are trees.
Arrows don’t mean
the same things
here.
•
•
Some arrows are
saying instance-of
a category (like
“noun”)
Other arrows are
saying has-pieceswithin (like links
from “subject” and
“object”)
ate
verb
subject
object
noun
article
cat
noun
adjective
adjective
a
worm
My
fat
How do we search a collection
of sentence diagrams?




Imagine: You’re an expert with the
National Security Agency.
You have megabytes of captured
terrorist messages.
Using specialized Natural Language
Understanding (NLU) technology, you
have trees of all this text.
Now what do you do with it?
Compare trees


We can create a tree
describing the kind of
sentence that we want to
find.
We leave variables that can
be bound in the process of
the query.
•

Matching complex trees like
this is sometimes called
unification.
Not exactly the same word?
How do we determine if it’s
close enough? See
previous slides…
attack
verb
subject
noun
object
noun
<suspect>
<location in United States>
Trees in User Interfaces



Any user interface
is actually
composed of a
tree!
Windows hold
panes hold buttons
and text areas.
Next lecture…
window
pane
pane
button
text area
button
button
Download