(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;
public Node() { this(null, null); }
public void setNext(Node newNode) { next = newNode; }
public 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 (good) design principles. Use the free lines above if necessary.
Refer to p. 87, chap. 3.3 in text book.
(a) [10 pts]
Show, using the definition of the big-Oh, that the function f(n)
= 0.5n3 + 25n - 2 is O(n3).
Let g(n) = n3.
For all n> ; 1, there is a constant c > 30 such that c*g(n) > f(n), therefore f(n) = O(n3).
(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*n)-1 do
for j <- 0 to (2 * log(n)) do
Let A[i mod n] <- A[i mod n] + j.
end for
end for
Best characterization: O( n2log(n)).
(a) [15 pts] Write down the resulting sequence (in order), after you do the following operations (in the order presented):
S.remove(S.last());
S.insertFirst(8);
S.remove(S.last());
S.insertAfter(S.first(),9);
S.insertFirst(3);
(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.insertFirst(1); (1 2 3 6)
S.remove(S.last( )); (1 2 3)
S.insertAfter(S.last( ),4); (1 2 3 4)
S.insertAfter(S.last( ),5); (1 2 3 4 5)
(b) 10 points. Edge should have
destinedNode and accessor method to get it.
(c) 10 points. As long as it works out.
(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___
(c) [20 pts] Draw all minimal-height binary search trees which store the
following numbers:
4 8 16 32 64 128