Trees: Discrete Structures For Computing
Trees: Discrete Structures For Computing
Chapter 11
Ngoc Le
Trees
Discrete Structures for Computing
Contents
Introduction
Properties of Trees
Tree Traversal
Applications of Trees
Binary Search Trees
Decision Trees
Spanning Trees
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
1 Introduction
Properties of Trees
2 Tree Traversal
Contents
Introduction
Tree Traversal
Binary Search Trees
Applications of Trees
Decision Trees Binary Search Trees
Decision Trees
Spanning Trees
4 Spanning Trees Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
5 Minimum Spanning Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.2
Trees
Course outcomes
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
11.3
Trees
Introduction
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Introduction
Properties of Trees
/
Tree Traversal
Applications of Trees
Spanning Trees
Minimum Spanning
tan mail ls junk Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.4
Trees
Tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
A tree (cây ) is a connected undirected graph with no simple circuits.
Consequently, a tree must be a simple graph.
Contents
Introduction
Properties of Trees
Tree Traversal
Applications of Trees
Binary Search Trees
Decision Trees
Spanning Trees
G1 G2 G3 G4
Minimum Spanning
circuit exists Trees
Prim’s Algorithm
Definition
Graphs containing no simple circuits that are not necessarily connected
is forest (rừng ), in which each connected component is a tree.
11.5
Trees
Rooted Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
A rooted tree (cây có gốc) is a tree in which:
• One vertex has been designated as the root and
• Every edge is directed away from the root
Contents
Introduction
Properties of Trees
f g a c Tree Traversal
Applications of Trees
Binary Search Trees
d b a Decision Trees
b c Spanning Trees
d e
Minimum Spanning
e b Trees
Prim’s Algorithm
a f g e d Kruskal’s Algorithm
c f g
11.6
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le
• the ancestors (tổ tiên) of a vertex are the vertices in the path Introduction
Properties of Trees
from the root to this vertex (excluding the vertex itself)
Tree Traversal
• descendants (con cháu) of a vertex v are those vertices that Applications of Trees
Spanning Trees
a Minimum Spanning
Trees
Prim’s Algorithm
b Kruskal’s Algorithm
c
d
f g e
11.7
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
• a vertex of a tree is called a leaf (lá ) if it has no children
• vertices that have children are called internal vertices (đỉnh
Contents
trong ) Introduction
Properties of Trees
a Tree Traversal
Applications of Trees
Binary Search Trees
Decision Trees
b
Spanning Trees
c
Minimum Spanning
d Trees
Prim’s Algorithm
Kruskal’s Algorithm
f g e
11.8
Trees
Terminology
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
If a is a vertex in a tree, the subtree (cây con) with a as its root is
the subgraph of the tree consisting of a and its descendants and
all edges incident to these descendants. Contents
Introduction
a Properties of Trees
Tree Traversal
Applications of Trees
b Binary Search Trees
c Decision Trees
d Spanning Trees
Minimum Spanning
Trees
f g e Prim’s Algorithm
Kruskal’s Algorithm
11.9
Trees
m-ary tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
• m-ary tree (cây m-phân): at most m children on each internal
vertex of a rooted tree.
• full m-ary tree (cây m-phân đầy đủ): every internal vertex has
exactly m children.
Contents
• An m-ary tree with m = 2 is called a binary tree (cây nhị phân). Introduction
Properties of Trees
Tree Traversal
Applications of Trees
Binary Search Trees
Decision Trees
Spanning Trees
Full 3-ary tree Minimum Spanning
Trees
Binary tree Prim’s Algorithm
Kruskal’s Algorithm
11.10
Trees
Ordered Rooted Trees
Huynh Tuong Nguyen,
Definition Tran Tuan Anh, Nguyen
Ngoc Le
left child (con bên trái) and the second is called the right Introduction
Properties of Trees
child (con bên phải). Tree Traversal
a Applications of Trees
Binary Search Trees
Decision Trees
b c Spanning Trees
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
e
f d
g
Left subtree of a Right subtree of a
11.11
Trees
Properties & Theorems
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Theorem
A tree with n vertices has n − 1 edges.
Contents
Theorem Introduction
Tree Traversal
(i) n vertices has (n − 1)/m internal vertices and Applications of Trees
[(m − 1)n + 1]/m leaves Binary Search Trees
Decision Trees
11.12
Trees
Example
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
person, if no one receives more than one letter and if the Binary Search Trees
Decision Trees
chain letter ends after there have been 100 people who read it Spanning Trees
11.13
Trees
Level and Height
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Definition Ngoc Le
Tree Traversal
a Applications of Trees
Binary Search Trees
b k Spanning Trees
• Level of root a = 0, Minimum Spanning
j b, j, k = 1 and
Trees
Prim’s Algorithm
c f l c, e, f, l = 2 . . . Kruskal’s Algorithm
e
• Because the largest
g level of any vertex is
d i m n
4, this tree has height
4.
h
11.14
Trees
Balanced m-ary Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
A rooted m-ary tree of height h is balanced (cân đối) if all leaves
are at levels h or h − 1.
Contents
Introduction
Properties of Trees
Tree Traversal
Applications of Trees
Binary Search Trees
Decision Trees
T1 Spanning Trees
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
T2
11.15
Trees
Balanced m-ary Tree
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Theorem
There are at most mh leaves in an m-ary tree of height h. Contents
Introduction
Tree Traversal
• If an m-ary tree of height h has ` leaves, then h ≥ dlogm `e. Decision Trees
Spanning Trees
• If the m-ary tree is full and balanced, then h = dlogm `e. Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.16
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
loss and games are played until only one entrant has not lost. Introduction
Tree Traversal
Applications of Trees
Exercise (Isomorphic) Binary Search Trees
Decision Trees
How many different isomers (đồng phân) do the following Spanning Trees
• C5 H12
• C6 H14
11.17
Trees
Question
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Exercise
Contents
• How many vertices and how many leaves does a complete Introduction
m-ary tree of height h have? Properties of Trees
Tree Traversal
• Show that a full m-ary balanced tree (cây m-phân hoàn hảo)
Applications of Trees
of height h has more than mh−1 leaves. Binary Search Trees
Decision Trees
• How many edges are there in a forest of t trees containing a
Spanning Trees
total of n vertices? Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.18
Trees
Labeling Ordered Rooted Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
• Ordered rooted trees are often used to store information. Ngoc Le
Introduction
0 Properties of Trees
Tree Traversal
Applications of Trees
Binary Search Trees
1 2 3 Decision Trees
Spanning Trees
Minimum Spanning
1.1 1.2 1.3 3.1 Trees
Prim’s Algorithm
Kruskal’s Algorithm
1.3.1
1.1.1 1.3.2 3.1.1 3.1.2
1.3.1.1
11.19
Trees
Traversal Algorithms (Thuật toán duyệt cây)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Preorder Traversal (duyệt tiền thứ tự - NLR)
procedure preorder (T : ordered rooted tree)
r := root of T
print r
for each child c of r from left to right
Contents
T (c) := subtree with c as its root
Introduction
preorder (T (c)) Properties of Trees
Tree Traversal
a Applications of Trees
Binary Search Trees
Decision Trees
Spanning Trees
b c Minimum Spanning
Trees
d Prim’s Algorithm
Kruskal’s Algorithm
f g e
a b f g c e d
11.20
Trees
Traversal Algorithms
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Introduction
Properties of Trees
a Tree Traversal
Applications of Trees
Binary Search Trees
Decision Trees
b c
Spanning Trees
d
Minimum Spanning
Trees
Prim’s Algorithm
f g e Kruskal’s Algorithm
f b g a e c d
11.21
Trees
Traversal Algorithms
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Postorder Traversal (Duyệt hậu thứ tự - LRN)
procedure postorder (T : ordered rooted tree)
r := root of T
for each child c of r from left to right
T (c) := subtree with c as its root
Contents
postorder (T (c))
Introduction
print r Properties of Trees
Tree Traversal
a Applications of Trees
Binary Search Trees
Decision Trees
Spanning Trees
b c Minimum Spanning
d Trees
Prim’s Algorithm
Kruskal’s Algorithm
f g e
f g b e c d a
11.22
Trees
Infix, Prefix and Postfix Notations
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
• Infix (trung tố ):
((x + y) ↑ 2) + ((x − 4)/3) +
Contents
Introduction
↑ / Properties of Trees
Applications of Trees
+ ↑ +xy2/ − x43 + −
2 3 Binary Search Trees
Decision Trees
Spanning Trees
x y x 4 Minimum Spanning
• Postfix (hậu tố ): Trees
Prim’s Algorithm
x y + 2 ↑ x 4 −3 / + Kruskal’s Algorithm
11.23
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Exercise
Implement postorder, inorder and preorder traversal of the
following tree.
Contents
Introduction
A Properties of Trees
Tree Traversal
Applications of Trees
B C Binary Search Trees
Decision Trees
Spanning Trees
D E F Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
G H I
11.24
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Exercise
Find the ordered rooted tree representing Contents
Introduction
Tree Traversal
Applications of Trees
Then use this rooted tree to find the prefix, postfix and infix forms Binary Search Trees
Spanning Trees
Minimum Spanning
Solution Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.27
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Contents
Exercise
Introduction
Determine postorder of a binary tree with inorder D B H E I A F C Properties of Trees
Applications of Trees
Binary Search Trees
Solution Decision Trees
Spanning Trees
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.28
Trees
Binary Search Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
Binary search tree (cây tìm kiếm nhị phân - BST) is a binary tree
in which the assigned key of a vertex is:
• larger than the keys of all vertices in its left subtree, and
Contents
• smaller than the keys of all vertices in its right subtree. Introduction
Properties of Trees
Tree Traversal
6 Applications of Trees
Binary Search Trees
Decision Trees
2 7 Spanning Trees
Minimum Spanning
Trees
Prim’s Algorithm
1 4 9 Kruskal’s Algorithm
3 5 8
11.32
Trees
Adding and Locating an Item in BST
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
Form a BST for the words mathematics, physics, geography ,
zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics Introduction
Properties of Trees
Tree Traversal
geography physics Applications of Trees
Binary Search Trees
Decision Trees
Minimum Spanning
Trees
Prim’s Algorithm
psychology Kruskal’s Algorithm
Complexity in searching
O(log(n)) vs. O(n) in linear list
11.33
Trees
Decision Trees (Cây quyết định)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example Contents
Introduction
There are seven coins, all with the same weight, and a counterfeit Properties of Trees
coin that weighs less than the others. How many weighings are Tree Traversal
necessary using a balance scale to determine which of the eight Applications of Trees
coins is the counterfeit one? Give an algorithm for finding this Binary Search Trees
Decision Trees
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.34
Trees
Yet Another Application
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Example
If we know that the probability that a person has tuberculosis
(TB) is p(TB) = 0.0005.
We also know p(+|TB) = 0.999 and p(−|TB) = 0.99.
What is p(TB|+) and p(TB|−)? Contents
Introduction
0.999
Properties of Trees
+ TB & + = 0.0004995
Tree Traversal
TB 0.001 Applications of Trees
5
00 − TB & − = 0.0000005 Binary Search Trees
− TB & − = 0.989505
p(TB∩+) 0.0004995
p(TB|+) = p(+) = 0.0004995+0.009995 ≈ 0.0476
11.35
Trees
Problem
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
• A spanning tree (cây khung ) in a graph G is a subgraph of G
Contents
that is a tree which contains all vertices of G. Introduction
Properties of Trees
a b c d Tree Traversal
Applications of Trees
Binary Search Trees
Decision Trees
e g Spanning Trees
f Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.36
Trees
Depth-First Search (Tìm kiếm ưu tiên chiều sâu)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
End! Start!
1
Contents
2 3 4 Introduction
Properties of Trees
Tree Traversal
Stuck! Stuck! Applications of Trees
Spanning Trees
Minimum Spanning
Property Trees
Prim’s Algorithm
11.37
Trees
Depth-First Search
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Algorithm
procedure DFS (G) Contents
Applications of Trees
procedure visit(v: vertex of G) /* recursive */ Binary Search Trees
Decision Trees
for each vertex w adjacent to v and not in T Spanning Trees
add w and edge {v, w} to T Minimum Spanning
visit(w) Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.38
Trees
Breadth-First Search (Tìm kiếm ưu tiên chiều rộng)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
vertex L
Start! ∅
1 1 2, 3, 4
2 3, 4, 5, 6 Contents
3 4, 5, 6 Introduction
4 5, 6, 7, 8 Properties of Trees
2 3 4 5 6, 7, 8 Tree Traversal
Applications of Trees
6 7, 8 Binary Search Trees
8 ∅ Spanning Trees
5 6 7 8 Minimum Spanning
Trees
Prim’s Algorithm
Property Kruskal’s Algorithm
• O(e) or O(n2 )
11.39
Trees
Breadth-First Search
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Algorithm
procedure BFS (G)
T := tree consisting only vertex v1 Contents
11.40
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Exercise
Find spanning tree in the following graphs.
a b c
Contents
Introduction
d e f g Properties of Trees
Tree Traversal
Applications of Trees
Binary Search Trees
h i j Decision Trees
Spanning Trees
a b c
Minimum Spanning
Trees
Prim’s Algorithm
Kruskal’s Algorithm
d e f g
h i
11.41
Trees
Minimum Spanning Trees
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Definition
• A minimum spanning tree (cây khung nhỏ nhất) in a
connected weighted graph is a spanning tree that has the
smallest possible sum of weights of its edges.
Contents
Introduction
Properties of Trees
Tree Traversal
$2000 Applications of Trees
Binary Search Trees
Decision Trees
$1200 Chicago $1000 New York Spanning Trees
San Francisco $9
00 00
$13
Minimum Spanning
Trees
Denver
$70
00
Prim’s Algorithm
$1600
$8
Kruskal’s Algorithm
0
$140
0
$220 Atlanta
0
11.42
Trees
Prim’s Algorithm (Nearest-Neighbor)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
11.43
Trees
Prim’s Algorithm (Nearest-Neighbor)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
• Pick a vertex to start from
• Iteratively absorb smallest edge possible
Contents
a 4 b 5 c Introduction
Properties of Trees
Tree Traversal
4 Applications of Trees
1 1 Binary Search Trees
10 Decision Trees
Spanning Trees
d e Minimum Spanning
Trees
Prim’s Algorithm
5 Kruskal’s Algorithm
10
3
f 10 g
11.44
Trees
Kruskal’s Algorithm (Lightest-Edge)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
e := any edge in G with smallest weight that does not form Applications of Trees
Binary Search Trees
a simple circuit when added to T Decision Trees
11.45
Trees
Kruskal’s Algorithm (Lightest-Edge)
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
a 4 b 5 c Contents
Introduction
Properties of Trees
4 Tree Traversal
1 1 Applications of Trees
10 Binary Search Trees
Decision Trees
d e Spanning Trees
Minimum Spanning
Trees
5
10 Prim’s Algorithm
3 Kruskal’s Algorithm
f 10 g
11.46
Trees
Exercise
Huynh Tuong Nguyen,
Exercise Tran Tuan Anh, Nguyen
Ngoc Le
a 4 b c
Contents
1 2 5 2 4 1 Introduction
Properties of Trees
d e 2 f g Tree Traversal
1 Applications of Trees
2 3 1 3 1 Binary Search Trees
Decision Trees
h i j Spanning Trees
Minimum Spanning
a 1 b 8 c Trees
Prim’s Algorithm
4 2 4 6 1 2 Kruskal’s Algorithm
d e f g
5 1 3
3 2 7
h i
11.47
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Cho một cây có gốc với n đỉnh. Giả thiết một đỉnh trong tập đỉnh Contents
Tree Traversal
A 1 Applications of Trees
C n Spanning Trees
Minimum Spanning
D 2 Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.48
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Xác định tiền tố (prefix) của cây nhị phân có gốc và có thứ tự
(ordered rooted tree) dùng để biểu diễn Contents
Introduction
Tree Traversal
Applications of Trees
Binary Search Trees
A → ∨¬ ∧ p q ∨ ¬q r ∨ ¬p r Decision Trees
Spanning Trees
B p q ∧ ¬ ∨ q¬r ∧ p¬r∨ →
Minimum Spanning
C p q¬ ∨ q¬ ∧ r → p¬ ∨ r Trees
Prim’s Algorithm
D p q¬ ∨ q¬ ∧ r → p¬ ∨ r Kruskal’s Algorithm
11.49
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Introduction
ABC? Properties of Trees
Tree Traversal
A 1 Applications of Trees
C 5 Spanning Trees
Minimum Spanning
D 7 Trees
Prim’s Algorithm
Kruskal’s Algorithm
11.50
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Hãy cho biết hậu tố (post-order traversal) của một cây nhị phân Contents
biết rằng tiền tố (pre-order traversal) là HBGF DECIA và trung Introduction
tố (in-order traversal) là GBF HCEIDA. Properties of Trees
Tree Traversal
Applications of Trees
A GF BCIEADH Binary Search Trees
Decision Trees
B BGF DECIAH
Spanning Trees
C GF BCIEJADH Minimum Spanning
Trees
D GF BHCIEADH Prim’s Algorithm
Kruskal’s Algorithm
11.51
Trees
Exercise
Huynh Tuong Nguyen,
Tran Tuan Anh, Nguyen
Ngoc Le
Cho đồ thị như trong hình vẽ dưới.
8 14
2 32 12 Contents
24 26 Introduction
Properties of Trees
Tree Traversal
18 28 Applications of Trees
Binary Search Trees
16 30 4 Decision Trees
22 Spanning Trees
Minimum Spanning
Trees
10 20 . Prim’s Algorithm
Kruskal’s Algorithm
Cho trước số tự nhiên a > 1, và xét đồ thị đầy đủ K2a+3 . Số lượng Contents
cạnh ta phải xóa khỏi đồ thị K2a+3 để thu được một cây phủ (cây Introduction
khung hay bao trùm, spanning tree) của K2a+3 là bao nhiêu? Properties of Trees
Tree Traversal
Applications of Trees
A 2a + 2 Binary Search Trees
Spanning Trees
C 4a2 + 3a + 1 Minimum Spanning
Trees
D 2a2 + 3a + 1 Prim’s Algorithm
Kruskal’s Algorithm
11.56