Top Banner
Foundations of Data Structures Practical Session #8 Heaps
16

Foundations of Data Structures Practical Session #8 Heaps.

Jan 20, 2016

Download

Documents

Gerard Hill
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Foundations of Data Structures Practical Session #8 Heaps.

Foundations of Data Structures

Practical Session #8Heaps

Page 2: Foundations of Data Structures Practical Session #8 Heaps.

2

Binary heap properties

HeapA binary heap can be considered as a complete binary tree(the last level is full from the left to a certain point).

Maximum-Heap

Each node is greater than or equal to each of its children.The root in a Maximum-Heap is the maximal element in the heap.

Minimum-Heap

Each node is greater than or equal to each of its children.The root in a Minimum-Heap is the minimal element in the heap.

Operations(On max-

heap)

Insert Max Extract-Max Build-Heap

Page 3: Foundations of Data Structures Practical Session #8 Heaps.

3

Heap-array

A heap can be represented as an array: 1. length[A] – size of array A.2. heap-size[A] - number of elements in the heap represented by the array A (heap-size(A) ≤ length(A)). 3. A[1] - the root of the heap 4. For every index :

• Parent(i) = • Left(i) = 2i• Right(i) = 2i+1

100 19 36 17 3 25 1 2 7

Page 4: Foundations of Data Structures Practical Session #8 Heaps.

4

Insert (heapify-up)

1. Add the element to the bottom level of the heap.2. Compare the added element with its parent; if they are in

the correct order, stop.3. If not, swap the element with its parent and return to the

previous step.

1. Insert 15 2. Swap 15, 8 3. Swap 15, 11

Page 5: Foundations of Data Structures Practical Session #8 Heaps.

5

Delete root (heapify-down)

1. Replace the root of the heap with the rightmost element on the last level.

2. Compare the new root with its children; if they are in the correct order, stop.

3. If not, swap the element with one of its children and return to the previous step. (Swap with its smaller child in a min-heap and its larger child in a max-heap.)

1. Remove 11 2. Put 4 as root 3. Swap 4, 8

Page 6: Foundations of Data Structures Practical Session #8 Heaps.

6

Question 1A max-heap is given as a heap-array A of size 63. The heap contains all keys in the range 10..72, each key exactly ones.

1. Where can be located the key 72? Specify possible indexes.Answer: A[1]. in a max-heap the maximum is always at the root.

2. Where can be located the key 10?Answer: anywhere on the last level, i.e., in A[32]…A[63].

3. Which keys can be located in A[2]?Answer: 40-71. The key 40 can be located at A[2] when all the larger keys, 41-70 are rooted at key 71, located at A[3].

4. Where can be located the key 70?Answer: in A[2]…A[7]. There are exactly two elements larger than 70

(71, 72), so it can’t reside lower than level 2. On level two it can reside anywhere.

Page 7: Foundations of Data Structures Practical Session #8 Heaps.

7

Question 2

1. Given a maximum-heap H, what is the time complexity of finding the 3 largest keys in H?

2. In general, what is the time complexity of finding the C largest keys in H? (Where C is a parameter)

Page 8: Foundations of Data Structures Practical Session #8 Heaps.

8

Question 2 solution

It takes O(1) to find the 3 maximal keys in H.The 3 maximal keys in H are:• The root.• The root's maximal son, y.• The largest among y's maximal son and root's

other son x.

Page 9: Foundations of Data Structures Practical Session #8 Heaps.

9

Question 2 solution

Finding the largest keys in is done in a similar way:• We keep a set (a linked-list) of "candidates" for the ’th largest

element. is updated on each iteration. There are iterations.• Assuming we have candidates for the largest element, if we

select one candidate, the remaining are still candidates for the ’th largest element.

• On each iteration we remove just one candidate (the “chosen one”, denoted by ) and add two candidates - 's two children.

• This way we always have candidates for the ’th largest element, from which we select the maximal by traversing in .

• Thus, finding the largest elements costs Remark: The above algorithm finds the largest keys in in (without making the assumption that C is constant). How can one improve the algorithm to run in time?

Page 10: Foundations of Data Structures Practical Session #8 Heaps.

10

Question 3

Suggest a data structure for storing numbers, that supports the following operations in the given time constraints.

Init

Insert(x)

FindMin()

FindMax

DelMin

DelMax

Page 11: Foundations of Data Structures Practical Session #8 Heaps.

11

Question 3 solution

Store the elements in two heaps, Min-Heap Hmin and Max-Heap Hmax with mutual pointers, each element has a pointer to the element with the same value in the other heap.Init• Build Hmin in O(n) and Hmax in O(n)

Insert(x)• Insert x to Hmin in O(logn)• Insert x to Hmax in O(logn)• Update the pointers in O(1)

FindMin()• Return the root of Hmin in O(1)

FindMax• Return the root of Hmax in O(1)

DelMin• Delete the minimum from Hmin in O(logn) • Delete the same element from Hmax by

following the mutual pointer in O(logn)

DelMax• Delete the maximum from Hmax in O(logn) • Delete the same element from Hmin by

following the mutual pointer in O(logn)

Page 12: Foundations of Data Structures Practical Session #8 Heaps.

12

Question 4

Two min-heaps are given, H1 and H2 with n1 and n2 keys respectively. Each element of H1 is smaller than each element of H2.

Suggest an algorithm for merging H1 and H2 into a single heap H (with n1+n2 keys) in O(n2) time.

Page 13: Foundations of Data Structures Practical Session #8 Heaps.

13

Question 4 solution

Case 1: Build a new heap with all the elements of and in time.

Page 14: Foundations of Data Structures Practical Session #8 Heaps.

14

Question 4 solution

Case 2: • Append the array of to the array of . • The resulting array represents a valid heap due to the

following reasoning. Number of leaves in is , hence number of internal nodes is and there are "openings" for new leaves.

• All keys from are added as leaves (possible due to the fact stated in the formula). As all keys in are smaller than any key in , the heap property still holds in the resulting array.

Page 15: Foundations of Data Structures Practical Session #8 Heaps.

15

Question 4 solution

Remarks:• The procedure described in case 2 won’t work in

the 1st case ().• We can append to the end of only if it fits entirely

on the leaves level.• If we can’t place all the keys of in the leaves level

of , and instead we will append to as several levels at the bottom, we might violate the heap property (and get nodes with incorrect order with respect to their children).

Page 16: Foundations of Data Structures Practical Session #8 Heaps.

16

Question 5

Suggest an algorithm for deleting the 'th indexed element in a given heap .

Delete(H, i) if (heap_size(H) < i) error(“heap underflow”) key = H[i] H[i]= H[heap_size] remove H[heap_size] heap_size-- Down-Heapify(H, i) Up-Heapify(H, i)