(a) [10 pts]
Show, using the definition of the big-Oh, that the function f(n)
= 0.5n + 255 is O(n).
(b) [20 pts] Characterize, using the big-Oh notation, the worst-case running time of the following algorithm:
Let A be a given array of n integers.
for i <- 0 to n-1 do
for j <- 0 to (i*i)-1 do
Let A[j mod n] <- j.
end for
end for
Best characterization: O(_______).
(b) [5 pts] Underscore the line(s) (if any) in the following code which
potentially break the principle of encapsulation:
class Node {
public Object element;
public Node next;
Node() { this(null, null); }
void setNext(Node newText) { next = newText; }
Node getNext() { return next; }
}
(c) [15 pts] Complete and/or change the above class to implement a Node of a linked list the way it should be done according to the design principles. Use the free lines above if necessary.
(a) [15 pts] Write down the resulting sequence (in order), after you do the following operations (in the order presented):
S.insertFirst(3);
S.insertFirst(8);
S.remove(S.last());
S.remove(S.last());
S.remove(S.last());
S.insertAfter(S.first(),9);
(b) [15 pts] Suppose you have the same sequence as in (a) at
the beginning. Write a possible ordering of statements to bring it to
the following state:
1 2 3 4 5
(b) [5 pts] How many possibilities are there to answer the
question (a)? (Two trees are considered different if any two nodes
have a parent with different label.)
Answer: __________
(c) [20 pts] Draw all minimal-height binary search trees which store the
following numbers:
4 8 16 32 64