Document 239048

Sequences
11/12/2007 2:12 PM
What is a Tree
In computer science, a
tree is an abstract model
Computers”R”Us
of a hierarchical
structure
A tree consists of nodes
Sales
Manufacturing
R&D
with a parent-child
relation (at most one
parent!)
US
International
Laptops
Desktops
Applications:
Trees
Make Money Fast!
Stock
Fraud
Organization charts
File systems
Europe
Programming
environments
„
Bank
Robbery
Ponzi
Scheme
„
„
Trees
© 2004 Goodrich, Tamassia
1
Subtree: tree consisting of
a node and its
descendants
„
„
„
B
C
D
G
„
H
„
subtree
J
1. Motivations
3
4
© 2004 Goodrich, Tamassia
integer size()
boolean isEmpty()
Iterator iterator()
Iterator positions()
„
Update method:
6
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
position root()
position parent(p)
Iterator children(p)
Trees
© 2004 Goodrich, Tamassia
Trees
4
In a postorder traversal, a
node is visited after its
descendants
Application: compute space
used by files in a directory and
its subdirectories
9
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
cs16/
3
9
7
2.1 Stock
Fraud
object replace (p, o)
„
Additional update methods
may be defined by data
structures implementing the
Tree ADT
Postorder Traversal
2. Methods
1.2 Avidity
„
K
5
3
„
boolean isInternal(p)
boolean isExternal(p)
boolean isRoot(p)
„
Make Money Fast!
2
1.1 Greed
„
Query methods:
Accessor methods:
F
Preorder Traversal
1
We use positions to
abstract nodes
Generic methods:
A
Trees
A traversal visits the nodes of a
tree in a systematic manner
In a preorder traversal, a node is
visited before its descendants
Application: print a structured
document
2
Tree ADT
Root: node without parent (A)
Internal node: node with at least
one child (A, B, C, F)
External node (a.k.a. leaf ): node
without children (E, I, J, K, G, H, D)
Ancestors of a node: parent,
grandparent, grand-grandparent,
etc.
Depth of a node: number of
ancestors (not counting itself)
E
Height of a tree: maximum depth
of any node (3)
Descendant of a node: child,
I
grandchild, grand-grandchild, etc.
© 2004 Goodrich, Tamassia
Canada
Trees
© 2004 Goodrich, Tamassia
Tree Terminology
Asia
2.2 Ponzi
Scheme
References
1
2
2.3 Bank
Robbery
h1c.doc
3K
h1nc.doc
2K
© 2004 Goodrich, Tamassia
todo.txt
1K
programs/
8
5
8
7
homeworks/
4
5
DDR.java
10K
Trees
Stocks.java
25K
6
Robot.java
20K
6
1
Sequences
11/12/2007 2:12 PM
Binary Trees
Arithmetic Expression Tree
„
„
„
B
a tree consisting of a single node, or
a tree whose root has an ordered
pair of children, each of which is a
root of a binary tree
„
„
Example: arithmetic expression tree for the
expression (2 × (a − 1) + (3 × b))
+
D
E
×
F
7
The BinaryTree ADT
extends the Tree
ADT, i.e., it inherits
all the methods of
the Tree ADT
Additional methods:
internal nodes: questions with yes/no answer
external nodes: decisions
Want a fast meal?
No
How about coffee?
On expense account?
„
„
Yes
No
Yes
No
Starbucks
Spike’s
Al Forno
Café Paragon
Trees
9
Inorder Traversal
In an inorder traversal a
node is visited after its left
subtree and before its right
subtree
Application: draw a binary
tree
4
3
8
„
7
Trees
10
Print Arithmetic Expressions
Specialization of an inorder
traversal
„
„
„
print operand or operator
when visiting node
print “(“ before traversing left
subtree
print “)“ after traversing right
subtree
+
×
9
×
−
2
5
a
Trees
Update methods
may be defined by
data structures
implementing the
BinaryTree ADT
position left(p)
position right(p)
boolean hasLeft(p)
boolean hasRight(p)
© 2004 Goodrich, Tamassia
8
2
© 2004 Goodrich, Tamassia
„
Algorithm inOrder(v)
if hasLeft (v)
inOrder (left (v))
visit(v)
if hasRight (v)
inOrder (right (v))
x(v) = inorder rank of v
y(v) = depth of v
6
1
Trees
BinaryTree ADT
Yes
„
b
1
© 2004 Goodrich, Tamassia
Example: dining decision
„
3
a
Trees
© 2004 Goodrich, Tamassia
−
I
Binary tree associated with a decision process
„
×
2
G
Decision Tree
„
internal nodes: operators
external nodes: operands
C
H
© 2004 Goodrich, Tamassia
„
A
We call the children of an internal
node left child and right child
Alternative recursive definition: a
binary tree is either
„
arithmetic expressions
decision processes
searching
„
Each internal node has at most two
children (exactly two for proper, or
perfectly balanced, binary trees)
The children of a node are an
ordered pair
„
Binary tree associated with an arithmetic expression
Applications:
A binary tree is a tree with the
following properties:
11
3
Algorithm printExpression(v)
if hasLeft (v)
print(“(’’)
printExpression (left(v))
print(v.element ())
if hasRight (v)
printExpression(right(v))
print (“)’’)
b
((2 × (a − 1)) + (3 × b))
1
© 2004 Goodrich, Tamassia
Trees
12
2
Sequences
11/12/2007 2:12 PM
Evaluate Arithmetic Expressions
Specialization of a postorder
traversal
„
„
recursive method returning
the value of a subtree
when visiting an internal
node, combine the values
of the subtrees
+
×
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
×
−
2
5
3
„
„
„
13
A
D
D
F
F
∅
E
∅
E
Trees
© 2004 Goodrich, Tamassia
14
Array-Based Representation of
Binary Trees
nodes are stored in an array
∅
A
B
∅
B
…
2
∅
A
„
D
∅
E
∅
∅
C
Trees
∅
15
4
rank(root) = 1
if node is the left child of parent(node),
rank(node) = 2*rank(parent(node))
if node is the right child of parent(node),
rank(node) = 2*rank(parent(node))+1
© 2004 Goodrich, Tamassia
5
E
Trees
6
7
C
F
10
J
11
G
H
16
Height is logarithmic in size
A binary tree is proper if every node has exactly 2
children, apart from the last level which only has
leaves.
level 0
Proper
level 1
binary tree
level 2
of height 3:
This is a very important property of proper,
or perfectly balanced, binary trees
Tree algorithms often work by going down the
tree level by level
So, their running time depends on the number
of levels – but we usually only know the
number of nodes
Let us prove that h = log2 (n + 1) − 1 where n
in the number of nodes. Or (same thing)
number of levels = log2 (n + 1).
level 3
Trees
„
„
E
D
let rank(node) be defined as follows:
„
D
3
B
Properties of proper binary
trees
© 2004 Goodrich, Tamassia
∅
1
Node objects implement
the Position ADT
© 2004 Goodrich, Tamassia
∅
C
Element
Parent node
Left child node
Right child node
C
B
B
C
Trees
A
∅
Node objects implement
the Position ADT
1
A node is represented
by an object storing
„
Element
Parent node
Sequence of children
nodes
„
A
Linked Structure for Binary Trees
„
A node is represented by
an object storing
2
© 2004 Goodrich, Tamassia
„
Linked Structure for Trees
17
© 2004 Goodrich, Tamassia
Trees
18
3
Sequences
11/12/2007 2:12 PM
How many nodes at level k
How many nodes at level k
First, it is useful to find out how many nodes
are at a certain level in the proper binary tree
Let us count levels from 0. This way level k
contains nodes which have depth k.
© 2004 Goodrich, Tamassia
Trees
19
Claim: level k contains 2k nodes.
Proof: by induction on k.
„ (basis of induction) if k = 0, the claim is true:
20 = 1, and we only have one node (root) at level 0.
„ (inductive step): suppose the claim is true for k-1:
level k-1 contains 2k-1 nodes.
Since each node at level k-1 has 2 children, there are
twice as many nodes at level k.
So, level k contains 2 * 2k-1 = 2k nodes.
© 2004 Goodrich, Tamassia
Trees
How many nodes in a tree of
height h?
What is the height of a tree of
size n (with n nodes)?
Theorem: A proper binary tree of height h contains
We know that n = 2h+1 – 1.
2
h+1
- 1 nodes.
So, 2h+1 = n + 1.
Proof: by induction on h
•
•
h + 1 = log 2 (n+1)
(basis of induction): h=0. The tree contains 21 - 1
= 1 node.
h = log 2 (n+1) – 1.
(inductive step): assume a tree of height h-1
contains 2h - 1 nodes. A tree of depth h has one
more level (h) which contains 2h nodes. The total
number of nodes in the tree of height h is: 2 h - 1
+ 2 h = 2 * 2 h – 1 = 2 h+1 - 1.
© 2004 Goodrich, Tamassia
20
Trees
21
So, the height of the tree is logarithmic in the
size of the tree.
The size of the tree is exponential in the height
(number of levels) of the tree.
© 2004 Goodrich, Tamassia
Trees
22
4