Ds 11 Tree

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 198

Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Chapter 10
An Khuong, Le Hong
Trang

Trees
Discrete Structures for Computing on February 22, 2021
Contents
Introduction
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Huynh Tuong Nguyen, Tran Tuan Anh, Nguyen An Khuong, Le Kruskal's Algorithm

Hong Trang
Faculty of Computer Science and Engineering
University of Technology - VNUHCM
trtanh@hcmut.edu.vn - htnguyen@hcmut.edu.vn
10.1
Contents Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

1 Introduction
Properties of Trees
2 Tree Traversal
Contents
Introduction
3 Applications of Trees
Binary Search Trees
Properties of Trees

Tree Traversal
Decision Trees Applications of Trees
Binary Search Trees

Decision Trees

4 Spanning Trees Spanning Trees


Minimum Spanning
Trees
Minimum Spanning Trees
Prim's Algorithm

Prim's Algorithm
Kruskal's Algorithm

Kruskal's Algorithm

10.2
Course outcomes Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Course learning outcomes


L.O.1 Understanding of logic and discrete structures
L.O.1.1  Describe definition of propositional and predicate logic
L.O.1.2  Define basic discrete structures: set, mapping, graphs

L.O.2 Represent and model practical problems with discrete structures Contents
L.O.2.1  Logically describe some problems arising in Computing Introduction
L.O.2.2  Use proving methods: direct, contrapositive, induction Properties of Trees

L.O.2.3  Explain problem modeling using discrete structures Tree Traversal


Applications of Trees
L.O.3 Understanding of basic probability and random variables Binary Search Trees

Decision Trees

L.O.3.1  Define basic probability theory Spanning Trees


L.O.3.2  Explain discrete random variables
Minimum Spanning
Trees
L.O.4 Compute quantities of discrete structures and probabilities Prim's Algorithm

L.O.4.1  Operate (compute/ optimize) on discrete structures Kruskal's Algorithm

L.O.4.2  Compute probabilities of various events, conditional


ones, Bayes theorem

10.3
Introduction Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

• Very useful in computer science: search algorithm, game


winning strategy, decision making, sorting, . . .
• Other disciplines: chemical compounds, family trees,
organizational tree, . . . Contents

/ Introduction
Properties of Trees

Tree Traversal

home bin tmp Applications of Trees


Binary Search Trees

Decision Trees

Spanning Trees
tan mail ls junk Minimum Spanning
Trees
Prim's Algorithm

music latex scala Kruskal's Algorithm

10.4
Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Definition Trang

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
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.5
Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Definition Trang

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

G1 G2 G3 G4 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.5
Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Definition Trang

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

G1 G2 G3 G4 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.5
Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Definition Trang

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

G1 G2 G3 G4 Spanning Trees
circuit exists Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.5
Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Definition Trang

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

G1 G2 G3 G4 Spanning Trees
circuit exists Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.5
Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Definition Trang

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

G1 G2 G3 G4 Spanning Trees
circuit exists Minimum Spanning
Trees
not connected Prim's Algorithm

Kruskal's Algorithm

10.5
Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Definition Trang

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

G1 G2 G3 G4 Spanning Trees
circuit exists Minimum Spanning
Trees
not connected Prim's Algorithm

Kruskal'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.

10.5
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g Properties of Trees

Tree Traversal
Applications of Trees
d Binary Search Trees

b Decision Trees

Spanning Trees
e Minimum Spanning
Trees
a Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g Properties of Trees

Tree Traversal
Applications of Trees
d Binary Search Trees

b Decision Trees

Spanning Trees
e Minimum Spanning
Trees
a Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a Properties of Trees

Tree Traversal
Applications of Trees
d Binary Search Trees

b Decision Trees

Spanning Trees
e Minimum Spanning
Trees
a Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a Properties of Trees

Tree Traversal
Applications of Trees
d b Binary Search Trees

b c Decision Trees

d Spanning Trees
e Minimum Spanning
Trees
a Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a Properties of Trees

Tree Traversal
Applications of Trees
d b Binary Search Trees

b c Decision Trees

d Spanning Trees
e Minimum Spanning
Trees
a f g Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a Properties of Trees

Tree Traversal
Applications of Trees
d b Binary Search Trees

b c Decision Trees

d Spanning Trees
e Minimum Spanning
Trees
a f g e Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a Properties of Trees

Tree Traversal
Applications of Trees
d b Binary Search Trees

b c Decision Trees

d Spanning Trees
e Minimum Spanning
Trees
a f g e Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a c Properties of Trees

Tree Traversal
Applications of Trees
d b Binary Search Trees

b c Decision Trees

d Spanning Trees
e Minimum Spanning
Trees
a f g e Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a c Properties of Trees

Tree Traversal
Applications of Trees
d b a Binary Search Trees

b c Decision Trees

d e Spanning Trees
e Minimum Spanning
Trees
a f g e Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a c Properties of Trees

Tree Traversal
Applications of Trees
d b a Binary Search Trees

b c Decision Trees

d e Spanning Trees
e b Minimum Spanning
Trees
a f g e d Prim's Algorithm

Kruskal's Algorithm

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a c Properties of Trees

Tree Traversal
Applications of Trees
d b a Binary Search Trees

b c Decision Trees

d e Spanning Trees
e b Minimum Spanning
Trees
a f g e d Prim's Algorithm

Kruskal's Algorithm

c f g

10.6
Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
f g a c Properties of Trees

Tree Traversal
Applications of Trees
d b a Binary Search Trees

b c Decision Trees

d e Spanning Trees
e b Minimum Spanning
Trees
a f g e d Prim's Algorithm

Kruskal's Algorithm

c f g

10.6
Terminology Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Definition An Khuong, Le Hong


Trang

• parent (cha) of v is the unique u such that there is a directed


edge from u to v
• when u is the parent of v, v is called a child (con) of u
• vertices with the same parent are called siblings (anh em)
• the ancestors (tê ti¶n) of a vertex are the vertices in the path Contents

from the root to this vertex (excluding the vertex itself) Introduction
Properties of Trees

• descendants (con ch¡u) of a vertex v are those vertices that Tree Traversal

have v as an ancestor Applications of Trees


Binary Search Trees

Decision Trees

a Spanning Trees
Minimum Spanning
Trees
b Prim's Algorithm

Kruskal's Algorithm

c
d

f g e

10.7
Terminology Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
trong ) Contents
Introduction
Properties of Trees

a Tree Traversal
Applications of Trees
Binary Search Trees

b Decision Trees

c Spanning Trees
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

10.8
Terminology Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
a Introduction
Properties of Trees

Tree Traversal
b Applications of Trees
c Binary Search Trees

d Decision Trees

Spanning Trees
Minimum Spanning
g e Trees
f Prim's Algorithm

Kruskal's Algorithm

10.9
m-ary tree
Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Definition Trang

• 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.
• An m-ary tree with m=2 is called a binary tree (c¥y nhà ph¥n). Contents
Introduction
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

Full 3-ary tree Spanning Trees


Minimum Spanning
Binary tree Trees
Prim's Algorithm

Kruskal's Algorithm

Full 5-ary tree 3-ary tree

10.10
Ordered Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Definition
An Khuong, Le Hong

An ordered rooted tree (c¥y câ gèc câ thù tü) is a rooted tree


Trang


where the children of each internal vertex are ordered (e.g. in
order from left to right).

Contents
Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

b c Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

e Kruskal's Algorithm

f d

g
Left subtree of a Right subtree of a
10.11
Ordered Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
Definition
An Khuong, Le Hong

An ordered rooted tree (c¥y câ gèc câ thù tü) is a rooted tree


Trang


where the children of each internal vertex are ordered (e.g. in
order from left to right).
• In an ordered binary tree (c¥y nhà ph¥n câ thù tü), if an
internal vertex has two children, the first child is called the
left child (con b¶n tr¡i ) and the second is called the right Contents
Introduction
child (con b¶n ph£i ). Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

b c Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

e Kruskal's Algorithm

f d

g
Left subtree of a Right subtree of a
10.11
Properties & Theorems Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Theorem

A tree with n vertices has n − 1 edges.

Theorem Contents
Introduction
A full m-ary tree Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.12
Properties & Theorems Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Theorem

A tree with n vertices has n − 1 edges.

Theorem Contents
Introduction
A full m-ary tree Properties of Trees

(i) n vertices has (n − 1)/m internal vertices and Tree Traversal


Applications of Trees
[(m − 1)n + 1]/m leaves Binary Search Trees

(ii) i internal vertices has n = mi + 1 vertices and (m − 1)i + 1


Decision Trees

Spanning Trees
leaves
Minimum Spanning
(iii) ` leaves has n = (m` − 1)/(m − 1) vertices and
Trees

(` − 1)/(m − 1) internal vertices


Prim's Algorithm

Kruskal's Algorithm

10.12
Example Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example (Chain Letter Game)

• Each person who receives the letter is asked to send it on to


four other peoples.
• Some peoples do this, but others do not send any letters.
• How many people have seen the letter, including the first Contents
person, if no one receives more than one letter and if the Introduction
chain letter ends after there have been 100 people who read it Properties of Trees

Tree Traversal
but did not send it out ? Applications of Trees
• How many people sent out the letter? Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.13
Example Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example (Chain Letter Game)

• Each person who receives the letter is asked to send it on to


four other peoples.
• Some peoples do this, but others do not send any letters.
• How many people have seen the letter, including the first Contents
person, if no one receives more than one letter and if the Introduction
chain letter ends after there have been 100 people who read it Properties of Trees

Tree Traversal
but did not send it out ? Applications of Trees
• How many people sent out the letter? Binary Search Trees

Decision Trees

Spanning Trees
Solution
Minimum Spanning
Trees
• Using 4-ary tree with 100 leaves corresponding to 100 persons
Prim's Algorithm

Kruskal's Algorithm

who did not send out the letter.


• =⇒ n = (ml − 1)/(m − 1) = (4 × 100 − 1)/(4 − 1) = 133
vertices and i = n − l = 133 − 100 = 33 internal vertices.

10.13
Level and Height Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Definition An Khuong, Le Hong


Trang

• The level (mùc ) of a vertex v in a rooted tree is the length of


the unique path from the root to this vertex.
• The level of the root is defined to be zero.
• The height (ë cao ) of a rooted tree is the maximum of the
levels of vertices (i.e. the length of the longest path from the Contents
root to any vertex). Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Example
Binary Search Trees

Decision Trees

b k
• Level of root a = 0, Spanning Trees

b, j, k = 1 and
j Minimum Spanning
Trees
c f l c, e, f, l = 2 . . . Prim's Algorithm

e
Because the largest
Kruskal's Algorithm


g level of any vertex is
d i m n
4, this tree has height
4.
h
10.14
Balanced m-ary Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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

T1 Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

T2

10.15
Balanced m-ary Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Theorem

There are at most mh leaves in an m-ary tree of height h. 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

10.16
Balanced m-ary Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Theorem

There are at most mh leaves in an m-ary tree of height h. Contents

It can be proved by using mathematical induction on the height. 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

10.16
Balanced m-ary Tree Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Theorem

There are at most mh leaves in an m-ary tree of height h. Contents

It can be proved by using mathematical induction on the height. Introduction


Properties of Trees

Tree Traversal
Corollary
Applications of Trees
• If an m-ary tree of height h has ` leaves, then h ≥ dlogm `e.
Binary Search Trees

Decision Trees

• If the m-ary tree is full and balanced, then h = dlogm `e. Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.16
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise (Chess tournament)

Suppose 1000 people enter a chess tournament. Use a rooted tree


model of the tournament to determine how many games must be
played to determine a champion. If a player is eliminated after one
loss and games are played until only one entrant has not lost. Contents

(Assume there are no ties) Introduction


Properties of Trees

Tree Traversal
Exercise (Isomorphic) Applications of Trees

How many different isomers (çng ph¥n) do the following


Binary Search Trees

Decision Trees

saturated hydrocarbons have ? Spanning Trees


Minimum Spanning
• C3 H8 Trees
Prim's Algorithm

• C5 H12 Kruskal's Algorithm

• C6 H14

10.17
Question Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

• How many vertices and how many leaves does a complete Contents
m-ary tree of height h have? Introduction

Show that a full m-ary balanced tree (c¥y m-ph¥n ho n h£o )


Properties of Trees

• Tree Traversal
of height h has more than mh−1 leaves. Applications of Trees

How many edges are there in a forest of t trees containing a


Binary Search Trees

• Decision Trees

total of n vertices? Spanning Trees


Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.18
Labeling Ordered Rooted Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
• Ordered rooted trees are often used to store information. An Khuong, Le Hong
Trang
• Need a procedure for visiting each vertex of an ordered rooted tree to
access data.
• Ordering and labeling the vertices is important to traverse them in any
procedure
• Universal address system (h» àa ch¿ phê döng )
0 < 1 < 1.1 < 1.1.1 < 1.2 < 1.3 < . . . < 2 < 3 < 3.1 < . . .
Contents
0 Introduction
Properties of Trees

Tree Traversal
Applications of Trees
1 2 3 Binary Search Trees

Decision Trees

Spanning Trees
1.1 1.2 1.3 3.1 Minimum Spanning
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

10.19
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a b d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a b f d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a b f g d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a b f g d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a b f g d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a b f g c d
10.20
Traversal Algorithms (Thuªt to¡n duy»t c¥y) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Preorder Traversal (duy»t ti·n thù tü - NLR) Trang

procedure preorder (T : ordered rooted tree)


r := root of T
print r
for each child c of r from left to right
T (c) := subtree with c as its root Contents
preorder (T (c)) Introduction
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

a b f g c e d
10.20
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

a d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

a d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

f a d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

f b a d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

f b g a d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

f b g a d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

f b g a d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

f b g a e d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Inorder Traversal (Duy»t trung thù tü - LNR)

Suppose a tree T with root r. If T consists only of r, then r is


inorder traversal of T . Otherwise, suppose r has subtrees T1 , T2 ,
. . . , Tn from left to right, inorder traversal:
T1 → r → T2 → . . . → Tn . Contents
Introduction
a Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

b c Decision Trees

d Spanning Trees
Minimum Spanning
Trees
f g e Prim's Algorithm

Kruskal's Algorithm

f b g a e c d

10.21
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

d
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

f d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

f g d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

f g b d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

f g b d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

f g b d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

f g b e d a
10.22
Traversal Algorithms Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Postorder Traversal (Duy»t hªu thù tü - LRN) Trang

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
postorder (T (c)) Contents
print r Introduction
Properties of Trees

Tree Traversal
a
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
b c
d Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

f g e

f g b e c d a
10.22
Infix, Prefix and Postfix Notations Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

• Infix (trung tè ):
((x + y) ↑ 2) + ((x − 4)/3) +
Contents
↑ / Introduction

Prefix (ti·n tè ):
Properties of Trees

• Tree Traversal
+ ↑ +xy2/ − x43 + 2 − 3 Applications of Trees
Binary Search Trees

Decision Trees

x y x 4 Spanning Trees

• Postfix (hªu tè ): Minimum Spanning


Trees
x y + 2 ↑ x 4 −3 / + Prim's Algorithm

Kruskal's Algorithm

10.23
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Exercise

Find the ordered rooted tree representing


(¬(p ∧ q) ∨ (¬q ∧ r)) → (¬p ∨ ¬r)

Then use this rooted tree to find the prefix, postfix and infix forms Contents
of this expression 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

10.24
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Exercise

Find the ordered rooted tree representing


(¬(p ∧ q) ∨ (¬q ∧ r)) → (¬p ∨ ¬r)

Then use this rooted tree to find the prefix, postfix and infix forms Contents
of this expression Introduction
Properties of Trees

Tree Traversal
Solution
Applications of Trees
• Constructing the rooted tree from the bottom up Binary Search Trees

Decision Trees

• Preorder traversal creates prefix notation Spanning Trees


→ ∨¬ ∧ p q ∧ ¬q r ∨ ¬p ¬r Minimum Spanning
Trees
• Postorder traversal creates postfix notation Prim's Algorithm

Kruskal's Algorithm

p q ∧ ¬ ∨ q¬r ∧ p¬r¬∨ →
• Inorder traversal creates infix notation (with parentheses)
p q¬ ∨ q¬ ∧ r → p¬ ∨ r¬

10.24
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

Find postorder traversal of a binary tree with inorder D B H E I A


F C J G K and preorder A B D E H I C F G J K.
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

10.25
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

Find postorder traversal of a binary tree with inorder D B H E I A


F C J G K and preorder A B D E H I C F G J K.
Solution Contents
Introduction
A Properties of Trees

Tree Traversal
Applications of Trees
B Binary Search Trees

Decision Trees

C Spanning Trees
D E F G Minimum Spanning
Trees
Prim's Algorithm

H I J K Kruskal's Algorithm

Post order: D H I E B F J K G C A.

10.25
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

Find in-order traversal of a binary tree with pre-order A D E B J C


F H I G and post-order E J B D H I F G C A.
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

10.26
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

Find in-order traversal of a binary tree with pre-order A D E B J C


F H I G and post-order E J B D H I F G C A.
Solution Contents
Introduction
A Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

D Decision Trees

B C Spanning Trees
E F G Minimum Spanning
Trees
Prim's Algorithm

J H I Kruskal's Algorithm

In-order: E D J B A H F I C G.

10.26
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

How many different trees are there with the in-order of K E B J C


A H G I D F and father-child relations respecting to the alphabet
order.
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

10.27
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

How many different trees are there with the in-order of K E B J C


A H G I D F and father-child relations respecting to the alphabet
order.
Contents
Solution
Introduction
Properties of Trees

A Tree Traversal
Applications of Trees
B Binary Search Trees

Decision Trees

D
Spanning Trees
Minimum Spanning
E G F Trees
C Prim's Algorithm

Kruskal's Algorithm

K J H I

Pre-order: E D J B A H F I C G.

10.27
Binary Search Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
• smaller than the keys of all vertices in its right subtree. Contents
Introduction
Properties of Trees

6 Tree Traversal
Applications of Trees
Binary Search Trees

2 7 Decision Trees

Spanning Trees
Minimum Spanning
Trees
1 4 9 Prim's Algorithm

Kruskal's Algorithm

3 5 8

10.28
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
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

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
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

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
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
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics
Introduction
Properties of Trees

physics Tree Traversal


Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics
Introduction
Properties of Trees

geography physics Tree Traversal


Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics
Introduction
Properties of Trees

geography physics Tree Traversal


Applications of Trees
Binary Search Trees

zoology
Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics
Introduction
Properties of Trees

geography physics Tree Traversal


Applications of Trees
Binary Search Trees

meteorology zoology
Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics
Introduction
Properties of Trees

geography physics Tree Traversal


Applications of Trees
Binary Search Trees

geology meteorology zoology


Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics
Introduction
Properties of Trees

geography physics Tree Traversal


Applications of Trees
Binary Search Trees

geology meteorology zoology


Decision Trees

Spanning Trees
Minimum Spanning
Trees
psychology Prim's Algorithm

Kruskal's Algorithm

10.29
Adding and Locating an Item in BST Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang
Example

Form a BST for the words mathematics , physics , geography ,


zoology , meteorology , geology , psychology , chemistry using
alphabetical order.
Contents
mathematics
Introduction
Properties of Trees

geography physics Tree Traversal


Applications of Trees
Binary Search Trees

chemistry geology meteorology zoology


Decision Trees

Spanning Trees
Minimum Spanning
Trees
psychology Prim's Algorithm

Kruskal's Algorithm

Complexity in searching

O(log(n)) vs. O(n) in linear list


10.29
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. 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

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig lig
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 Kruskal's Algorithm

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 Kruskal's Algorithm

lig
1

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 Kruskal's Algorithm

lig lig
1 2

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 Kruskal's Algorithm

lig bal lig


1 3 2

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 7 8 Kruskal's Algorithm

lig bal lig


1 3 2

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 7 8 Kruskal's Algorithm

lig bal lig lig bal lig


1 3 2 7 Null 8

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 7 8 4 5 Kruskal's Algorithm

lig bal lig lig bal lig


1 3 2 7 Null 8

10.30
Decision Trees (C¥y quy¸t ành) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Example
Trang

There are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are
necessary using a balance scale to determine which of the eight
coins is the counterfeit one? Give an algorithm for finding this
counterfeit coin. Contents
Introduction
Properties of Trees

1 2 3 4 5 6 Tree Traversal
Applications of Trees

lig bal lig


Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

1 2 7 8 4 5 Kruskal's Algorithm

lig bal lig lig bal lig lig bal lig


1 3 2 7 Null 8 4 6 5

10.30
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Start! Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.31
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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
Properties of Trees

5 TB
Tree Traversal

0 0
0.0
Applications of Trees
Binary Search Trees

Start! Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.31
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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
Properties of Trees

5 TB
Tree Traversal

0 0
0.0
Applications of Trees

Start! 0.999
Binary Search Trees

Decision Trees

5 Spanning Trees
Minimum Spanning
Trees
TB Prim's Algorithm

Kruskal's Algorithm

10.31
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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

0.999
Introduction
+ Properties of Trees

5 TB
Tree Traversal

0 0
0.0
Applications of Trees

Start! 0.999
Binary Search Trees

Decision Trees

5 Spanning Trees
Minimum Spanning
Trees
TB Prim's Algorithm

Kruskal's Algorithm

10.31
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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

0.999
Introduction
+
0
Properties of Trees

5 TB .001
Tree Traversal

0 0
0.0
Applications of Trees

Start! 0.999
Binary Search Trees

Decision Trees

5 Spanning Trees
Minimum Spanning
Trees
TB Prim's Algorithm

Kruskal's Algorithm

10.31
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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

0.999
Introduction
+
0
Properties of Trees

5 TB .001
Tree Traversal

0 0
0.0
Applications of Trees

Start! 0.999
Binary Search Trees

Decision Trees

5 0.01 +
Spanning Trees
Minimum Spanning
TB 0.99 Trees
Prim's Algorithm

− Kruskal's Algorithm

10.31
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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

0.999 TB & + = 0.0004995 Introduction


+
0
Properties of Trees

5 TB .001
Tree Traversal

0 0 TB & − = 0.0000005
0.0
Applications of Trees

Start! 0.999
Binary Search Trees

Decision Trees

5 0.01 + TB & + = 0.009995


Spanning Trees
Minimum Spanning
TB 0.99 Trees
Prim's Algorithm

− TB & − = 0.989505 Kruskal's Algorithm

10.31
Yet Another Application Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

Example Trang

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

0.999 TB & + = 0.0004995 Introduction


+
0
Properties of Trees

5 TB .001
Tree Traversal

0 0 TB & − = 0.0000005
0.0
Applications of Trees

Start! 0.999
Binary Search Trees

Decision Trees

5 0.01 + TB & + = 0.009995


Spanning Trees
Minimum Spanning
TB 0.99 Trees
Prim's Algorithm

− TB & − = 0.989505 Kruskal's Algorithm

p(TB∩+) 0.0004995
p(TB|+) = p(+) = 0.0004995+0.009995 ≈ 0.0476

10.31
Problem Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Definition

• A spanning tree (c¥y khung ) in a graph G is a subgraph of G


that is a tree which contains all vertices of G. 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

10.32
Problem Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Definition

• A spanning tree (c¥y khung ) in a graph G is a subgraph of G


that is a tree which contains all vertices of G. Contents
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

10.32
Problem Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Definition

• A spanning tree (c¥y khung ) in a graph G is a subgraph of G


that is a tree which contains all vertices of G. Contents
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

10.32
Problem Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Definition

• A spanning tree (c¥y khung ) in a graph G is a subgraph of G


that is a tree which contains all vertices of G. Contents
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

10.32
Problem Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Definition

• A spanning tree (c¥y khung ) in a graph G is a subgraph of G


that is a tree which contains all vertices of G. Contents
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

10.32
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Start!
1

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Stuck! Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Stuck! Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

End!
1

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.33
Depth-First Search (T¼m ki¸m ÷u ti¶n chi·u s¥u) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

2 3 4 Contents
Introduction
Properties of Trees

Tree Traversal

5 6 7 8 Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Property Minimum Spanning
Trees
• Go deeper as you can Prim's Algorithm

Kruskal's Algorithm

• Backtrack (quay lui ) to possible branch when you are stuck.


• O(e) or O(n2 )

10.33
Depth-First Search Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Algorithm

procedure DFS (G)


T := tree consisting only vertex v1 Contents

visit (v1 )
Introduction
Properties of Trees

Tree Traversal
procedure visit (v : vertex of G) /* recursive */ Applications of Trees

for each vertex w adjacent to v and not in T


Binary Search Trees

Decision Trees

add w and edge {v, w} to T Spanning Trees


visit (w) Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.34
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1
Contents
Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L
Start! ∅
1 1
Contents
Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L
Start! ∅
1 1 2, 3, 4
Contents
Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4
Contents
Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
Contents
Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents
Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents

4 5, 6 Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents

4 5, 6, 7, 8 Introduction

2 3 4
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents

4 5, 6, 7, 8 Introduction

2 3 4 5 6, 7, 8
Properties of Trees

Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents

4 5, 6, 7, 8 Introduction

2 3 4 5 6, 7, 8
Properties of Trees

Tree Traversal
6 7, 8 Applications of Trees
Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents

4 5, 6, 7, 8 Introduction

2 3 4 5 6, 7, 8
Properties of Trees

Tree Traversal
6 7, 8 Applications of Trees
7 8 Binary Search Trees

Decision Trees

5 6 7 8 Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents

4 5, 6, 7, 8 Introduction

2 3 4 5 6, 7, 8
Properties of Trees

Tree Traversal
6 7, 8 Applications of Trees

End! 7 8 Binary Search Trees

8
Decision Trees

5 6 7 8 ∅ Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.35
Breadth-First Search (T¼m ki¸m ÷u ti¶n chi·u rëng) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

vertex L

1 1 2, 3, 4
2 3, 4, 5, 6
3 4, 5, 6 Contents

4 5, 6, 7, 8 Introduction

2 3 4 5 6, 7, 8
Properties of Trees

Tree Traversal
6 7, 8 Applications of Trees
7 8 Binary Search Trees

8
Decision Trees

5 6 7 8 ∅ Spanning Trees
Minimum Spanning
Trees
Property Prim's Algorithm

Kruskal's Algorithm

• O(e) or O(n2 )

10.35
Breadth-First Search Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Algorithm

procedure BFS (G)


T := tree consisting only vertex v1 Contents
L := empty list Introduction
put v1 in the list L of unprocessed vertices Properties of Trees

while L is not empty Tree Traversal

remove the first vertex, v, from L Applications of Trees

for each neighbor w of v


Binary Search Trees

Decision Trees

if w is not in L and not in T then Spanning Trees

add w to the end of the list L Minimum Spanning


Trees
add w and edge {v, w} to T Prim's Algorithm

Kruskal's Algorithm

10.36
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Exercise Trang

Find spanning tree in the following graphs.


a b c

Contents
d e f g Introduction
Properties of Trees

Tree Traversal
Applications of Trees
h i j Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.37
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Exercise Trang

Find spanning tree in the following graphs.


a b c

Contents
d e f g Introduction
Properties of Trees

Tree Traversal
Applications of Trees
h i j Binary Search Trees

Decision Trees

a b c Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

d e f g Kruskal's Algorithm

h i

10.37
Minimum Spanning Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.38
Minimum Spanning Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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

$2000 Tree Traversal


Applications of Trees
Binary Search Trees

$1200 Chicago $1000 New York Decision Trees

San Francisco $9 Spanning Trees


00 $1300 Minimum Spanning
Denver Trees
$70

00
$1600

$8
Prim's Algorithm

0 Kruskal's Algorithm

$140
0
$220 Atlanta
0

10.38
Minimum Spanning Trees Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

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

$2000 Tree Traversal


Applications of Trees
Binary Search Trees

$1200 Chicago $1000 New York Decision Trees

San Francisco $9 Spanning Trees


00 $1300 Minimum Spanning
Denver Trees
$70

00
$1600

$8
Prim's Algorithm

0 Kruskal's Algorithm

$140
0
$220 Atlanta
0

10.38
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Prim's Algorithm (1957)

procedure Prim(G) Contents


T := a minimum-weight edge Introduction
for i := 1 to n − 2
Properties of Trees

Tree Traversal
e := an edge of minimum weight incident to a vertex in T Applications of Trees
and not forming a simple circuit in T if added to T Binary Search Trees

T := T with e added
Decision Trees

Spanning Trees
return T Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.39
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

1 4 1
Tree Traversal

10 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

1 4 1
Tree Traversal

10 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

1 4 1
Tree Traversal

10 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

4 Tree Traversal
1 10 1 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

4 Tree Traversal
1 10 1 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

4 Tree Traversal
1 10 1 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

4 Tree Traversal
1 10 1 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Prim's Algorithm (Nearest-Neighbor) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

• Pick a vertex to start from An Khuong, Le Hong


Trang

• Iteratively absorb smallest edge possible

a 4 b 5 c Contents
Introduction
Properties of Trees

4 Tree Traversal
1 10 1 Applications of Trees
Binary Search Trees

Decision Trees

d e Spanning Trees
Minimum Spanning
Trees
5 10 Prim's Algorithm

3 Kruskal's Algorithm

f 10 g

10.40
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Kruskal's Algorithm (1958)

procedure Kruskal (G) Contents


T := empty graph Introduction
for i := 1 to n − 1
Properties of Trees

Tree Traversal
e := any edge in G with smallest weight that does not form Applications of Trees
a simple circuit when added to T Binary Search Trees

T := T with e added
Decision Trees

Spanning Trees
return T Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.41
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

• Iteratively add smallest edge possible Trang

a 4 b 5 c
Contents
Introduction

1 4 1
Properties of Trees

Tree Traversal
10 Applications of Trees
Binary Search Trees

d e Decision Trees

Spanning Trees

5 10
Minimum Spanning
Trees
3 Prim's Algorithm

Kruskal's Algorithm

f 10 g

10.42
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

• Iteratively add smallest edge possible Trang

a 4 b 5 c
Contents
Introduction

1 4 1
Properties of Trees

Tree Traversal
10 Applications of Trees
Binary Search Trees

d e Decision Trees

Spanning Trees

5 10
Minimum Spanning
Trees
3 Prim's Algorithm

Kruskal's Algorithm

f 10 g

10.42
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

• Iteratively add smallest edge possible Trang

a 4 b 5 c
Contents
Introduction

1 4 1
Properties of Trees

Tree Traversal
10 Applications of Trees
Binary Search Trees

d e Decision Trees

Spanning Trees

5 10
Minimum Spanning
Trees
3 Prim's Algorithm

Kruskal's Algorithm

f 10 g

10.42
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

• Iteratively add smallest edge possible Trang

a 4 b 5 c
Contents
Introduction

1 4 1
Properties of Trees

Tree Traversal
10 Applications of Trees
Binary Search Trees

d e Decision Trees

Spanning Trees

5 10
Minimum Spanning
Trees
3 Prim's Algorithm

Kruskal's Algorithm

f 10 g

10.42
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

• Iteratively add smallest edge possible Trang

a 4 b 5 c
Contents
Introduction

1 4 1
Properties of Trees

Tree Traversal
10 Applications of Trees
Binary Search Trees

d e Decision Trees

Spanning Trees

5 10
Minimum Spanning
Trees
3 Prim's Algorithm

Kruskal's Algorithm

f 10 g

10.42
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

• Iteratively add smallest edge possible Trang

a 4 b 5 c
Contents
Introduction

1 4 1
Properties of Trees

Tree Traversal
10 Applications of Trees
Binary Search Trees

d e Decision Trees

Spanning Trees

5 10
Minimum Spanning
Trees
3 Prim's Algorithm

Kruskal's Algorithm

f 10 g

10.42
Kruskal's Algorithm (Lightest-Edge) Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong

• Iteratively add smallest edge possible Trang

a 4 b 5 c
Contents
Introduction

1 4 1
Properties of Trees

Tree Traversal
10 Applications of Trees
Binary Search Trees

d e Decision Trees

Spanning Trees

5 10
Minimum Spanning
Trees
3 Prim's Algorithm

Kruskal's Algorithm

f 10 g

10.42
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

By using Prim's and Kruskal's algorithm, determine minimum Contents


Introduction
spanning tree in the following graphs. Properties of Trees

. Tree Traversal
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.43
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

By using Prim's and Kruskal's algorithm, determine minimum


spanning tree in the following graphs.
. Contents
Introduction

4
Properties of Trees

a b c Tree Traversal
1 2 5 2 4 1 Applications of Trees
Binary Search Trees

d e 2 f g Decision Trees

1 Spanning Trees

2 3 1 3 1 Minimum Spanning
Trees
Prim's Algorithm

h i j Kruskal's Algorithm

10.43
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

By using Prim's and Kruskal's algorithm, determine minimum


spanning tree in the following graphs.
. Contents
Introduction

1 8
Properties of Trees

a b c Tree Traversal
4 2 4 6 1 2 Applications of Trees
Binary Search Trees

Decision Trees

d e f g
5 1 3 Spanning Trees

3 2 7 Minimum Spanning
Trees
Prim's Algorithm

h i Kruskal's Algorithm

10.43
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

By using Prim's and Kruskal's algorithm, determine minimum


spanning tree in the following graphs.
.
Contents
a 2 b 3 c 1 d Introduction
Properties of Trees

Tree Traversal
3 1 2 5 Applications of Trees

4 3 3
Binary Search Trees

f g Decision Trees

e h Spanning Trees
Minimum Spanning
4 2 4 3 Trees

3 3 1
Prim's Algorithm

Kruskal's Algorithm

i j k l

10.43
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Exercise

By using Prim's and Kruskal's algorithm, determine minimum


spanning tree in the following graphs. (and maximum spanning
tree (c¥y khung cüc ¤i ).
Contents
a 2 b 3 c 1 d Introduction
Properties of Trees

Tree Traversal
3 1 2 5 Applications of Trees

4 3 3
Binary Search Trees

f g Decision Trees

e h Spanning Trees
Minimum Spanning
4 2 4 3 Trees

3 3 1
Prim's Algorithm

Kruskal's Algorithm

i j k l

10.43
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Given a rooted tree has n verticies. Suppose that degree of every Contents
vertex is n − 1. Which is the height of the tree? Introduction
Properties of Trees

A) 1 Tree Traversal
Applications of Trees
B) n−1 Binary Search Trees

Decision Trees

C) n Spanning Trees
D) 2 Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.44
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Determine the prefix of the following binary ordered rooted tree:


Contents
(¬(p ∧ q) ∨ (¬q ∧ r)) → (¬p ∨ ¬r) Introduction
Properties of Trees

Tree Traversal
A) → ∨¬ ∧ p q ∨ ¬q r ∨ ¬p r Applications of Trees
Binary Search Trees

B) p q ∧ ¬ ∨ q¬r ∧ p¬r∨ → Decision Trees

Spanning Trees
C) p q¬ ∨ q¬ ∧ r → p¬ ∨ r Minimum Spanning
Trees
D) p q¬ ∨ q¬ ∧ r → p¬ ∨ r Prim's Algorithm

Kruskal's Algorithm

10.45
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

How many binary tree (3 vertices: A, B, C ) which has the Contents


preorder-traversal ABC ? Introduction
Properties of Trees

A) 1 Tree Traversal
Applications of Trees
B) 3 Binary Search Trees

Decision Trees

C) 5 Spanning Trees
D) 7 Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.46
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Find the post-order traversal of a binary tree has the pre-order


traversal: HBGF DECIA and in-order traversal: Contents

GBF HCEIDA. Introduction


Properties of Trees

Tree Traversal
A) GF BCIEADH Applications of Trees
B) BGF DECIAH Binary Search Trees

Decision Trees

C) GF BCIEJADH Spanning Trees


Minimum Spanning
D) GF BHCIEADH Trees
Prim's Algorithm

Kruskal's Algorithm

10.47
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

Which is the total weight of the minimum spanning tree of the


An Khuong, Le Hong
Trang

graph below?
8 14

32 Contents
2 12 Introduction
24 26 Properties of Trees

Tree Traversal
18 28 Applications of Trees
Binary Search Trees

Decision Trees

16 30 4 Spanning Trees
22
Minimum Spanning
Trees

.
Prim's Algorithm

10 20 Kruskal's Algorithm

A) 40
B) 60
C) 84

D) 100 10.48
Revision Trees

Given two graphs G and H below: Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen

2 3 B C
An Khuong, Le Hong
Trang

1 4 A D Contents
Introduction
Properties of Trees

Tree Traversal
Applications of Trees

6 5
Binary Search Trees

F E Decision Trees

(G) (H ) Spanning Trees


Minimum Spanning
Trees
Prim's Algorithm

Choose the best answer. Kruskal's Algorithm

A) G is a tree.

B) G and H are isomorphic.


C) If we remove an edge of G, then it will be a tree.
D) If we remove an edge of H , then it will be a tree.
10.49
Revision Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

What is the value of each of these prefix expressions? Contents


a) - * 2 / 8 4 3
Introduction
b) ∗ - * 3 3 * 4 2 5
Properties of Trees

Tree Traversal
c) + - ∗ 3 2 + 2 3 / 6 - 4 2 Applications of Trees
d) * + 3 + 3 ∗ 3 + 3 3 3
Binary Search Trees

Decision Trees

Spanning Trees
Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.50
Revision Trees

Huynh Tuong Nguyen,

Find the minimum spanning tree of the graph below (using both Tran Tuan Anh, Nguyen
An Khuong, Le Hong

of Prim and Kruskal algorithm): Trang

A 11 C 5 E
10
6 2 2 4
S 10 Contents

B 8 D 7 F 8
Introduction
Properties of Trees

H Tree Traversal

14 3 4 6
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees

G Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

10.51
Revision Trees

Huynh Tuong Nguyen,

Find the minimum spanning tree of the graph below (using both Tran Tuan Anh, Nguyen
An Khuong, Le Hong

of Prim and Kruskal algorithm): Trang

A 11 C 5 E
10
6 2 2 4
S 10 Contents

B 8 D 7 F 8
Introduction
Properties of Trees

H Tree Traversal

14 3 4 6
Applications of Trees
Binary Search Trees

Decision Trees

Spanning Trees

G Minimum Spanning
Trees
Prim's Algorithm

Kruskal's Algorithm

By using Prim's or Kruskal's algorithm, could we determine a


minimum spanning tree in a directed graph?
Explain it.
10.51
Exercise Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Cho tr÷îc sè tü nhi¶n a > 1, v x²t ç thà ¦y õ K2a+3 . Sè l÷ñng


c¤nh ta ph£i xâa khäi ç thà K2a+3 º thu ÷ñc mët c¥y phõ (c¥y Contents

khung hay bao tròm, spanning tree ) cõa K2a+3 l bao nhi¶u? Introduction
Properties of Trees

Tree Traversal
A) 2a + 2 Applications of Trees
2 Binary Search Trees

B) 2a + 3a − 1 Decision Trees

C) 4a2 + 3a + 1 Spanning Trees


Minimum Spanning
D) 2a2 + 3a + 1 Trees
Prim's Algorithm

Kruskal's Algorithm

10.52
Revision Trees

Huynh Tuong Nguyen,


Tran Tuan Anh, Nguyen
An Khuong, Le Hong
Trang

Cho G = (V, E) l mët ç thà ìn v væ h÷îng b§t ký, câ n ¿nh.


ành ngh¾a ç thà bò cõa G l Gc = (V, F ) thäa hai t½nh ch§t:
G ∪ Gc = Kn v E ∩ F = ∅. Contents
Introduction
Properties of Trees

Gåi T l c¥y bao tròm cõa ç thà ¦y õ K6 . Sè l÷ñng c¤nh cõa Tree Traversal

ç thà bò T c l Applications of Trees


Binary Search Trees

Decision Trees

A) 5
Spanning Trees
B) 10 Minimum Spanning
Trees
C) 15 Prim's Algorithm

Kruskal's Algorithm

D) 20

10.53

You might also like