homework7_heapsort

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)
b)
c)
d)
e)
5
4
3
2
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
20=1
|----------|----------|
14
10
<--------- height h=1
21=2
|---|---|
|---|---|
8
7
9
3 <--------- height h=0
22=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
b) 19 15
c) 19 15
d) 19 15
16 7 15
7
17 10
16
7 17
10 7
17
10
16
10
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) The time complexity to heapify a random n-node binary tree (or
n-number array) is O(n)
b) As implemented in an array, the largest node in the heap is the
first element in the array.
c) Heap sort is an in-place sort since no extra space, besides the
given array, is used during the sorting.
d) Heap sort is a stable sort, because operations on the heap will
keep the relative order of equal items.
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) An inversely sorted array of numbers is always a heap. For
example: A={9,8,7,6, ..., 1 }
b) All the numbers after A[N/2] must satisfy the heap property.
c) All the numbers after A[N/2] have no children.
d) All the numbers before A[N/2] have two children.
===============================================================
Heapsort
The first step to heapsort a random array is to build a heap.
Next steps will be to extract the largest number (at the root) and
replace it with the last node. The new root could violate the heap
property, so heapify is applied to the new root node. Each time, the
size of heap will decrease by one. The process is repeated until the
size of the heap is 1.
Consider the following example.
7
|-----|-----|
5
6
|--|--|
|--|--|
1
3
2
4
Array:
7
A[1]
5
A[2]
6
A[3]
1
A[4]
3
A[5]
2
A[6]
4
A[7]
Step 1. Swap root A[1] with the last node A[7].
Decrease heap size by one. Heapfiy the new root node.
Step 2. Swap the new root node with the last node A[6]=2. Decrease
heap size by one. Heapfiy the new root node.
16. What will the array look like at step 1?
A[1] A[2] A[3] A[4]
a) 6
5
4
1
b) 4
5
6
1
c) 5
4
6
1
d) 7
5
6
1
A[5]
3
3
3
3
17. What will the array look like at step 2?
A[1] A[2] A[3] A[4]
a) 5
3
4
1
b) 4
5
2
1
c) 5
4
2
1
d) 4
5
2
1
2
3
3
3
A[5]
A[6]
2
2
2
2
A[7]
(7)
(7)
(7)
(4)
A[6]
6
6
6
7
A[7]
7
7
7
6
e) None of the above is correct
===============================================================
18. Show the result of the following example where the key of root node A[0] is decreased to 1 from
10.
10 ----------> changed to 1
|-----|-----|
7
8
|--|--|
|--|--|
3
5
4
6
A[1] A[2] A[3] A[4] A[5] A[6] A[7]
10
7
8
3
5
4
6
What will the resulting array look like?
a) 8
7
1
3
5
4
6
b) 8
7
6
3
5
1
4
c) 8
7
6
3
5
4
1
d) 8
7
6
3
4
5
1
===============================================================
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?
Array:
A[1]
7
A[2]
5
A[3]
6
A[4]
1
A[5]
3
A[6]
2
A[7]
4
7
|-----|-----|
5
6
|--|--|
|--|--|
1
3
2
4
(Node A)
^
|_____ change key of node A[5] from 3 to 10
a)
b)
c)
d)
Array:
Array:
Array:
Array:
A[1]
10
7
7
10
A[2]
7
5
10
7
A[3]
6
6
6
6
A[4]
1
1
1
5
A[5]
5
10
5
1
A[6]
2
2
2
2
A[7]
4
4
4
4
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?