Download presentation
Presentation is loading. Please wait.
Published byGerard Hill Modified over 9 years ago
1
Foundations of Data Structures Practical Session #8 Heaps
2
Binary heap properties Heap A binary heap can be considered as a complete binary tree (the last level is full from the left to a certain point). Maximum- Heap greater maximal 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 greater minimal 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) 2
3
Heap-array 3 100 193617325127
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. 4 1. Insert 152. Swap 15, 83. Swap 15, 11
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.) 5 1. Remove 112. Put 4 as root3. Swap 4, 8
6
Question 1 A 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. 6
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) 7
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. 8
9
Question 2 solution 9
10
Question 3 10InitInsert(x) FindMin() FindMax DelMin DelMax
11
Question 3 solution Store the elements in two heaps, Min-Heap H min and Max-Heap H max with mutual pointers, each element has a pointer to the element with the same value in the other heap. 11 Init Build H min in O(n) and H max in O(n) Insert(x) Insert x to H min in O(logn) Insert x to H max in O(logn) Update the pointers in O(1) FindMin() Return the root of H min in O(1) FindMax Return the root of H max in O(1) DelMin Delete the minimum from H min in O(logn) Delete the same element from H max by following the mutual pointer in O(logn) DelMax Delete the maximum from H max in O(logn) Delete the same element from H min by following the mutual pointer in O(logn)
12
Question 4 Two min-heaps are given, H 1 and H 2 with n 1 and n 2 keys respectively. Each element of H 1 is smaller than each element of H 2. Suggest an algorithm for merging H 1 and H 2 into a single heap H (with n 1 +n 2 keys) in O(n 2 ) time. 12
13
Question 4 solution 13
14
Question 4 solution 14
15
Question 4 solution 15
16
Question 5 16 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.