heap examples-2 - marmara...
TRANSCRIPT
HEAP EXAMPLES-2 14 May-16 May 2014
Ps-Gözde ALP
1
• Remember…
2
Leftist
3
Example 1: Consider the heap below is it leftist?
Leftist
4
7
12 16
21 8
4
11 19
17 46 25
Example 2: Consider the heaps below are they leftist?
0 0 0 0 0
0 0 1 1
1 1
Leftist- Recursive Merging
5
7
12 16
21 8
4
11 19
17 46 25
Example 3: Recursively merge the two leftist heaps showing each immediate
step.
Compare
6
7
12 16
21 8
4
11 19
17 46 25
Compare
7
7
12 16
21 8
4
11
19 17 46
25
Compare
8
7
12 16
21 8
4
11
19
17 46
25
Leftist - Recursive Merging 9
Example 4: Recursively merge the two leftist heaps showing each immediate
step.
10
11
12
Leftist – Nonrecursive Merging 13
Example 5: Non recursively merge the two leftist heaps showing each
immediate step.
Remember Non recursive Merging Algorithm
Requires 2 passes on the heap:
First pass: Create a new tree by merging
the right paths of both heaps in sorted order
Second pass: Perform child swaps at
nodes that violate the leftist heap property.
14
15
3 8 6 7 18 SORT 3 6 7 8 18
16
Heaps
First pass
17
First pass
Second pass
Binomial Trees
18
Useful properties of order k binomial tree Bk.
• Number of nodes = 2𝑘.
• Height = k.
• Degree of root = k.
• Deleting root yields binomial trees B k-1 , … , B0.
The binomial trees B0 through B4. Node depths in B4 are shown. below
19
Binomial Heaps
20
Example 6: What is the Node number, tree number, height and the binary
equivalent of the following tree.
21
22
Example 7: Merge the following binomial trees and show the result.
23
24
+
Example 8: Merge the following binomial heaps and show the result step by
step.
25
26
Example 9: Delete the node with minimum key in binomial heap H and show
the result.
27
H ← Union(H’, H)
28
Example 10: If x=11 insert x into the following heap and show the result.
29
Example 11: How many binomial trees are there in a binomial heap with n
element?
a. At least
b. At most
30
Example 11: How many binomial trees are there in a binomial heap with n
element?
a. At least
b. At most
31
Example 12: How many binomial trees are there in a binomial heap with n
element?
If n is equal to 4378
32
Example 12: How many binomial trees are there in a binomial heap with n
element?
If n is equal to 4378
4378=4096 + 256 + 16 + 8 + 2
4378=212+ 28 + 24 + 23 + 21
4378= 𝐵12 + 𝐵8 + 𝐵4 + 𝐵3 + 𝐵1
33
Example 13: How many leaves does a binomial tree with n elements have?
34
Example 14: Where and how many nodes would you search to find the max
key in a binomial tree Bi confirming to the min-heap order property?
References
35
https://courses.cs.washington.edu/courses/cse326/00wi/handouts/lecture7/sld0
18.htm
http://www.cs.princeton.edu/~wayne/cs423/lectures/heaps-4up.pdf
T.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein, “Introduction to Algorithms,”
2nd edition, MIT Press, 2003