You are on page 1of 8

CS502 Design And Analysis of Algorithms

HW #7: Binary Heap and Heapsort


Due Date/Time:
March 22, 2017 (Wednesday Session)
March 24, 2016 (Friday Session)

Student ID:
Name:
==========================================
======
A perfect binary tree is a binary tree in which all interior nodes have two children and
all leaves have the same depth or same level. A complete binary tree is a binary tree
in which every level, except possibly the last, is completely filled, and all nodes are
as far left as possible. A heap is a complete binary tree, where each parent node has
value bigger than its children (heap property).

1. Consider the following array, where number 4 violates heap property. Which of
the following is NOT the reason why it violates the heap property?

a) Number 4 is smaller than its children 10, 14


b) Number 4 is smaller than its parent.
c) Number 4 is smaller than one of its children 14, 7

2. Consider the following array, which is a heap. How many numbers are the leaf
nodes (i.e., without children)?

a) 5
b) 4
c) 3
d) 2
e) 1

3. What is not true about the max-heap property?


a) Parent node cannot be smaller than its children.
b) The root node is the node with the greatest value.
c) The last node in the heap array is the smallest.
d) The first node in the heap array is the largest.

4. What is big-Oh order of the height of a n-node heap?


a) O(log n)
b) O(n)
c) O(n log n)
d) O(n2)

5. What is time complexity of building a n-node heap?


a) O(log n)
b) O(n)
c) O(n log n)
d) O(n2)

6. What is time complexity of sorting n numbers using heapsort?


a) O(log n)
b) O(n)
c) O(n log n)
d) O(n2)
7. What are the minimum and maximum number of nodes in a heap of height h?
a) 2h and 2h+1 1
b) 2h and 2h+1
c) 2h and 2h+1 + 1
d) 2h - 1 and 2h+1
e) 2h + 1 and 2h+1

8. What are the minimum and maximum number of nodes in a heap of height 5?
a) 32 and 63
b) 32 and 64
c) 32 and 65
d) 31 and 64
e) 33 and 64

Hints: The following is a full binary tree of height 2 that has the max number of nodes
16 <--------- height h=2 2 0=1
|----------|----------|
14 10 <--------- height h=1 2 1=2
|---|---| |---|---|
8 7 9 3 <--------- height h=0 2 2=4
........................................................................
The total number of nodes, in general, = 20+21+ ... + 2h

Hints: The following is a binary tree of height 2 that has min number of nodes
16 <--------- height h=2
|----------|----------|
14 10 <--------- height h=1
|---|
8 <--------- height h=0
........................................................................
The total number of nodes, in general, = 20+21+ ... + 2h-1 + 1

9. A heap can be represented by an integer array. Heapfiy is an operation that can


make a given random array as a heap. That is, make all the nodes satisfy the
heap property. For a given random array below, please answer the following
questions.
15
+--------+--------+
19 10
+-----+-----+ +-----+
7 17 16

Array A: 15 19 10 7 17 16
index : A[1] A[2] A[3] A[4] A[5] A[6]

Step 1. Heapfiy node A[3]=10


15
+--------+--------+
19 10 <--- Heapify A[3]=10
+-----+-----+ +-----+
7 17 16
What will be the new array A after heaify A[3]?
a) 15 19 10 7 17 16
b) 15 19 16 7 17 10
c) 15 19 7 17 10 16
d) 16 19 10 7 17 15

10. (Continue) What will be the next node to be checked? Does this node violate the
heap property of that node?
a) A[2]=19, it does not violate the heap property.
b) A[2]=19, it does violate the heap property.
c) A[1]=15, it does not violate the heap property.
d) A[1]=15, it does violate the heap property.

Step 3. The last step is to heapify A[1]=15. (sift down)

15 <------- heapify A[1]=15


+--------+--------+
19 ?
+-----+-----+ +-----+
7 17 ?

11. What will be the new heap tree and array A after heaify A[1]=15:
A
+--------+--------+
B C
+-----+-----+ +-----+
D E F

a) 19 17 16 7 15 10
b) 19 15 7 17 10 16
c) 19 15 16 7 17 10
d) 19 15 10 7 17 16

12. What are the parents and children of node A[i]? (Assume they all exist.)
a) A[i/2], A[2i] and A[2i+1]
b) A[2i], A[i/2] and A[i/2+1]
c) A[i/2], A[2i+1] and A[2i+2]
d) A[2i], A[2i+1] and A[2i+2]

13. Given a 100-node heap, represented in a array A[1-100]. Which of the following is
not correct?
What are the possible child nodes, parent node, and sibling node of A[50] ?
a) The sibling node of A[50] is A[51]
b) The parent node of A[50] is A[25]
c) A[50] has two child node
Note: A sibling node of A[50] is the node that has the same parent as A[50]
14. Which of the following is NOT correct?
a)Thetimecomplexitytoheapifyarandomnnodebinarytree(orn
numberarray)isO(n)
b)Asimplementedinanarray,thelargestnodeintheheapisthe
firstelementinthearray.
c)Heapsortisaninplacesortsincenoextraspace,besidesthe
givenarray,isusedduringthesorting.
d)Heapsortisastablesort,becauseoperationsontheheapwill
keeptherelativeorderofequalitems.

15. For an array A of N numbers, with index starting with 1 (That is, the first number
is A[1] ). Assume N is an even number. Which of the following is NOT correct?
a) Aninverselysortedarrayofnumbersisalwaysaheap.For
example:A={9,8,7,6,...,1}
b)AllthenumbersafterA[N/2]mustsatisfytheheapproperty.
c)AllthenumbersafterA[N/2]havenochildren.
d)AllthenumbersbeforeA[N/2]havetwochildren.
===============================================================

Heapsort
Thefirststeptoheapsortarandomarrayistobuildaheap.Next
stepswillbetoextractthelargestnumber(attheroot)andreplaceit
withthelastnode.Thenewrootcouldviolatetheheapproperty,so
heapifyisappliedtothenewrootnode.Eachtime,thesizeofheap
willdecreasebyone.Theprocessisrepeateduntilthesizeoftheheap
is1.

Considerthefollowingexample.
7
|||
56
||||||
1324

Array:7561324
A[1]A[2]A[3]A[4]A[5]A[6]A[7]

Step1.SwaprootA[1]withthelastnodeA[7].
Decreaseheapsizebyone.Heapfiythenewrootnode.

Step2.SwapthenewrootnodewiththelastnodeA[6]=2.Decrease
heapsizebyone.Heapfiythenewrootnode.

16. What will the array look like at step 1?


A[1]A[2]A[3]A[4]A[5]A[6]A[7]
a)654132(7)
b)456132(7)
c)546132(7)
d)756132(4)

17. What will the array look like at step 2?


A[1]A[2]A[3]A[4]A[5]A[6]A[7]
a)5341267
b)4521367
c)5421367
d)4521376
e)Noneoftheaboveiscorrect
===============================================================

18. Show the result of the following example where the key of root node A[0] is
decreased to 1 from 10.

10>changedto1
|||
78
||||||
3546

A[1]A[2]A[3]A[4]A[5]A[6]A[7]
10783546
Whatwilltheresultingarraylooklike?
a)8713546
b)8763514
c)8763541
d)8763451
===============================================================

19. In the following example of a heap, the key of node A[5] is increased from 3 to 10.
The violation of the heap property caused by this key change can be fixed by
swapping nodes in a manner of bubble up. What will the new array A for this new
heap look like?

A[1]A[2]A[3]A[4]A[5]A[6]A[7]
Array:7561324

7
|||
56
||||||
1324
(NodeA)
^
|_____changekeyofnodeA[5]from3to10

A[1]A[2]A[3]A[4]A[5]A[6]A[7]
a)Array:10761524
b)Array:75611024
c)Array:71061524
d)Array:10765124

20. Consider the use of a heap and a sorted array for implementing priority queues.
Which of the following is not correct?
a) Maximum is (log n) for heap, and (1) for sorted array.
b) ExtractMax is (log n) for heap, and (1) or (n) for sorted array.
c) ChangeKey: (log n). and (n) for sorted array.
d) Insert: (log n), and (n) for sorted array.
21. Build a heap for the given array: 15, 19, 10, 7, 17, 6. What will be the new array
that represents the heap?
a) 19 17 16 15 10 7
b) 19 17 16 7 15 10
c) 19 17 15 10 7 16

22. Build a heap for the given array: 90 35 82 3 24 15 79. What will be the new array
that represents the heap?
a) 3 15 24 35 79 82 90
b) 90 35 82 3 24 15 79
c) 90 82 35 3 24 15 79
d) 90 82 79 3 35 15 24

23. Build a heap for the given array:


23 63 9 33 4 96 68 80 94 31 47 12 58 68 13.
What will be the new array that represents the heap?
a) 96 94 68 80 47 58 68 63 33 31 4 12 9 13 23
b) 96 94 68 80 47 58 68 63 33 31 4 12 9 23 13
c) 96 94 68 80 47 58 68 63 33 31 4 12 13 23 9
d) 96 94 68 80 47 58 68 63 33 31 4 9 12 13 23

24. Build a heap for the given array: 1 7 20 10 2 16 16. What will be the new array
that represents the heap?
a) 16 16 20 10 7 2 1
b) 20 16 16 10 7 2 1
c) 20 16 10 7 2 1 16
d) 20 10 16 7 2 1 16

25. Heaps and Heapsort. Which of the following is TRUE about binary heaps and
heapsort?
a) Finding the maximum element in a min heap tree has a complexity of O(log
n)
b) Turning a random set of elements into a heap has a complexity of O(log n)
c) If the priority of one element in a heap changes, fixing the heap requires a
minimum of O(n) operations
d) The worst-case time complexity of heapsort is O(n2) .
e) Heapsort can be implemented with no additional space

26. Binary Heaps: Which of the following represents a valid binary heap?
Ans: C

You might also like