(a) [10 pts] Show, using the definition of the big-Oh, that the
function f(n) = 0.5n + 255 is O(n).
g(n) = n. For all n > 510, g(n) >
f(n), therefore f(n) = 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(__n3_____).
(a) [10 pts] When we talk about linked lists, does it have to
do with (choose the most closely related item):
( ) data types (such as priority queues)
( ) goals (such as adaptability)
( X ) implementation
(of, say, a sequence)
(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; }
}
Note:
It is ok if you did not spot "next = NewText;". No extra credit if
you
did, though :-)
(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.
Refer to
p. 87, chap. 3.3 in text book.
Suppose you have a sequence S, which contains two elements in the following order: 5 4.
(a) [15 pts] Write down the resulting sequence (in order), after you do the following operations (in the order presented):
|
S.insertFirst(3); |
3,5,4 |
|
S.insertFirst(8); |
8,3,5,4 |
|
S.remove(S.last()); |
8,3,5 |
|
S.remove(S.last()); |
8,3 |
|
S.remove(S.last()); |
8 |
|
S.insertAfter(S.first(),9); |
8,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
S.remove(S.last()); //5
S.insertFirst(4); //4,5
S.insertFirst(3); //3,4,5
S.insertFirst(2); //2,3,4,5
S.insertFirst(1); //1,2,3,4,5
---Anything fine as long as it works
here.
[30 pts] Write a pseudocode (or a complete Java code) for classes
implementing (a) a Node class for a node (vertex) in a directed graph, (b) an
Edge class for an edge in a directed graph, (c) a Graph class for a directed
graph (using the Node and Edge classes defined previously). The Graph class
should contain the following methods: (i) a method degree(n) which returns the
number of edges outgoing from a node n, (ii) a method destination(e) returning
the node at the destination end of a directed edge e, and (iii) a method
NumNodes() for returning the number of nodes in the graph.
(a) 10 points. Node should have degree
and accessor method to get it.
(b) 10 points. Edge should have
destinedNode and accessor method to get it.
(c) 10 points. As long as it works out.
(a) [5 pts] Draw a single general tree which does not make any
difference between internal and external nodes, such that it fulfills both (one
solution is sufficient):
(i) it has 6 nodes labeled A, B, C, D, E, F;
(ii) it has the maximal possible height.

(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: _6!=720__
(c) [20 pts] Draw all minimal-height
binary search trees which store the following numbers:
4 8 16 32 64
