ppt

advertisement
Exercise problems: Union-Find &
Height biased Leftist trees
Problem 11.61
• Assume that we start with singleton sets and
perform unions, using the height rule. Let t be
a tree with p nodes created in this way. Prove
that the height of t is at most ⌊log2 p⌋ + 1.
Recap: Union with Height Rule
• Make tree with smaller height a subtree of the other tree.
• Break ties arbitrarily.
13
4
7
5
8
9
11
3
22
6
30
2
1
10
union(7,13)
20
16
14
12
Proof:
• Prove that the number of elements in a tree of
height h is at least 2h-1. We shall do this by
induction on h.
• Base case: use h = 1. A tree of height 1 has
exactly 1 >= 2h-1 element/node.
• Assume that trees of height h for h < m, where
m is an arbitrary integer >= 1, have at least 2h-1
elements.
Proof: (cont.)
• Show that all trees of height m + 1 created
using the height rule have at least 2m
elements
• How is a tree of height m+1 is created?
– Union of two trees of height m
• From induction hypothesis a tree of height m
has 2m - 1 elements
• Therefore, the resulting tree has at least 2m
elements
Problem 11.62
• Give an example of a sequence of unions that
start with singleton sets and create trees
whose height equals the upper bound given in
previous proof. Assume that each union is
performed with the height rule.
Solution:
• Any sequence which always combines sets that
have the same number of elements will do.
• For example, the sequence union(1,2),
union(3,4), union(1,3) results in a tree
of 4 elements and height 3.
• The sequence union(5,6), union(7,8),
union(5,7) also results in a height 3 tree
with 4 elements.
• Performing the operation union(1,5) now results
in a height 4 tree with 8 elements.
Problem 11.59
• Give an example of a sequence of unions that
start with singleton sets and create trees
whose height equals the upper bound (log n).
Assume that each union is performed with the
weight rule.
Solution:
• Similar to 11.62
Problem 12.19
(Height biased Leftist trees)
• Consider the array the Elements = [3, 5, 6, 7, 20,
8, 2, 9, 12, 15, 30, 17].
– Draw the max leftist tree created by initialization
algorithm from class.
– Now insert the elements 10, 18, 11, and 4 (in this
order) using the insert method. Show the max leftist
tree following each insert.
– Perform three remove max operations on the max
leftist tree of part (c). Use the remove method. Show
the max leftist tree following each remove.
Quick Recap:
• We’ll quickly go over the lecture slides on
height biased leftist trees for Push(), Pop(),
and initializing algorithms.
Solution:
• http://www.cise.ufl.edu/~sahni/dsaac/public/
exer/c12/e19.htm
Download