Topic 09 Heaps Solutions.pdf - Solutions Topic 9 Class...

• Homework Help
• 3

This preview shows page 1 - 3 out of 3 pages.

Solutions -- Topic 9, Class 09/26: Heaps Copyright (c) 2016-2018 Dan Suthers. All rights reserved. These solution notes may ONLY be used by students and TAs in ICS 311 Fall 2018 at the University of Hawaii. 1. (4 points) Heap-Delete(A, i) Procedure Heap-Delete( A , i ) deletes the item at index i in heap A (represented as an array). Give an implementation of Heap-Delete that runs in O(lg n ) time for a heap of size n = A.heapSize. You may use instance variable A.heapSize and any of the other procedures already defined in the text (see the end of this document). You do not need to include error checking. You do not need to return anything. Hint: What other heap procedure does something similar? How does it work? 2 points. Take off 1 for minor code error, and give 2.0 if there was some right idea but a logic error. Heap-Delete(A,i) 1 A[i] = A[A.heapSize] 2 A.heapSize = A.heapSize - 1 3 // Max-Heapify(A, i) on i and ancestors or: 3 while i >= 1 4 Max-Heapify(A, i) 5 i = floor(i/2) // go to parent even better: 4 value = A[i] 5 j = floor(i/2) 6 while j >= 1 and A[j] < value 7 i = j 8 j = floor(i/2) 9 A[i] = value
2. (6 points) Heapsort on Sorted Data

You've reached the end of your free preview.

Want to read all 3 pages?

• Fall '08
• Barjaktarovic,M

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern