CS2468 Tutorial 7 the final result. binary tree) after running BUILD-MAX-HEAP(A).

advertisement
CS2468 Tutorial 7
Q1. Insert the following integers (as keys) into a max-heap. Show the complete binary of
the final result.
1, 2, 3, 4, 5, 6, 7, 8.
Q2. Let A=[8, 4, 5, 1, 7, 3, 2, 4] be an array of 8 integers. Show the result (complete
binary tree) after running BUILD-MAX-HEAP(A).
Q3. Write a java program using heap that takes an array of integers as input, where the n
integers may not be distinct, and output a new array B, where B contains the distinct
integers in non-decreasing order according to their frequency.
For example, A={2, 3, 1, 1, 1, 5, 2 ,1}, where 1 appears 4 times, 2 appears twice, 3 and 5
appears once. Thus, B[1]=1, B[2]=2, B[3]=3, B[4]=5. (B[1]=1, B[2]=2, B[3]=5, B[4]=3
is also an acceptable solution.) Your program should have a running time complexity
O(nlog n).
The java code for heap can be downloaded in week 7’s folder as MyHeap.java.
Hint: Before working on program, give an algorithm first.
Download