exercise problems: union-find & height biased leftist trees

12
Exercise problems: Union-Find & Height biased Leftist trees

Upload: dick

Post on 24-Feb-2016

46 views

Category:

Documents


1 download

DESCRIPTION

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 ⌊log 2 p ⌋ + 1. 7. 13. 4. 5. 8. 3. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Exercise problems: Union-Find & Height biased Leftist trees

Exercise problems: Union-Find & Height biased Leftist trees

Page 2: 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 log⌊ 2 p + 1.⌋

Page 3: Exercise problems: Union-Find & Height biased Leftist trees

Recap: Union with Height Rule• Make tree with smaller height a subtree of the other tree.• Break ties arbitrarily.

4

2

930

5

13

11

1

7

8 3 22 6

10

20 16 14 12

union(7,13)

Page 4: Exercise problems: Union-Find & Height biased Leftist trees

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.

Page 5: Exercise problems: Union-Find & Height biased Leftist trees

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

Page 6: Exercise problems: Union-Find & Height biased Leftist trees

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.

Page 7: Exercise problems: Union-Find & Height biased Leftist trees

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.

Page 8: Exercise problems: Union-Find & Height biased Leftist trees

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.

Page 9: Exercise problems: Union-Find & Height biased Leftist trees

Solution:

• Similar to 11.62

Page 10: Exercise problems: Union-Find & Height biased Leftist trees

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.

Page 11: Exercise problems: Union-Find & Height biased Leftist trees

Quick Recap:

• We’ll quickly go over the lecture slides on height biased leftist trees for Push(), Pop(), and initializing algorithms.

Page 12: Exercise problems: Union-Find & Height biased Leftist trees

Solution:

• http://www.cise.ufl.edu/~sahni/dsaac/public/exer/c12/e19.htm