10-Trees (1) Huylm PDF
10-Trees (1) Huylm PDF
10-Trees (1) Huylm PDF
Trees
10.1- Introduction to Trees
Introduction to Trees…
Introduction to Trees
circuit disconnected
Introduction to Trees…
Proof: page 684
Introduction to Trees…
Subtree
Root node (vertex)
Internal node
Leaf
Parent
Child
Siblings
Descendants
Ancestors
Introduction to Trees…
Q4. Construct a binary search tree for numbers: 23, 16, 43, 5, 9, 1, 6, 2, 33, 27.
Algorithm for inserting an element to BST
Complexity: O(logn)
Proof: page 698
Decision Trees
The Counterfeit Coin Problem
Q5. Suppose you have 5 coins, one of which is heavier than the other four. How many
weighings are used to find the fake coin? Draw the decision tree for using a pan balance
scale to find the heavy coin.
Decision Trees
Q6. Suppose you have 8 coins, one of which is lighter than the others. How many weighings are
used to find the fake coin?
Q7. Suppose you have 50 coins, one of which is lighter than the others. How many weighings are
used to find the bad coin?
Prefix Codes: Introduction
Q10. Use Huffman coding to encode the word: “pass the exam”.
What is the average number of bits used to encode a character?
Prefix Codes: Huffman Coding Algorithm
Traversal Algorithms
a, b, e, f, c, d, g, h, i
Inorder Traversal
e, b, f, a, c, g, d, h, i
Postorder Traversal
e, f, b, c, g, h, i, d, a
Traversal
Infix, Prefix, and Postfix Notation
⚫ Expression Trees
Infix, Prefix, and Postfix Notation
⚫ Expression Trees
Infix, Prefix, and Postfix Notation
⚫ Infix form:
operand_1 operator operand_2 x+y
⚫ Prefix form:
operator(operand_1,operand_2) +xy
⚫ Postfix form:
(operand_1,operand_2)operator xy+
⚫ How to find prefix and postfix form from infix
form?
(1) Draw expression tree.
(2) Using Preorder traverse → Prefix form
Using Postorder traverse → Postfix form
Infix, Prefix, and Postfix Notation
Infix form
Prefix form
Postfix form
Infix, Prefix, and Postfix Notation
Infix, Prefix, and Postfix Notation
Q11. Represent the expression ((x +2) ↑ 3) ∗ (y −(3+x)) − 5 using a binary tree. Write
this expression in
a. prefix notation.
b. postfix notation.
Infix, Prefix, and Postfix Notation
Q12. What is the value of each of these prefix expressions?
a. − ∗ 2 / 8 4 3
b. ↑ − ∗ 3 3 ∗ 4 2 5
c. + − ↑ 3 2 ↑ 2 3 / 6 − 4 2
d. ∗ + 3 + 3 ↑ 3 + 3 3 3
39
Spanning trees
Definition.
Let G be a simple graph. A spanning tree of
G is a subgraph of G that is a tree
containing every vertex of G.
40
Spanning trees
41
Remov EdgesThat Form Simple Circuits.
Spanning trees
Q14. Find a spanning tree for each of these graphs.
a) K5 b) K4,4 c) K1,6
d) Q3 e) C5 f ) W5
THEOREM 1
A simple graph is connected if and only if it
has a spanning tree.
43
Depth-First Search
44
The edges selected by
depth-first search of a
graph are called tree
edges.
All other edges are
called back edges.
45
Depth-First Search
Q15. Use depth-first search to find a spanning tree the following graph starting at node 1.
Assume that the vertices are ordered numerically.
46
Breadth-First Search
47
Breadth-First Search
Q15. Use breadth-first search to find a spanning tree the following graph starting at
node 1. Assume that the vertices are ordered numerically.
48
Minimum Spanning Trees
49
Algorithms for Minimum Spanning
Trees
⚫ Prim’s algorithm (1957 by Robert Prim) (originally
discovered by Vojtech Jarník in1930).
⚫ Choosing any edge with smallest weight, putting it into
the spanning tree.
⚫ 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.
50
51
Kruskal’s algorithm (by Joseph
Kruskal in 1956)
⚫ Choose an edge in the graph with minimum weight.
⚫ Add edges with minimum weight that do not form a
simple circuit with those edges already chosen.
⚫ Stop after n−1 edges have been selected.
52
53
54
Minimum Spanning Trees
Q16. Use Prim’s and Kruskal’s algorithm to find a minimum spanning tree for the given
weighted graph.
55
Summary
⚫ Introduction to Trees
⚫ Applications of Trees
⚫ Tree Traversal
⚫ Spanning Trees
⚫ Minimum Spanning Trees