name: __________________________ login: _____________ name: __________________________ login: _____________ name: __________________________ login: _____________
values
sums to target.
For example, if the array contains (8,4,1,6) then
sums2target(array,10)
returns true since 4+6 = 10, but
sums2target(array,8)
returns false since no pair of numbers
sums to 8.
sums2target
? Justify your
answer.
In the diagram below the heap has 9 elements, so only values with
indexes 1-9 (inclusive) are considered part of the heap.
Write the method lessThanCount
that returns the number of
values in heap that are less than parameter
key
. You may want to write an auxiliary method to
help -- your auxiliary method should have a parameter
indicating the root of the (sub)heap being checked, initially
this will be 1. Your code should run in time O(k) where k
is the number of values less than key
. For
example, for the heap above
lessThanCount(heap,9,13)
should return 3 since
three of the nine values in the heap are less than 13.
archive
and D is the size of the dictionary, what is
the big-Oh running time of the code above?
An alternate solution to the APT is given the incomplete code below.
public class TreeNode{
String info;
TreeNode left;
TreeNode right;
TreeNode(String s){
info = s;
}
}
public String decode(String archive, String[] dictionary){
TreeNode root = new TreeNode("dummy");
for(int k=0; k < dictionary.length; k++){
// code here to add dictionary[k] to Huffman-tree 'root'
}
String ret = "";
TreeNode current = root;
for(char ch : archive.toCharArray()) {
if (ch == '0') current = current.left;
else current = current.right;
if (current.left == null && current.right == null){
ret += current.info;
current = root;
}
}
return ret;
}
- In terms if
A and
D what is the big-Oh running time of the code
above? Ignore the lengths of strings in the dictionary, they're
effectively constant.