600.226 Data Structures (Spring 2000)

Midterm Exam

Date: Mar 13 11am (40 min.)

VERSION B




 
 
 

1. Design

(a) [10 pts] When we talk about arrays, does it have to do with (choose the most closely related item):

(  ) data types (such as priority queues)
(X) implementation (of, say, a stack)
(  ) goals (such as adaptability)

(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.

2. Algorithm Analysis

(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)).

3. Sequence ADT

Suppose you have a sequence S, which contains two elements in the following order:  3  6.

(a) [15 pts] Write down the resulting sequence (in order), after you do the following operations (in the order presented):

         S.remove(S.last()); 3
         S.insertFirst(8); 8 3
         S.remove(S.last()); 8
         S.insertAfter(S.first(),9); 8 9
         S.insertFirst(3); 3 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.insertFirst(2);       (2 3 6)
    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)



4. Graphs

[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 two methods: (i) a method degree(n) which returns the number of edges outgoing from a node n, and (ii) a method outIncidentEdges(n) for returning a (FIFO) Queue of edges outgoing from node n. You may suppose that the Queue ADT (with the usual methods such as enqueue, dequeue, size, isEmpty and front) is ready for you to use.





(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.







5. Trees

(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 minimal 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___

(c) [20 pts] Draw all minimal-height binary search trees which store the following numbers:

      4  8  16  32  64  128