heap examples-2 - marmara...

35
HEAP EXAMPLES-2 14 May-16 May 2014 Ps-Gözde ALP 1

Upload: others

Post on 09-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

HEAP EXAMPLES-2 14 May-16 May 2014

Ps-Gözde ALP

1

Page 2: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

• Remember…

2

Page 3: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

Leftist

3

Example 1: Consider the heap below is it leftist?

Page 4: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

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

Page 5: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

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

Page 6: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

6

7

12 16

21 8

4

11 19

17 46 25

Compare

Page 7: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

7

7

12 16

21 8

4

11

19 17 46

25

Compare

Page 8: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

8

7

12 16

21 8

4

11

19

17 46

25

Page 9: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

Leftist - Recursive Merging 9

Example 4: Recursively merge the two leftist heaps showing each immediate

step.

Page 10: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

10

Page 11: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

11

Page 12: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

12

Page 13: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

Leftist – Nonrecursive Merging 13

Example 5: Non recursively merge the two leftist heaps showing each

immediate step.

Page 14: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

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

Page 15: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

15

3 8 6 7 18 SORT 3 6 7 8 18

Page 16: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

16

Heaps

First pass

Page 17: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

17

First pass

Second pass

Page 18: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

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

Page 19: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

19

Page 20: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

Binomial Heaps

20

Example 6: What is the Node number, tree number, height and the binary

equivalent of the following tree.

Page 21: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

21

Page 22: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

22

Example 7: Merge the following binomial trees and show the result.

Page 23: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

23

Page 24: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

24

+

Example 8: Merge the following binomial heaps and show the result step by

step.

Page 25: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

25

Page 26: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

26

Example 9: Delete the node with minimum key in binomial heap H and show

the result.

Page 27: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

27

H ← Union(H’, H)

Page 28: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

28

Example 10: If x=11 insert x into the following heap and show the result.

Page 29: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

29

Example 11: How many binomial trees are there in a binomial heap with n

element?

a. At least

b. At most

Page 30: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

30

Example 11: How many binomial trees are there in a binomial heap with n

element?

a. At least

b. At most

Page 31: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

31

Example 12: How many binomial trees are there in a binomial heap with n

element?

If n is equal to 4378

Page 32: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

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

Page 33: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

33

Example 13: How many leaves does a binomial tree with n elements have?

Page 34: HEAP EXAMPLES-2 - Marmara Üniversitesimimoza.marmara.edu.tr/.../courses/cse225/PS/2014Spring/Ps_Heaps… · Leftist 3 Example 1: Consider the heap below is it leftist? Leftist 4

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?