Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Design and Analysis of Algorithm Assignment 04 Submission Date: 12/06/20

Fatima Jinnah Women University

Computer Science

DAA-Assignment 04 (2.5 Points)


A. Sorting Algorithm: Heap

Answer following question:

You are advised to refer to the recommended textbook “Introduction to Algorithms (CLRS) 3rd
edition by Thomas H. Corman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein”. Have a
reading through Chapter 06, 11, 12 and answer the following questions as a follow up exercise:

Question # 01: (20 Marks)

Consider the array A = {29, 18, 10, 15, 20, 9, 5, 13, 2, 4, 15}.

1. Does A satisfy the max-heap property? If not, fix it by swapping two elements.
2. Using array A (possibly corrected), illustrate the execution of the heap-extract-max algorithm, which
extracts the max element and then rearranges the array to satisfy the max-heap property. For each
iteration or recursion of the algorithm, write the content of the array A.

Question # 02:

Answer short questions: (20 Marks)

1. What are the minimum and maximum numbers of elements in a heap of height h?
2. Show that an n-element heap has height ⌊lg n⌋.
3. Show that in any subtree of a max-heap, the root of the subtree contains the largest value
occurring anywhere in that subtree.
4. Is an array that is in sorted order a min-heap?

B. Search Problem: Hashing

Question # 03: (15 Marks)

Given is a list of keys: 36 18 72 43 6 10 5 15

Recall your knowledge of hashing and using the hash function,

Hash-key= Key % Number of Slots in the table.

Generate Hash Keys for a table of size 8. Resolve collision, if any, using

a) Chaining

b) Linear Probing

Draw the hash table, to show before and after scenario with the given data.
Design and Analysis of Algorithm Assignment 04 Submission Date: 12/06/20

C. Search Problem: Binary Search Tree

Question # 04: (15 Marks)

For the set of {1, 4, 5, 10, 16, 17, 21} of keys, draw binary search trees of height 2,3,4,5,6.

Question # 05: (10 Marks)

What is the difference between the binary-search-tree property and the min-heap property?

Can the min-heap property be used to print out the keys of an n-node tree in sorted order in
O(n)O(n) time? Show how? or explain why not.

Note:

You may submit your assignment as a word document, or hand-written on white pages, take images/scan
and convert to pdf format.

Assignments must be submitted by the deadline.

You might also like