(In class, Oct 21, 1998)


  1. About Abstract Data Types (ADT):
    • Name an example of a ADT and name the operations that characterize this ADT. No need to explain if you know the right terminology.
    • How is the concept of an ADT expressed in Java?
  2. Consider the pseudo code:
    X = 0;
    for i = 1 to N do
    for j = i to N do
    for k = j to N do
    What is its running time in big-Oh notation?
  3. Give the big-Oh value of the following geometric sums:
    • S1 = 1 + 3 + 9 + 27 + ... + 3n-1 (sum to n terms).
    • S2 = 1 + (1/3) + (1/32) + (1/33) + ... + (1/3i) + ... (infinite sum).
  4. Write a complete Java program which simply prints the string "Hello World!" on the screen.
    NOTE: "complete" means this program would compile and run. Make as short a program as possible, without any comments. It is possible to only use 3 lines of code.
  5. Assume you have a reference H to the first node (or "head") in a singly linked list:
    H --> (a) --> (b) --> (c) --> (d) --> (e) --> null.
    Write a Java method called LastButOne to return a reference to the last but one node (this is node (d) in the figure above). In case the list has only one node, then return H itself. Here is the header for your method:
    public Node LastButOne(Node H);
    Note: the class Node has a public method called getNode() that returns a reference to the next node in the list.
  6. Here is the pre- and post-order traveral listing of nodes in a binary tree:
    pre: (e, c, a, b, d)
    post: (a, b, c, d, e)
    Draw the binary tree. HINT: looking at the pre-order list, you know where to place node e and node c. But node a has two possible places.


  1. ADT:
    • ADT Stack. Characteristic operations are Push and Pop.
    • In Java, ADT's are represented by interfaces.
  2. Each iteration does at most n operations. Hence, the running time is O(n3)
  3. Sums:
    • The infinite sum (1+x+x2+x3+...) has value 1/(1-x) provided |x|<1. Hence answer is O(1).
    • The second sum is O(3n).
  4. Hello Java Program:
    	class Hello {
    	  public static void main(String[] args){
    		System.out.println("Hello World!");
  5. List Java Program:
    	public Node LastButOne(Node H){
    	   Node Prev = H;
    	   H = H.getNext();
    	   if (H == null) {
    		return Prev;
    	   while (H.getNext() != null){
    		Prev = H;
    		H = H.getNext();
  6. Tree Traversal: The tree is e(c(a,b),a).
    NOTATION: x(E1,E2) where E1,E2 are recursively expressions for the left and right subtrees rooted at x. A singleton tree with node x is simply denoted "x".