06 Trees PDF
06 Trees PDF
06 Trees PDF
1
Trees
• Trees
• Binary Trees
• Properties of Binary Trees
• Traversals of Trees
• Data Structures for Trees
2
a Tree
3
Trees
A graph G = (V,E) consists of an set V of VERTICES
and a set E of edges, with E = {(u,v): u,v ∈V, u ≠ v}
4
What is a Tree
• Abstract model of a
Computers”R”Us
hierarchical
structure
• A tree consists of nodes Sales Manufacturing R&D
with a parent-child
relation
• Applications: US International Laptops Desktops
– Organization charts
– File systems
– Programming Europe Asia Canada
environments
5
Example: Genealogical Tree
CHARLES I
PHILIP II MARIA
JOAN
WENZEL
PHILIP III
ERNEST ALBERT
CHARLES 2 daugthers
MAXIMILIAN
MATHIAS
RUDOLPH II
Hasburg Family
6
Tree Terminology
• Root: node without parent (A) • Subtree: tree consisting
of a node and its
descendants
•Internal node: node with at least
one child (A, B, C, F) A
E F G H
•Ancestors of a node: parent, subtree
grandparent, grand-grandparent, etc.
I J K
E F G H
I J K
8
ADTs for Trees
• generic container methods
- size(), isEmpty(), elements()
• query methods
- isRoot(p), isInternal(p), isExternal(p)
• accessor methods
- root(), parent(p), children(p)
• update methods
- application specific
9
Computing the depth of a node
Algorithm depth(T,v)
if T.isRoot(v) then
return 0
else
return 1 + depth(T, T.parent(v))
Complexity ?
10
Now, Traversing a tree!
B C D
E F G H
I J K
11
Traversing Trees
Preorder Traversal
1
Make Money Fast!
2 5 9
1. Motivations 2. Methods References
6 7 8
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery
12
Traversing Trees
Preorder Traversal
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
D B A C F E H L I G
13
Traversing Trees
Postorder Traversal
• In a postorder traversal, a node Algorithm postOrder(v)
is visited after its for each child w of v
descendants postOrder (w)
• Application: compute space used visit(v)
by files in a directory and its
subdirectories
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
14
Traversing Trees
Postorder Traversal
Algorithm postOrder(v)
for each child w of v do
recursively perform postOrder(w)
“visit” node v
A C B F L H I G E D
15
Traversing Trees
Inorder Traversal of a tree (Depth-first)
16
17
CHARLES I
PHILIP II
WENZEL
PHILIP III
ERNEST ALBERT
CHARLES MAXIMILIA
N
RUDOLPH II MATHIAS
right child
left child
{
is a leaf, or
Each node:
has two children
20
Complete Binary Trees
Decision Tree
• Binary tree associated with a decision process
– internal nodes: questions with yes/no answer
– external nodes: decisions
• Example: dining decision
Yes No
Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon
22
Examples of Binary Trees
Arithmetic Expression Tree
• Binary tree associated with an arithmetic
expression
– internal nodes: operators
– external nodes: operands
a 1
23
Properties of Binary Trees
• Notation
n # of nodes e # of leaves
i # of internal nodes h height
Maximum number of
nodes at each level ?
Level 0 1
Level 1 2
Level 2
4
level i ------- 2i 24
Properties of Full Binary Trees
• Notation • Properties:
n number of nodes – e=i+1
e number of leaves – n = 2e - 1
i number of internal nodes – h≤i
h height – h ≤ (n - 1)/2
– e ≤ 2h
– h ≥ log2 e
– h ≥ log2 (n + 1) - 1
25
e=i+1
26
e=i+1
27
e=i+1
28
n = 2e - 1
n=i+ e
e = i + 1 (just proved)
i = e -1
n = 2e - 1
29
h≤i
(h = max num of ancestors)
30
e ≤ 2h level i ------- max num of nodes is 2i
h=3
23 leaves
if all at last
level h
otherwise less
31
Since e ≤ 2h
log2 e ≤ log2 2h
log2 e ≤h
h ≥ log2 e
32
In Perfect Binary Trees…
with height h there are 2h+1 -1 nodes
l=0
n = 2h+1 -1
l=1
l=2
l=3
33
As a consequence:
In Binary trees:
obviously n ≤ 2h+1 -1
n ≤ 2h+1-1
n+1 ≤ 2h+1
h ≥ log (n+1) -1
34
In Complete Binary Trees …
2h- 1
n ≥ 2h
35
n ≥ 2h
It follows that:
36
ADTs for Binary Trees
• accessor methods
-leftChild(p), rightChild(p), sibling(p)
• update methods
-expandExternal(p), removeAboveExternal(p)
37
Traversing Binary Trees
38
Traversing Binary Trees
Preorder, Postorder,
Algorithm preOrder(T,v)
visit(v)
if v is internal:
preOrder (T,T.LeftChild(v))
preOrder (T,T.RightChild(v))
Algorithm postOrder(T,v)
if v is internal:
postOrder (T,T.LeftChild(v))
postOrder(T,T.RightChild(v))
visit(v)
39
Traversing Binary Trees
Inorder
(Depth-first)
Algorithm inOrder(T,v)
if v is internal:
inOrder (T,T.LeftChild(v))
visit(v)
if v is internal:
inOrder(T,T.RightChild(v))
40
Arithmetic Expressions
Inorder: a–b
−
Postorder: a b –
Preorder – a b
a b
Inorder:
2×a−1+3×b
+
× ×
Postorder:
2 − 3 b
2 a 1-× 3 b×+
a 1
41
a + (b • c – d)/e
PRE-ORDER:
+a/–•bcde
POST-ORDER:
abc•d–e/+
IN-ORDER:
a+b•c–d/e
42
Evaluate Arithmetic Expressions
• Specialization of a Algorithm evalExpr(v)
postorder traversal if isExternal (v)
– recursive method returning return v.element ()
the value of a subtree
– when visiting an internal else
node, combine the values x ← evalExpr(leftChild (v))
of the subtrees y ← evalExpr(rightChild (v))
◊ ← operator stored at v
return x ◊ y
+
× ×
2 − 3 2
5 1
43
+
× ×
2 − 3 2
5 1
Eval
Eval
×
×
2 − + 3 2
5 1
Eval Eval
Eval Eval
× − ×
3 2
2 5 1
−
44
5 1
Print Arithmetic Expressions
• Specialization of an inorder
Algorithm printExpression(v)
traversal if isInternal (v)
– print operand or operator when print(“(’’)
visiting node
– print “(“ before traversing left
inOrder (leftChild (v))
subtree print(v.element ())
– print “)“ after traversing right if isInternal (v)
subtree
inOrder (rightChild (v))
print (“)’’)
× ×
2× a−1 + 3×b
2 − 3 b
((2 × (a − 1)) + (3 × b))
a 1 45
Algorithm preOrderTraversalwithStack(T)
Stack S
TreeNode N
46
Algorithm preOrderTraversalwithStack(T)
N = S.pop()
print(N.elem)
b g
c d h i
e f
47
Algorithm preOrderTraversalwithStack(T)
N = S.pop()
print(N.elem)
N
T
b g
c d h i
e f
a
48
Algorithm preOrderTraversalwithStack(T)
b g
c d h i
e f
a
49
Algorithm preOrderTraversalwithStack(T)
N = S.pop()
b g
c d h i
e f
a
50
Algorithm preOrderTraversalwithStack(T)
N = S.pop()
print(N.elem)
N
T
b g
c d h i
e f
a b
51
Algorithm preOrderTraversalwithStack(T)
S.push(N.rightChild)
S.push(N.leftChild)
b g
c d h i
e f
a b
52
Euler Tour Traversal
• generic traversal of a binary tree
• the preorder, inorder, and postorder traversals are special
cases of the Euler tour traversal
• “walk around” the tree and visit each node three times:
– on the left
– from below
– on the right
53
Algorithm eulerTour(T,v)
54
Implementations of Binary trees….
55
Implementing Binary Trees
with a Linked Structure
• A node is represented
by an object storing ∅
– Element
– Parent node
– Left child node
– Right child node B
• Node objects implement
the Position ADT
∅ ∅
B A D
A D ∅ ∅ ∅ ∅
C E C E
56
leftChild(p), rightChild(p), sibling(p):
57
BTNode
Object Element
BTNode left, right, parent
Element
leftChild(v) return v.left
sibling(v)
p ← parent(v)
q ← leftChild(p)
if (v = q) return rightChild(p)
else return q
58
replaceElement(v,obj)
temp ← v.element
v.element ← obj
return temp
swapElements(v,w)
temp ← w.element
w.element ← v.element
v.element ← temp
59
leftChild(p), rightChild(p), sibling(p):
swapElements(p,q),
replaceElement(p,e)
isRoot(p), isInternal(p),
isExternal(p)
60
Other interesting methods for the
ADT Binary Tree:
expandExternal(v): Transform v from an external
node into an internal node by creating two new children
B B
A D A D
C E C E
new1 new2
expandExternal(v):
new1 and new 2 are the new nodes
if isExternal(v)
v.left ← new1
v.right ← new2
size ← size +2
61
removeAboveExternal(v):
B
B
A D
A D
C
C E
G
F G
A D
C
G
62
g p B
B
A D
A D s
C
C E
F G
v G
removeAboveExternal(v):
p if isExternal(v)
{ p ← parent(v)
s ← sibling(v)
s if isRoot(p) s.parent ← null and root ← s
B else
v A { g ← parent(p)
G
if p is leftChild(g) g.left ← s
else g.right ← s
s.parent ← g
}
size ← size -2 }
63
Implementing Complete Binary Trees
with Vectors (Array-based)
1 2 3 4 5 6 7 8 9 10 11 12 13
H D I B E L O A C F G H N
64
leftChild(p), rightChild(p), sibling(p):
swapElements(p,q),
n = 11
replaceElement(p,e)
isRoot(p), isInternal(p),
isExternal(p)
swapElements(p,q),
replaceElement(p,e)
isRoot(p), isInternal(p),
isExternal(p)
66
Implementing General Trees
with a Linked Structure
• A node is represented by an
object storing
– Element
∅
– Parent node
– Sequence of children
nodes B
• Node objects implement
the Position ADT ∅ ∅
A D F
B
A D F
∅ ∅
C E
C E 67
Representing General Trees
tree T
68
RULES
u in T u’ in T’
69
B
T
A D I
B
C E G L
H
stupid
T’
A
F
D
C
I
E
H
Place holder
G
L
F
70
children are “completed” with “fake” nodes
D
D
C
C E G
D