MADChap8 Trees
MADChap8 Trees
MADChap8 Trees
Trees
MAD101
Ly Anh Duong
[email protected]
Table of Contents
1 Intro.& Def.
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
The Bernoulli family of mathematicians
1 Intro.& Def.
An undirected graph is a tree if and only if there is a unique simple path between
any two of its vertices.
• g is root of tree
• j is parent of m and l
• m or l is a child of j
• m, l are siblings
• g is ancestor of m and m is descendant of
g
• i, k, l, m are leaves: nodes without children.
• g, h, j are Internal nodes: nodes have
children.
• n=i+l
• A rooted tree is called an m-ary tree if every internal vertex has no more
than m children.
• The tree is called a full m-ary tree if every internal vertex has exactly m
children.
• An m-ary tree with m = 2 is called a binary tree.
If an m-ary tree of height h has l leaves, then h ≥ ⌈logm l⌉. If the m-ary tree is full
and balanced, then h = ⌈logm l⌉.
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
Definition
2 Bi.S.Tr.
• A binary search tree where each vertex is labeled with a key.
• The key of a vertex is both larger than the keys of all vertices in its left
subtree and smaller than the keys of all vertices in its right subtree.
Construct the Binary Search Trees for 8, 3, 11, 15, 2, 4, 14. How many comparisons
are required to locate number 6 in the search tree.
Construct the Binary Search Trees for 8, 3, 11, 15, 2, 4, 14. How many comparisons
are required to locate number 6 in the search tree. =⇒ 4 comparisons are
required.
a < b < c...; ab < ac < ae...; abc < abd <
abe...
Note: The algorithm returns the location of x or adds a new vertex with label x
into binary search tree. Complexity O(logn).
Ly Anh Duong Chapter 8 Trees [email protected] 21 / 135
Table of Contents
3 Prefix code
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
Introduction
3 Prefix code
• Standard ASCII (American Standard Code for Information Interchange)
character encoding: use 8 bits (1 byte) to store each character.
• For example, using the standard ASCII encoding, the 12- characters string
”no one knows” requires 12 ∗ 8 = 96 bits total.
12 ∗ 3 = 36 bits total.
• A variable-length encoding
Prefix code is a type of codes which encodes the letters so that the bit string for
a letter never occurs as the first part of the bit string for another letter.
s : 1111
a : 10
n : 1110
e:0
So, ”sane” will be store as 11111011100 →
11 bits.
Hence, compression factor: 32/11 ∼ 3. Is
it best? How to construct a prefix code
that is using the fewest bits?
• Final character
• 0:1
• g : 01
• l : 001
• e : 000
Hence, ”google” will be store as 011101001000 →
12 bits. Compare with ASCII, the copression factor
is 48/12 ∼ 4.
Use Huffman coding algorithm to encode the text ”maximum”. What is the
average number of bits?
Use Huffman coding algorithm to encode the text ”maximum”. What is the
average number of bits?
Ans: The average number of bits used to encode a symbol using this encoding is
15
≈ 2.14
7
Use Huffman coding to encode the following symbols with the frequencies listed:
A: 0.08, B: 0.10, C: 0.12, D: 0.15, E: 0.20, F: 0.35. What is the average number of
bits used to encode a character?
Use Huffman coding to encode the following symbols with the frequencies listed:
A: 0.08, B: 0.10, C: 0.12, D: 0.15, E: 0.20, F: 0.35. What is the average number of
bits used to encode a character?
Ans: The average number of bits used to encode a symbol using this encoding is
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
A counterfeit coin problem
4 De.tree
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
Introduction
5 Tree Traversal
Pre-order traversal: a b e f c d g h
Ly Anh Duong Chapter 8 Trees [email protected] 48 / 135
Pre-order traversal algorithm
5 Tree Traversal
In-order traversal: e b f a c g d h
Ly Anh Duong Chapter 8 Trees [email protected] 50 / 135
In-order traversal algorithm
5 Tree Traversal
Post-order traversal: e f b c g h d a
Ly Anh Duong Chapter 8 Trees [email protected] 52 / 135
Post-order traversal algorithm
5 Tree Traversal
1. Pre-order: 8 3 1 6 7 11 9 13 21
2. In-order: 1 3 6 7 8 9 11 13 21
3. Post-order: 1 7 6 3 9 21 13 11 8
In in-order, the sequences of numbers are sorted.
Ly Anh Duong Chapter 8 Trees [email protected] 55 / 135
Table of Contents
6 I.,P.&P.fix No.
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
Representing the expression
6 I.,P.&P.fix No.
• +: addition
• −: subtraction
• ∗: multiplication
• /: division
• ↑: exponentiation
The expression can be represent by the binary trees with the rules:
• The internal vertices represent operations,
• The leaves represent the variables or numbers.
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
Introduction
7 Spa.Trees
• The only way the roads can be kept open in the winter is by frequently
plowing them,
• The highway department wants to plow the fewest roads so that there will
always be cleared roads connecting any two towns,
• How can this be done?
Ly Anh Duong Chapter 8 Trees [email protected] 66 / 135
Introduction
7 Spa.Trees
At least five roads must be plowed to ensure that there is a path between any two
towns
• Removed {e, f }
The spanning tree above is not unique, there are four different spanning trees
• Arbitrarily choose a vertex of the graph as the root. Form a path starting at
this vertex by successively adding vertices and edges, where each new edge is
incident with the last vertex in the path and a vertex not already in the path.
• Continue adding vertices and edges to this path as long as possible,
1. If the path goes through all vertices of the graph, the tree consisting of this path
is a spanning tree.
2. If the path does not go through all vertices, move back to the next to last vertex
in the path, forming new paths (adding new vertices and edges) that are as long
as possible until no more edges can be added → spanning tree.
• Arbitrarily choose a root from the vertices of the graph, then add all edges
incident to this vertex (level 1),
• For each vertex at level 1, add each edge incident to this vertex to the tree as
long as it does not produce a simple circuit (level 2),
• The same procedure until all the vertices in the tree have been added.
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
Introduction
8 Min.Span.Trees
• Which links should be made to ensure that there is a path between any two
computer centers so that the total cost of the network is minimized?
• We can solve this problem by finding a spanning tree so that the sum of the
weights of the edges of the tree is minimized. Such a spanning tree is called a
minimum spanning tree.
Ly Anh Duong Chapter 8 Trees [email protected] 88 / 135
Definition
8 Min.Span.Trees
• Begin by choosing any edge with smallest weight, putting it into the spanning
tree.
• Successively add to the tree edges of minimum weight that are incident to a
vertex already in the tree, never forming a simple circuit with those edges
already in the tree.
• Stop when n − 1 edges have been added.
=⇒ Total 24
Use Kruskal’s algorithm to find a minimum spanning tree in the weighted graph
▶ Intro.& Def.
▶ Bi.S.Tr.
▶ Prefix code
▶ De.tree
▶ Tree Traversal
▶ I.,P.&P.fix No.
▶ Spa.Trees
▶ Min.Span.Trees
▶ Problems
Quizz
9 Problems
Ans: 2.5
Ly Anh Duong Chapter 8 Trees [email protected] 104 / 135
Quizz
9 Problems
Ans: 5, 3, 2, 4, 6, 7
Ly Anh Duong Chapter 8 Trees [email protected] 106 / 135
Quizz
9 Problems
Ans: 200
Ly Anh Duong Chapter 8 Trees [email protected] 107 / 135
Quizz
9 Problems
Ans:Ly(i)
Anh Duong Chapter 8 Trees [email protected] 108 / 135
Quizz
9 Problems
Ans: dabbce
Ly Anh Duong Chapter 8 Trees [email protected] 110 / 135
Quizz
9 Problems
Ans: 3
Ly Anh Duong Chapter 8 Trees [email protected] 111 / 135
Introduction to Trees
9 Problems