600.226 Data Structures (Spring 2000)

Midterm Exam

Date: Mar 13 11am (40 min.)

VERSION A




 
 
 

1. Algorithm Analysis

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

2. Design

(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)
(  ) 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; }
         }




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

3. Sequence ADT

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);
         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 











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







































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 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: __________

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

      4  8  16  32  64