Problem Set 1
Problem Set 1
i. Insert 9 in the minheap. Show the intermediate steps in your solution (you don’t need to
redraw the heap every time, simply indicate which changes happen at every step, and draw
the final result).
ii. From the updated minheap of part i., remove the minimum. Show the intermediate steps in
your solution (you don’t need to redraw the heap every time, simply indicate which changes
happen at every step, and draw the final result).
RealFunction* derivative() which returns (a pointer to) the derivative of the function. (Note that
the return type is Function *, not Function, because Function is an abstract class so we will not
use it as a return type.)
a) Create a class Linear which inherits from RealFunction. Linear represents linear functions, i.e.,
functions of the form f (x) = a1 x+a0 . Therefore the class Linear should have two member variables
to represent a1 and a0 . Implement an appropriate constructor for the class, and implement the
above three functions.
1
b) Create a class Polynomial which inherits from RealFunction
Pn . Polynomial represents polynomial
functions, i.e., functions of the form f (x) = a0 + i=1 ai xi , for some n ∈ N. Therefore the
class Polynomial should have a member variable of type vector coresponding to the coefficients.
Implement an appropriate constructor for the class, and implement the above three functions.
– You may decide that index 0 of the vector corresponds to a0 or it corresponds to an ; both are
acceptable choices; please indicate in comments in your solution which one you are using.
– For full grade, do not use pow in the compute function.
a) Update(A,index,newValue), which is given a min-heap A and two integers index and newValue. It
updates the value of the node at position index to the provided value newValue. For example,
consider the min-heap given in Problem 1-b. The node at index 1 is the node which contains the
value 10 (index 1 is the left child of the root). So if we call Update(A,1,4), the value 10 should be
changed to 4. This change may violate the min-heap property so we may need to fix it.
Provide an algorithm (in pseudo-code) to implement Update(A,index,newValue) (i.e., it changes the
value of the node, and if a min-heap property is violated, it fixes it). Explain why your algorithm
works.
b) Remove(A,index), which is given a min-heap A and an integer index. It removes the node at the
corresponding position. For example, in the min-heap of Problem 1-b, Remove(A,3) should remove
the node that contains the value 12.
Provide an algorithm SymmDiff(A,B) (in pseudo-code) which, given two arrays A and B (we have acccess
to their sizes through A.size and B.size), returns an array corresponding to their symmetric difference.
Faster algorithms are worth more points. For full grade, your algorithm should run in expected
O(n + m) time.
2
Problem 5. Range Queries
Given a data structure D storing integers, a range query is a query of the following form: given a and
b where a ≤ b, return an array containing all values x in D such that a ≤ x ≤ b.
For example, say D contains {3, 8, 2, 9, 7, 5}. Then RangeQuery(D,1,6) should return [3,2,5]. We
do not care about the order of the elements in the output, i.e., [2,3,5], [2,5,3], or all other permutations
of the array [3,2,5] are correct answers. Furthermore, we do not care about duplicates, i.e., if 3 appears
twice in D, it can appear once or twice in the output, both are considered correct. (For simplicity, you
may assume that D does not contain duplicates.)
a) Suppose D is an array. Write in pseudo-code an algorithm to implement RangeQuery(D,a,b). What
is its running time?
b) Design a data structure D (or use one of the data structures we learned) so that RangeQuery can
be answered efficiently, and inserting a new element in D is also efficient. Write in pseudo-code an
algorithm to implement RangeQuery. What is its running time? What is the running time of insert?