Professional Documents
Culture Documents
Week 3 - Trees
Week 3 - Trees
WARNING
Lecture 3: Trees
Mentimiter: 8747 3745
[GT 2.3]
A/Prof Julian Mestre
School of Computer Science
leaves
root
12
9 26
3 11 24 30
2 5 22 29 31
20
root leaves
E F G H
I J K
I J K
B C D
– Level: set of nodes with given depth
(e.g., {E, F, G, H} are level 2)
E F G H
– Height of a tree: maximum depth
(e.g., 3)
I J K
Substructures of a tree:
B C D
– Edge: pair of nodes (u, v) such that
one is the parent of the other
E F G H
– Path: sequence of nodes such that 2
consecutive nodes in the sequence
have an edge (e.g., <E, B, F, J>). I J K
18 23 31 34 31
16 11
I J K
The University of Sydney Page 12
Ordered Trees
/user/rt/courses/
cs016/ cs252/
grades grades
homeworks/ programs/ projects/
Book
§ 1.1 ... § 1.4 § 5.1 ... § 5.7 § 6.1 ... § 6.5 § 9.1 ... § 9.6
def is_external(v)
# test if v is a leaf
return v.children.is_empty()
def is_root(v)
# test if v is the root
return v.parent = null
Trees are more complex and admit more than one natural way:
– pre-order
– post-order
– in-order (for binary trees)
def pre_order(v)
Nodes are numbered in the
visit(v)
order they are visited when
for each child w of v
we call pre_order() at the root
pre_order(w)
1
Make Money Fast!
2 9
5
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 12
14
18
14 13 20
23
Preorder
visit traversal
order 16
18 23 31 34 31 11 of subtree
31
13
16 11 34
31
20
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
12 18
16
14 13 20 11
23 Postorder
visit 31 traversal
order
18 23 31 34 31 14 of subtree
34
31
16 11 13
20
12
´ ´
2 - 3 b
a 1
The University of Sydney Page 27
Binary tree application: Decision trees
Yes No
Yes No Yes No
Starbucks Chipotle Gracie’s Café Paragon
– Additional methods:
– position leftChild(p) return null when there is
– position rightChild(p) no left, right, or sibling
– position sibling(p) of p, respectively
def is_external(v)
# test if v is a leaf
return v.left = null and v.right = null
1 4 7 9
3 5
´ ´
2 - 3 b
((2 ´ (a - 1)) + (3 ´ b))
a 1
The University of Sydney Page 32
Evaluate Arithmetic Expressions
´ ´
2 - 3 2
5 1
The University of Sydney Page 33
Euler Tour Traversal
Walk around the tree, keeping the tree on your left, and visit each
node three times:
– on the left (preorder)
– from below (inorder) 6
– on the right (postorder)
L 10 R 7
B
2 9 3 4
5 1
The University of Sydney Page 34
Euler Tour Traversal
L 10 R 7
B
2 9 3 4
5 1
6,10,2,2,2,10,9,5,5,5,9,1,1,1,9,10,6,7,3,3,3,7,4,4,4,7,6
L 10 R 7
B
2 9 3 4
5 1
6,10,2,2,2,10,9,5,5,5,9,1,1,1,9,10,6,7,3,3,3,7,4,4,4,7,6
A node is represented by B
an object storing A D
Æ
– Element
C E
– Parent node
– Left child node B
– Right child node
Æ Æ
Node objects implement
the Position ADT
A D
Æ Æ Æ Æ
C E
The University of Sydney
© Goodrich, Tamassia, Goldwasser Page 37
Linked Structure for General Trees
Æ
A node is represented by
an object storing B
– Element
– Parent node Æ Æ
– Sequence of children
A D F
C E
The University of Sydney Page 38
Examples of recursive code on trees
def depth(v)
# compute the depth of v
D E F
def height(v)
# compute height of subtree at v
if v.isExternal() then
# a leave’s height is 0
A
return 0
else
h ¬ 0 B C
for each child w of v
h ¬ max(h, height(w))
return h + 1 D E F
So far we’ve been focused on the structure of the tree. The real usefulness of
trees hinges on the values we store at each element and how these values are
laid out.
BST is a data structure for storing values that can be sorted. These values are
laid out so that an in-order traversal of the BST visits the values in sorted order.
Can search for elements and insert/delete operations run in O(log n) time
provided the tree is “balanced”. More on that next week!
2 9
1 4 8