Professional Documents
Culture Documents
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?
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
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
Array A: 15 19 10 7 17 16
index : A[1] A[2] A[3] A[4] A[5] A[6]
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.
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.
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
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