Professional Documents
Culture Documents
Trees
Trees
Trees
6/8/2002 2:15 PM
Trees
Make Money Fast!
Stock
Fraud
6/8/2002 2:15 PM
Ponzi
Scheme
Bank
Robbery
Trees
What is a Tree
Organization charts
File systems
Europe
Programming
environments
6/8/2002 2:15 PM
Asia
Canada
Trees
Tree ADT
integer size()
boolean isEmpty()
objectIterator elements()
positionIterator positions()
Accessor methods:
position root()
position parent(p)
positionIterator children(p)
6/8/2002 2:15 PM
subtree
J
Trees
Preorder Traversal
Trees
Tree Terminology
In computer science, a
ComputersRUs
tree is an abstract model
of a hierarchical
structure
Sales
Manufacturing
R&D
A tree consists of nodes
with a parent-child
relation
US
International
Laptops
Desktops
Applications:
6/8/2002 2:15 PM
Trees
Query methods:
boolean isInternal(p)
boolean isExternal(p)
boolean isRoot(p)
Update methods:
swapElements(p, q)
object replaceElement(p, o)
1. Motivations
9
2. Methods
1.1 Greed
1.2 Avidity
6/8/2002 2:15 PM
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
2.1 Stock
Fraud
Trees
2.2 Ponzi
Scheme
References
8
2.3 Bank
Robbery
6
Trees
6/8/2002 2:15 PM
Postorder Traversal
In a postorder traversal, a
node is visited after its
descendants
Application: compute space
used by files in a directory and
its subdirectories
9
Binary Tree
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
cs16/
homeworks/
todo.txt
1K
programs/
h1c.doc
3K
h1nc.doc
2K
DDR.java
10K
6/8/2002 2:15 PM
Stocks.java
25K
Trees
Trees
I
8
No
On expense account?
Yes
No
Yes
No
Starbucks
Spikes
Al Forno
Caf Paragon
6/8/2002 2:15 PM
Trees
10
BinaryTree ADT
Properties:
e = i + 1
n = 2e 1
h i
h (n 1)/2
h
e 2
h log2 e
h log2 (n + 1) 1
Trees
6/8/2002 2:15 PM
Yes
n number of nodes
e number of
external nodes
i number of internal
nodes
h height
Notation
6/8/2002 2:15 PM
Trees
6/8/2002 2:15 PM
arithmetic expressions
decision processes
searching
Decision Tree
Applications:
Robot.java
20K
11
Update methods
may be defined by
data structures
implementing the
BinaryTree ADT
position leftChild(p)
position rightChild(p)
position sibling(p)
6/8/2002 2:15 PM
Trees
12
Trees
6/8/2002 2:15 PM
Inorder Traversal
In an inorder traversal a
node is visited after its left
subtree and before its right
subtree
Application: draw a binary
tree
Algorithm inOrder(v)
if isInternal (v)
inOrder (leftChild (v))
visit(v)
if isInternal (v)
inOrder (rightChild (v))
2
4
3
Trees
13
Algorithm evalExpr(v)
if isExternal (v)
return v.element ()
else
x evalExpr(leftChild (v))
y evalExpr(rightChild (v))
operator stored at v
return x y
1
Trees
14
6/8/2002 2:15 PM
Trees
15
6/8/2002 2:15 PM
6/8/2002 2:15 PM
Specialization of a postorder
traversal
6/8/2002 2:15 PM
Algorithm printExpression(v)
if isInternal (v)
print(()
inOrder (leftChild (v))
print(v.element ())
if isInternal (v)
inOrder (rightChild (v))
print ())
Specialization of an inorder
traversal
Trees
16
Specializations of EulerTour
6/8/2002 2:15 PM
17
We show how to
specialize class
EulerTour to evaluate
an arithmetic
expression
Assumptions
6/8/2002 2:15 PM
Trees
18
Trees
6/8/2002 2:15 PM
Element
Parent node
Sequence of children
nodes
A node is represented
by an object storing
6/8/2002 2:15 PM
Tree interface
BinaryTree interface
extending Tree
Classes implementing Tree
and BinaryTree and
providing
Trees
19
expandExternal(v)
removeAboveExternal(w)
6/8/2002 2:15 PM
Trees
Trees
20
InspectableBinaryTree
InspectableTree
BinaryTree
Tree
InspectableTree
removeAboveExternal(w)
6/8/2002 2:15 PM
Constructors
Update methods
Print methods
Trees in JDSL
expandExternal(v)
Java Implementation
D
C
Element
Parent node
Left child node
Right child node
21
NodeBinaryTree
NodeTree
6/8/2002 2:15 PM
Tree
BinaryTree
Trees
22